github.com/team-ide/go-dialect@v1.9.20/dialect/back/dialect_oracle.go (about)

     1  package back
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  func NewOracleDialect() *OracleDialect {
     9  
    10  	res := &OracleDialect{
    11  		DefaultDialect: NewDefaultDialect(OracleType),
    12  	}
    13  	res.init()
    14  	return res
    15  }
    16  
    17  type OracleDialect struct {
    18  	*DefaultDialect
    19  }
    20  
    21  func (this_ *OracleDialect) init() {
    22  	/** 数值类型 **/
    23  
    24  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BIT", TypeFormat: "NUMBER($l, $d)", HasLength: false, IsNumber: true})
    25  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    26  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SMALLINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    27  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    28  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    29  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "INTEGER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    30  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BIGINT", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    31  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NUMBER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    32  
    33  	/** 小数 **/
    34  
    35  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "FLOAT", TypeFormat: "NUMBER($l, $d)", HasLength: true, HasDecimal: true, IsNumber: true})
    36  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DOUBLE", TypeFormat: "NUMBER($l, $d)", HasLength: true, HasDecimal: true, IsNumber: true})
    37  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DEC", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    38  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DECIMAL", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    39  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NUMBER", TypeFormat: "NUMBER($l, $d)", HasLength: true, IsNumber: true})
    40  
    41  	/** 日期/时间类型 **/
    42  
    43  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "YEAR", TypeFormat: "DATE", IsDateTime: true})
    44  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIME", TypeFormat: "DATE", IsDateTime: true})
    45  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DATE", TypeFormat: "DATE", IsDateTime: true})
    46  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "DATETIME", TypeFormat: "DATE", IsDateTime: true})
    47  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TIMESTAMP", TypeFormat: "TIMESTAMP", IsDateTime: true})
    48  
    49  	/** 字符串类型 **/
    50  
    51  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "CHAR", TypeFormat: "CHAR($l)", HasLength: true, IsString: true})
    52  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "VARCHAR", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    53  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "VARCHAR2", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    54  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYTEXT", TypeFormat: "VARCHAR2($l)", HasLength: true, IsString: true})
    55  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TEXT", TypeFormat: "VARCHAR2(4000)", HasLength: true, IsString: true})
    56  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMTEXT", TypeFormat: "CLOB", HasLength: true, IsString: true})
    57  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "LONGTEXT", TypeFormat: "CLOB", HasLength: true, IsString: true})
    58  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "CLOB", TypeFormat: "CLOB", HasLength: true, IsString: true})
    59  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "ENUM", TypeFormat: "VARCHAR2(50)", HasLength: true, IsString: true})
    60  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "TINYBLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    61  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "BLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    62  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "MEDIUMBLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    63  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "LONGBLOB", TypeFormat: "BLOB", HasLength: true, IsString: true})
    64  
    65  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "SET", TypeFormat: "VARCHAR2(50)", HasLength: true, IsString: true})
    66  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "XMLTYPE", TypeFormat: "XMLTYPE($l)", HasLength: true, IsString: true})
    67  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "RAW", TypeFormat: "RAW($l)", HasLength: true, IsString: true})
    68  	this_.AddColumnTypeInfo(&ColumnTypeInfo{Name: "NVARCHAR2", TypeFormat: "NVARCHAR2($l)", HasLength: true, IsString: true})
    69  
    70  	this_.AddFuncTypeInfo(&FuncTypeInfo{Name: "md5", Format: "md5"})
    71  }
    72  
    73  func (this_ *OracleDialect) OwnerModel(data map[string]interface{}) (owner *OwnerModel, err error) {
    74  	if data == nil {
    75  		return
    76  	}
    77  	owner = &OwnerModel{}
    78  	if data["USERNAME"] != nil {
    79  		owner.Name = data["USERNAME"].(string)
    80  	}
    81  	return
    82  }
    83  func (this_ *OracleDialect) OwnersSelectSql() (sql string, err error) {
    84  	sql = `SELECT USERNAME FROM DBA_USERS ORDER BY USERNAME`
    85  	return
    86  }
    87  func (this_ *OracleDialect) OwnerSelectSql(ownerName string) (sql string, err error) {
    88  	sql = `SELECT USERNAME FROM DBA_USERS `
    89  	sql += ` WHERE USERNAME='` + ownerName + `'`
    90  	return
    91  }
    92  func (this_ *OracleDialect) OwnerChangeSql(ownerName string) (sql string, err error) {
    93  	return
    94  }
    95  func (this_ *OracleDialect) OwnerCreateSql(owner *OwnerModel) (sqlList []string, err error) {
    96  	sql := ``
    97  	sql = `CREATE USER ` + owner.Name + ` IDENTIFIED BY "` + owner.Password + `" `
    98  	sqlList = append(sqlList, sql)
    99  
   100  	sql = `GRANT dba,resource,connect TO ` + owner.Name + ` `
   101  	sqlList = append(sqlList, sql)
   102  	return
   103  }
   104  
   105  func (this_ *OracleDialect) TableModel(data map[string]interface{}) (table *TableModel, err error) {
   106  	if data == nil {
   107  		return
   108  	}
   109  	table = &TableModel{}
   110  	if data["TABLE_NAME"] != nil {
   111  		table.Name = data["TABLE_NAME"].(string)
   112  	}
   113  	if data["OWNER"] != nil {
   114  		table.OwnerName = data["OWNER"].(string)
   115  	}
   116  	return
   117  }
   118  func (this_ *OracleDialect) TablesSelectSql(ownerName string) (sql string, err error) {
   119  	sql = `SELECT TABLE_NAME,OWNER FROM ALL_TABLES  `
   120  	if ownerName != "" {
   121  		sql += `WHERE OWNER ='` + ownerName + `' `
   122  	}
   123  	sql += `ORDER BY TABLE_NAME`
   124  	return
   125  }
   126  func (this_ *OracleDialect) TableSelectSql(ownerName string, tableName string) (sql string, err error) {
   127  	sql = `SELECT TABLE_NAME,OWNER FROM ALL_TABLES `
   128  	sql += `WHERE 1=1 `
   129  	if ownerName != "" {
   130  		sql += `AND OWNER='` + ownerName + `' `
   131  	}
   132  	sql += `AND TABLE_NAME='` + tableName + `' `
   133  	sql += `ORDER BY TABLE_NAME`
   134  	return
   135  }
   136  
   137  func (this_ *OracleDialect) ColumnModel(data map[string]interface{}) (column *ColumnModel, err error) {
   138  	if data == nil {
   139  		return
   140  	}
   141  	column = &ColumnModel{}
   142  	if data["COLUMN_NAME"] != nil {
   143  		column.Name = data["COLUMN_NAME"].(string)
   144  	}
   145  	if data["COMMENTS"] != nil {
   146  		column.Comment = data["COMMENTS"].(string)
   147  	}
   148  	if data["DATA_DEFAULT"] != nil {
   149  		column.Default = GetStringValue(data["DATA_DEFAULT"])
   150  	}
   151  	if data["TABLE_NAME"] != nil {
   152  		column.TableName = data["TABLE_NAME"].(string)
   153  	}
   154  	if data["CHARACTER_SET_NAME"] != nil {
   155  		column.CharacterSetName = data["CHARACTER_SET_NAME"].(string)
   156  	}
   157  
   158  	if GetStringValue(data["NULLABLE"]) == "N" {
   159  		column.NotNull = true
   160  	}
   161  	var columnTypeInfo *ColumnTypeInfo
   162  	if data["DATA_TYPE"] != nil {
   163  		dataType := data["DATA_TYPE"].(string)
   164  		if strings.Contains(dataType, "(") {
   165  			dataType = dataType[:strings.Index(dataType, "(")]
   166  		}
   167  		columnTypeInfo, err = this_.GetColumnTypeInfo(dataType)
   168  		if err != nil {
   169  			return
   170  		}
   171  		column.Type = columnTypeInfo.Name
   172  
   173  		//bs, _ := json.Marshal(data)
   174  		//println("data:", string(bs))
   175  		dataLength := GetStringValue(data["DATA_LENGTH"])
   176  		if dataLength != "" && dataLength != "0" {
   177  			column.Length, err = StringToInt(dataLength)
   178  			if err != nil {
   179  				return
   180  			}
   181  		}
   182  		dataPrecision := GetStringValue(data["DATA_PRECISION"])
   183  		if dataPrecision != "" && dataPrecision != "0" {
   184  			column.Length, err = StringToInt(dataPrecision)
   185  			if err != nil {
   186  				return
   187  			}
   188  		}
   189  		dataScale := GetStringValue(data["DATA_SCALE"])
   190  		if dataScale != "" && dataScale != "0" {
   191  			column.Decimal, err = StringToInt(dataScale)
   192  			if err != nil {
   193  				return
   194  			}
   195  		}
   196  	}
   197  	return
   198  }
   199  func (this_ *OracleDialect) ColumnsSelectSql(ownerName string, tableName string) (sql string, err error) {
   200  	sql = `SELECT t.COLUMN_NAME,t.DATA_DEFAULT,t.TABLE_NAME,t.CHARACTER_SET_NAME,t.NULLABLE,t.DATA_TYPE,t.DATA_LENGTH,t.DATA_PRECISION,t.DATA_SCALE,tc.COMMENTS from ALL_TAB_COLUMNS t `
   201  	sql += "LEFT JOIN ALL_COL_COMMENTS tc ON(tc.OWNER=t.OWNER AND tc.TABLE_NAME=t.TABLE_NAME AND tc.COLUMN_NAME=t.COLUMN_NAME)"
   202  	sql += `WHERE 1=1 `
   203  	if ownerName != "" {
   204  		sql += `AND t.OWNER='` + ownerName + `' `
   205  	}
   206  	sql += `AND t.TABLE_NAME='` + tableName + `' `
   207  	return
   208  }
   209  
   210  func (this_ *OracleDialect) PrimaryKeyModel(data map[string]interface{}) (primaryKey *PrimaryKeyModel, err error) {
   211  	if data == nil {
   212  		return
   213  	}
   214  	primaryKey = &PrimaryKeyModel{}
   215  	if data["COLUMN_NAME"] != nil {
   216  		primaryKey.ColumnName = data["COLUMN_NAME"].(string)
   217  	}
   218  	if data["TABLE_NAME"] != nil {
   219  		primaryKey.TableName = data["TABLE_NAME"].(string)
   220  	}
   221  	if data["OWNER"] != nil {
   222  		primaryKey.OwnerName = data["OWNER"].(string)
   223  	}
   224  	return
   225  }
   226  func (this_ *OracleDialect) PrimaryKeysSelectSql(ownerName string, tableName string) (sql string, err error) {
   227  	sql = `SELECT cu.COLUMN_NAME,au.TABLE_NAME,au.OWNER FROM ALL_CONS_COLUMNS cu, ALL_CONSTRAINTS au `
   228  	sql += `WHERE cu.CONSTRAINT_NAME = au.CONSTRAINT_NAME and au.CONSTRAINT_TYPE = 'P' `
   229  	if ownerName != "" {
   230  		sql += `AND au.OWNER='` + ownerName + `' `
   231  	}
   232  	sql += `AND au.TABLE_NAME='` + tableName + `' `
   233  	return
   234  }
   235  
   236  func (this_ *OracleDialect) IndexModel(data map[string]interface{}) (index *IndexModel, err error) {
   237  	if data == nil {
   238  		return
   239  	}
   240  	index = &IndexModel{}
   241  	if data["INDEX_NAME"] != nil {
   242  		index.Name = data["INDEX_NAME"].(string)
   243  	}
   244  	if data["COLUMN_NAME"] != nil {
   245  		index.ColumnName = data["COLUMN_NAME"].(string)
   246  	}
   247  	if data["INDEX_COMMENT"] != nil {
   248  		index.Comment = data["INDEX_COMMENT"].(string)
   249  	}
   250  	if GetStringValue(data["UNIQUENESS"]) == "UNIQUE" {
   251  		index.Type = "unique"
   252  	}
   253  	if data["TABLE_NAME"] != nil {
   254  		index.TableName = data["TABLE_NAME"].(string)
   255  	}
   256  	if data["TABLE_OWNER"] != nil {
   257  		index.OwnerName = data["TABLE_OWNER"].(string)
   258  	}
   259  	return
   260  }
   261  func (this_ *OracleDialect) IndexesSelectSql(ownerName string, tableName string) (sql string, err error) {
   262  	sql = `SELECT t.INDEX_NAME,t.COLUMN_NAME,t.TABLE_OWNER,t.TABLE_NAME,i.INDEX_TYPE,i.UNIQUENESS FROM ALL_IND_COLUMNS t,ALL_INDEXES i  `
   263  	sql += `WHERE t.INDEX_NAME = i.INDEX_NAME `
   264  	if ownerName != "" {
   265  		sql += `AND t.TABLE_OWNER='` + ownerName + `' `
   266  	}
   267  	sql += `AND t.TABLE_NAME='` + tableName + `' `
   268  	sql += `AND t.COLUMN_NAME NOT IN( `
   269  	sql += `SELECT cu.COLUMN_NAME FROM ALL_CONS_COLUMNS cu, ALL_CONSTRAINTS au `
   270  	sql += `WHERE cu.CONSTRAINT_NAME = au.CONSTRAINT_NAME and au.CONSTRAINT_TYPE = 'P' `
   271  	if ownerName != "" {
   272  		sql += `AND au.OWNER='` + ownerName + `' `
   273  	}
   274  	sql += `AND au.TABLE_NAME='` + tableName + `' `
   275  
   276  	sql += ") "
   277  	return
   278  }
   279  
   280  func (this_ *OracleDialect) InsertDataListSql(ownerName string, tableName string, columnList []*ColumnModel, dataList []map[string]interface{}) (sqlList []string, batchSqlList []string, err error) {
   281  	var batchSql = "INSERT ALL "
   282  	var columnNames []string
   283  	for _, one := range columnList {
   284  		columnNames = append(columnNames, one.Name)
   285  	}
   286  	for index, data := range dataList {
   287  		var columnList_ []string
   288  		var values = "("
   289  		for _, column := range columnList {
   290  			str := this_.PackValueForSql(column, data[column.Name])
   291  			if strings.EqualFold(str, "null") {
   292  				continue
   293  			}
   294  			columnList_ = append(columnList_, column.Name)
   295  			if column.Type == "TIMESTAMP" {
   296  				str = `TO_TIMESTAMP(` + str + `,'yyyy-MM-dd HH24:mi:ss.ff6')`
   297  			} else {
   298  				if column.NotNull {
   299  					if str == `''` {
   300  						str = `' '`
   301  					}
   302  				}
   303  				if len(str) > 1000 {
   304  					key := fmt.Sprintf("%s_%s_%s_%d", ownerName, tableName, column.Name, index)
   305  					batchSqlList = append(batchSqlList, `DECLARE `+key+` CLOB :=`+str)
   306  					str = `:` + key
   307  				}
   308  			}
   309  			values += str + ", "
   310  		}
   311  		values = strings.TrimSuffix(values, ", ")
   312  		values += ")"
   313  
   314  		insertSqlInfo := "INSERT INTO "
   315  		if ownerName != "" {
   316  			insertSqlInfo += this_.PackOwner(ownerName) + "."
   317  		}
   318  		insertSqlInfo += this_.PackTable(tableName)
   319  		insertSqlInfo += " ("
   320  		insertSqlInfo += this_.PackColumns(columnList_)
   321  		insertSqlInfo += ") VALUES "
   322  
   323  		sqlList = append(sqlList, insertSqlInfo+values)
   324  
   325  		batchOne := "INTO "
   326  		if ownerName != "" {
   327  			batchOne += this_.PackOwner(ownerName) + "."
   328  		}
   329  		batchOne += this_.PackTable(tableName)
   330  		batchOne += " ("
   331  		batchOne += this_.PackColumns(columnList_)
   332  		batchOne += ") VALUES " + values
   333  
   334  		batchSql += "\n" + batchOne
   335  	}
   336  	batchSql += "\n" + `SELECT 1 FROM DUAL`
   337  	batchSqlList = append(batchSqlList, batchSql)
   338  	return
   339  }