github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/xorm/oracle_dialect.go (about)

     1  // Copyright 2015 The Xorm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package xorm
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"github.com/insionng/yougam/libraries/go-xorm/core"
    13  )
    14  
    15  var (
    16  	oracleReservedWords = map[string]bool{
    17  		"ACCESS":                    true,
    18  		"ACCOUNT":                   true,
    19  		"ACTIVATE":                  true,
    20  		"ADD":                       true,
    21  		"ADMIN":                     true,
    22  		"ADVISE":                    true,
    23  		"AFTER":                     true,
    24  		"ALL":                       true,
    25  		"ALL_ROWS":                  true,
    26  		"ALLOCATE":                  true,
    27  		"ALTER":                     true,
    28  		"ANALYZE":                   true,
    29  		"AND":                       true,
    30  		"ANY":                       true,
    31  		"ARCHIVE":                   true,
    32  		"ARCHIVELOG":                true,
    33  		"ARRAY":                     true,
    34  		"AS":                        true,
    35  		"ASC":                       true,
    36  		"AT":                        true,
    37  		"AUDIT":                     true,
    38  		"AUTHENTICATED":             true,
    39  		"AUTHORIZATION":             true,
    40  		"AUTOEXTEND":                true,
    41  		"AUTOMATIC":                 true,
    42  		"BACKUP":                    true,
    43  		"BECOME":                    true,
    44  		"BEFORE":                    true,
    45  		"BEGIN":                     true,
    46  		"BETWEEN":                   true,
    47  		"BFILE":                     true,
    48  		"BITMAP":                    true,
    49  		"BLOB":                      true,
    50  		"BLOCK":                     true,
    51  		"BODY":                      true,
    52  		"BY":                        true,
    53  		"CACHE":                     true,
    54  		"CACHE_INSTANCES":           true,
    55  		"CANCEL":                    true,
    56  		"CASCADE":                   true,
    57  		"CAST":                      true,
    58  		"CFILE":                     true,
    59  		"CHAINED":                   true,
    60  		"CHANGE":                    true,
    61  		"CHAR":                      true,
    62  		"CHAR_CS":                   true,
    63  		"CHARACTER":                 true,
    64  		"CHECK":                     true,
    65  		"CHECKPOINT":                true,
    66  		"CHOOSE":                    true,
    67  		"CHUNK":                     true,
    68  		"CLEAR":                     true,
    69  		"CLOB":                      true,
    70  		"CLONE":                     true,
    71  		"CLOSE":                     true,
    72  		"CLOSE_CACHED_OPEN_CURSORS": true,
    73  		"CLUSTER":                   true,
    74  		"COALESCE":                  true,
    75  		"COLUMN":                    true,
    76  		"COLUMNS":                   true,
    77  		"COMMENT":                   true,
    78  		"COMMIT":                    true,
    79  		"COMMITTED":                 true,
    80  		"COMPATIBILITY":             true,
    81  		"COMPILE":                   true,
    82  		"COMPLETE":                  true,
    83  		"COMPOSITE_LIMIT":           true,
    84  		"COMPRESS":                  true,
    85  		"COMPUTE":                   true,
    86  		"CONNECT":                   true,
    87  		"CONNECT_TIME":              true,
    88  		"CONSTRAINT":                true,
    89  		"CONSTRAINTS":               true,
    90  		"CONTENTS":                  true,
    91  		"CONTINUE":                  true,
    92  		"CONTROLFILE":               true,
    93  		"CONVERT":                   true,
    94  		"COST":                      true,
    95  		"CPU_PER_CALL":              true,
    96  		"CPU_PER_SESSION":           true,
    97  		"CREATE":                    true,
    98  		"CURRENT":                   true,
    99  		"CURRENT_SCHEMA":            true,
   100  		"CURREN_USER":               true,
   101  		"CURSOR":                    true,
   102  		"CYCLE":                     true,
   103  		"DANGLING":                  true,
   104  		"DATABASE":                  true,
   105  		"DATAFILE":                  true,
   106  		"DATAFILES":                 true,
   107  		"DATAOBJNO":                 true,
   108  		"DATE":                      true,
   109  		"DBA":                       true,
   110  		"DBHIGH":                    true,
   111  		"DBLOW":                     true,
   112  		"DBMAC":                     true,
   113  		"DEALLOCATE":                true,
   114  		"DEBUG":                     true,
   115  		"DEC":                       true,
   116  		"DECIMAL":                   true,
   117  		"DECLARE":                   true,
   118  		"DEFAULT":                   true,
   119  		"DEFERRABLE":                true,
   120  		"DEFERRED":                  true,
   121  		"DEGREE":                    true,
   122  		"DELETE":                    true,
   123  		"DEREF":                     true,
   124  		"DESC":                      true,
   125  		"DIRECTORY":                 true,
   126  		"DISABLE":                   true,
   127  		"DISCONNECT":                true,
   128  		"DISMOUNT":                  true,
   129  		"DISTINCT":                  true,
   130  		"DISTRIBUTED":               true,
   131  		"DML":                       true,
   132  		"DOUBLE":                    true,
   133  		"DROP":                      true,
   134  		"DUMP":                      true,
   135  		"EACH":                      true,
   136  		"ELSE":                      true,
   137  		"ENABLE":                    true,
   138  		"END":                       true,
   139  		"ENFORCE":                   true,
   140  		"ENTRY":                     true,
   141  		"ESCAPE":                    true,
   142  		"EXCEPT":                    true,
   143  		"EXCEPTIONS":                true,
   144  		"EXCHANGE":                  true,
   145  		"EXCLUDING":                 true,
   146  		"EXCLUSIVE":                 true,
   147  		"EXECUTE":                   true,
   148  		"EXISTS":                    true,
   149  		"EXPIRE":                    true,
   150  		"EXPLAIN":                   true,
   151  		"EXTENT":                    true,
   152  		"EXTENTS":                   true,
   153  		"EXTERNALLY":                true,
   154  		"FAILED_LOGIN_ATTEMPTS":     true,
   155  		"FALSE":                     true,
   156  		"FAST":                      true,
   157  		"FILE":                      true,
   158  		"FIRST_ROWS":                true,
   159  		"FLAGGER":                   true,
   160  		"FLOAT":                     true,
   161  		"FLOB":                      true,
   162  		"FLUSH":                     true,
   163  		"FOR":                       true,
   164  		"FORCE":                     true,
   165  		"FOREIGN":                   true,
   166  		"FREELIST":                  true,
   167  		"FREELISTS":                 true,
   168  		"FROM":                      true,
   169  		"FULL":                      true,
   170  		"FUNCTION":                  true,
   171  		"GLOBAL":                    true,
   172  		"GLOBALLY":                  true,
   173  		"GLOBAL_NAME":               true,
   174  		"GRANT":                     true,
   175  		"GROUP":                     true,
   176  		"GROUPS":                    true,
   177  		"HASH":                      true,
   178  		"HASHKEYS":                  true,
   179  		"HAVING":                    true,
   180  		"HEADER":                    true,
   181  		"HEAP":                      true,
   182  		"IDENTIFIED":                true,
   183  		"IDGENERATORS":              true,
   184  		"IDLE_TIME":                 true,
   185  		"IF":                        true,
   186  		"IMMEDIATE":                 true,
   187  		"IN":                        true,
   188  		"INCLUDING":                 true,
   189  		"INCREMENT":                 true,
   190  		"INDEX":                     true,
   191  		"INDEXED":                   true,
   192  		"INDEXES":                   true,
   193  		"INDICATOR":                 true,
   194  		"IND_PARTITION":             true,
   195  		"INITIAL":                   true,
   196  		"INITIALLY":                 true,
   197  		"INITRANS":                  true,
   198  		"INSERT":                    true,
   199  		"INSTANCE":                  true,
   200  		"INSTANCES":                 true,
   201  		"INSTEAD":                   true,
   202  		"INT":                       true,
   203  		"INTEGER":                   true,
   204  		"INTERMEDIATE":              true,
   205  		"INTERSECT":                 true,
   206  		"INTO":                      true,
   207  		"IS":                        true,
   208  		"ISOLATION":                 true,
   209  		"ISOLATION_LEVEL":           true,
   210  		"KEEP":                      true,
   211  		"KEY":                       true,
   212  		"KILL":                      true,
   213  		"LABEL":                     true,
   214  		"LAYER":                     true,
   215  		"LESS":                      true,
   216  		"LEVEL":                     true,
   217  		"LIBRARY":                   true,
   218  		"LIKE":                      true,
   219  		"LIMIT":                     true,
   220  		"LINK":                      true,
   221  		"LIST":                      true,
   222  		"LOB":                       true,
   223  		"LOCAL":                     true,
   224  		"LOCK":                      true,
   225  		"LOCKED":                    true,
   226  		"LOG":                       true,
   227  		"LOGFILE":                   true,
   228  		"LOGGING":                   true,
   229  		"LOGICAL_READS_PER_CALL":    true,
   230  		"LOGICAL_READS_PER_SESSION": true,
   231  		"LONG":                     true,
   232  		"MANAGE":                   true,
   233  		"MASTER":                   true,
   234  		"MAX":                      true,
   235  		"MAXARCHLOGS":              true,
   236  		"MAXDATAFILES":             true,
   237  		"MAXEXTENTS":               true,
   238  		"MAXINSTANCES":             true,
   239  		"MAXLOGFILES":              true,
   240  		"MAXLOGHISTORY":            true,
   241  		"MAXLOGMEMBERS":            true,
   242  		"MAXSIZE":                  true,
   243  		"MAXTRANS":                 true,
   244  		"MAXVALUE":                 true,
   245  		"MIN":                      true,
   246  		"MEMBER":                   true,
   247  		"MINIMUM":                  true,
   248  		"MINEXTENTS":               true,
   249  		"MINUS":                    true,
   250  		"MINVALUE":                 true,
   251  		"MLSLABEL":                 true,
   252  		"MLS_LABEL_FORMAT":         true,
   253  		"MODE":                     true,
   254  		"MODIFY":                   true,
   255  		"MOUNT":                    true,
   256  		"MOVE":                     true,
   257  		"MTS_DISPATCHERS":          true,
   258  		"MULTISET":                 true,
   259  		"NATIONAL":                 true,
   260  		"NCHAR":                    true,
   261  		"NCHAR_CS":                 true,
   262  		"NCLOB":                    true,
   263  		"NEEDED":                   true,
   264  		"NESTED":                   true,
   265  		"NETWORK":                  true,
   266  		"NEW":                      true,
   267  		"NEXT":                     true,
   268  		"NOARCHIVELOG":             true,
   269  		"NOAUDIT":                  true,
   270  		"NOCACHE":                  true,
   271  		"NOCOMPRESS":               true,
   272  		"NOCYCLE":                  true,
   273  		"NOFORCE":                  true,
   274  		"NOLOGGING":                true,
   275  		"NOMAXVALUE":               true,
   276  		"NOMINVALUE":               true,
   277  		"NONE":                     true,
   278  		"NOORDER":                  true,
   279  		"NOOVERRIDE":               true,
   280  		"NOPARALLEL":               true,
   281  		"NOREVERSE":                true,
   282  		"NORMAL":                   true,
   283  		"NOSORT":                   true,
   284  		"NOT":                      true,
   285  		"NOTHING":                  true,
   286  		"NOWAIT":                   true,
   287  		"NULL":                     true,
   288  		"NUMBER":                   true,
   289  		"NUMERIC":                  true,
   290  		"NVARCHAR2":                true,
   291  		"OBJECT":                   true,
   292  		"OBJNO":                    true,
   293  		"OBJNO_REUSE":              true,
   294  		"OF":                       true,
   295  		"OFF":                      true,
   296  		"OFFLINE":                  true,
   297  		"OID":                      true,
   298  		"OIDINDEX":                 true,
   299  		"OLD":                      true,
   300  		"ON":                       true,
   301  		"ONLINE":                   true,
   302  		"ONLY":                     true,
   303  		"OPCODE":                   true,
   304  		"OPEN":                     true,
   305  		"OPTIMAL":                  true,
   306  		"OPTIMIZER_GOAL":           true,
   307  		"OPTION":                   true,
   308  		"OR":                       true,
   309  		"ORDER":                    true,
   310  		"ORGANIZATION":             true,
   311  		"OSLABEL":                  true,
   312  		"OVERFLOW":                 true,
   313  		"OWN":                      true,
   314  		"PACKAGE":                  true,
   315  		"PARALLEL":                 true,
   316  		"PARTITION":                true,
   317  		"PASSWORD":                 true,
   318  		"PASSWORD_GRACE_TIME":      true,
   319  		"PASSWORD_LIFE_TIME":       true,
   320  		"PASSWORD_LOCK_TIME":       true,
   321  		"PASSWORD_REUSE_MAX":       true,
   322  		"PASSWORD_REUSE_TIME":      true,
   323  		"PASSWORD_VERIFY_FUNCTION": true,
   324  		"PCTFREE":                  true,
   325  		"PCTINCREASE":              true,
   326  		"PCTTHRESHOLD":             true,
   327  		"PCTUSED":                  true,
   328  		"PCTVERSION":               true,
   329  		"PERCENT":                  true,
   330  		"PERMANENT":                true,
   331  		"PLAN":                     true,
   332  		"PLSQL_DEBUG":              true,
   333  		"POST_TRANSACTION":         true,
   334  		"PRECISION":                true,
   335  		"PRESERVE":                 true,
   336  		"PRIMARY":                  true,
   337  		"PRIOR":                    true,
   338  		"PRIVATE":                  true,
   339  		"PRIVATE_SGA":              true,
   340  		"PRIVILEGE":                true,
   341  		"PRIVILEGES":               true,
   342  		"PROCEDURE":                true,
   343  		"PROFILE":                  true,
   344  		"PUBLIC":                   true,
   345  		"PURGE":                    true,
   346  		"QUEUE":                    true,
   347  		"QUOTA":                    true,
   348  		"RANGE":                    true,
   349  		"RAW":                      true,
   350  		"RBA":                      true,
   351  		"READ":                     true,
   352  		"READUP":                   true,
   353  		"REAL":                     true,
   354  		"REBUILD":                  true,
   355  		"RECOVER":                  true,
   356  		"RECOVERABLE":              true,
   357  		"RECOVERY":                 true,
   358  		"REF":                      true,
   359  		"REFERENCES":               true,
   360  		"REFERENCING":              true,
   361  		"REFRESH":                  true,
   362  		"RENAME":                   true,
   363  		"REPLACE":                  true,
   364  		"RESET":                    true,
   365  		"RESETLOGS":                true,
   366  		"RESIZE":                   true,
   367  		"RESOURCE":                 true,
   368  		"RESTRICTED":               true,
   369  		"RETURN":                   true,
   370  		"RETURNING":                true,
   371  		"REUSE":                    true,
   372  		"REVERSE":                  true,
   373  		"REVOKE":                   true,
   374  		"ROLE":                     true,
   375  		"ROLES":                    true,
   376  		"ROLLBACK":                 true,
   377  		"ROW":                      true,
   378  		"ROWID":                    true,
   379  		"ROWNUM":                   true,
   380  		"ROWS":                     true,
   381  		"RULE":                     true,
   382  		"SAMPLE":                   true,
   383  		"SAVEPOINT":                true,
   384  		"SB4":                      true,
   385  		"SCAN_INSTANCES":           true,
   386  		"SCHEMA":                   true,
   387  		"SCN":                      true,
   388  		"SCOPE":                    true,
   389  		"SD_ALL":                   true,
   390  		"SD_INHIBIT":               true,
   391  		"SD_SHOW":                  true,
   392  		"SEGMENT":                  true,
   393  		"SEG_BLOCK":                true,
   394  		"SEG_FILE":                 true,
   395  		"SELECT":                   true,
   396  		"SEQUENCE":                 true,
   397  		"SERIALIZABLE":             true,
   398  		"SESSION":                  true,
   399  		"SESSION_CACHED_CURSORS":   true,
   400  		"SESSIONS_PER_USER":        true,
   401  		"SET":                      true,
   402  		"SHARE":                    true,
   403  		"SHARED":                   true,
   404  		"SHARED_POOL":              true,
   405  		"SHRINK":                   true,
   406  		"SIZE":                     true,
   407  		"SKIP":                     true,
   408  		"SKIP_UNUSABLE_INDEXES":    true,
   409  		"SMALLINT":                 true,
   410  		"SNAPSHOT":                 true,
   411  		"SOME":                     true,
   412  		"SORT":                     true,
   413  		"SPECIFICATION":            true,
   414  		"SPLIT":                    true,
   415  		"SQL_TRACE":                true,
   416  		"STANDBY":                  true,
   417  		"START":                    true,
   418  		"STATEMENT_ID":             true,
   419  		"STATISTICS":               true,
   420  		"STOP":                     true,
   421  		"STORAGE":                  true,
   422  		"STORE":                    true,
   423  		"STRUCTURE":                true,
   424  		"SUCCESSFUL":               true,
   425  		"SWITCH":                   true,
   426  		"SYS_OP_ENFORCE_NOT_NULL$": true,
   427  		"SYS_OP_NTCIMG$":           true,
   428  		"SYNONYM":                  true,
   429  		"SYSDATE":                  true,
   430  		"SYSDBA":                   true,
   431  		"SYSOPER":                  true,
   432  		"SYSTEM":                   true,
   433  		"TABLE":                    true,
   434  		"TABLES":                   true,
   435  		"TABLESPACE":               true,
   436  		"TABLESPACE_NO":            true,
   437  		"TABNO":                    true,
   438  		"TEMPORARY":                true,
   439  		"THAN":                     true,
   440  		"THE":                      true,
   441  		"THEN":                     true,
   442  		"THREAD":                   true,
   443  		"TIMESTAMP":                true,
   444  		"TIME":                     true,
   445  		"TO":                       true,
   446  		"TOPLEVEL":                 true,
   447  		"TRACE":                    true,
   448  		"TRACING":                  true,
   449  		"TRANSACTION":              true,
   450  		"TRANSITIONAL":             true,
   451  		"TRIGGER":                  true,
   452  		"TRIGGERS":                 true,
   453  		"TRUE":                     true,
   454  		"TRUNCATE":                 true,
   455  		"TX":                       true,
   456  		"TYPE":                     true,
   457  		"UB2":                      true,
   458  		"UBA":                      true,
   459  		"UID":                      true,
   460  		"UNARCHIVED":               true,
   461  		"UNDO":                     true,
   462  		"UNION":                    true,
   463  		"UNIQUE":                   true,
   464  		"UNLIMITED":                true,
   465  		"UNLOCK":                   true,
   466  		"UNRECOVERABLE":            true,
   467  		"UNTIL":                    true,
   468  		"UNUSABLE":                 true,
   469  		"UNUSED":                   true,
   470  		"UPDATABLE":                true,
   471  		"UPDATE":                   true,
   472  		"USAGE":                    true,
   473  		"USE":                      true,
   474  		"USER":                     true,
   475  		"USING":                    true,
   476  		"VALIDATE":                 true,
   477  		"VALIDATION":               true,
   478  		"VALUE":                    true,
   479  		"VALUES":                   true,
   480  		"VARCHAR":                  true,
   481  		"VARCHAR2":                 true,
   482  		"VARYING":                  true,
   483  		"VIEW":                     true,
   484  		"WHEN":                     true,
   485  		"WHENEVER":                 true,
   486  		"WHERE":                    true,
   487  		"WITH":                     true,
   488  		"WITHOUT":                  true,
   489  		"WORK":                     true,
   490  		"WRITE":                    true,
   491  		"WRITEDOWN":                true,
   492  		"WRITEUP":                  true,
   493  		"XID":                      true,
   494  		"YEAR":                     true,
   495  		"ZONE":                     true,
   496  	}
   497  )
   498  
   499  type oracle struct {
   500  	core.Base
   501  }
   502  
   503  func (db *oracle) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName string) error {
   504  	return db.Base.Init(d, db, uri, drivername, dataSourceName)
   505  }
   506  
   507  func (db *oracle) SqlType(c *core.Column) string {
   508  	var res string
   509  	switch t := c.SQLType.Name; t {
   510  	case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.BigInt, core.Bool, core.Serial, core.BigSerial:
   511  		res = "NUMBER"
   512  	case core.Binary, core.VarBinary, core.Blob, core.TinyBlob, core.MediumBlob, core.LongBlob, core.Bytea:
   513  		return core.Blob
   514  	case core.Time, core.DateTime, core.TimeStamp:
   515  		res = core.TimeStamp
   516  	case core.TimeStampz:
   517  		res = "TIMESTAMP WITH TIME ZONE"
   518  	case core.Float, core.Double, core.Numeric, core.Decimal:
   519  		res = "NUMBER"
   520  	case core.Text, core.MediumText, core.LongText, core.Json:
   521  		res = "CLOB"
   522  	case core.Char, core.Varchar, core.TinyText:
   523  		res = "VARCHAR2"
   524  	default:
   525  		res = t
   526  	}
   527  
   528  	hasLen1 := (c.Length > 0)
   529  	hasLen2 := (c.Length2 > 0)
   530  
   531  	if hasLen2 {
   532  		res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
   533  	} else if hasLen1 {
   534  		res += "(" + strconv.Itoa(c.Length) + ")"
   535  	}
   536  	return res
   537  }
   538  
   539  func (db *oracle) AutoIncrStr() string {
   540  	return "AUTO_INCREMENT"
   541  }
   542  
   543  func (db *oracle) SupportInsertMany() bool {
   544  	return true
   545  }
   546  
   547  func (db *oracle) IsReserved(name string) bool {
   548  	_, ok := oracleReservedWords[name]
   549  	return ok
   550  }
   551  
   552  func (db *oracle) Quote(name string) string {
   553  	return "\"" + name + "\""
   554  }
   555  
   556  func (db *oracle) QuoteStr() string {
   557  	return "\""
   558  }
   559  
   560  func (db *oracle) SupportEngine() bool {
   561  	return false
   562  }
   563  
   564  func (db *oracle) SupportCharset() bool {
   565  	return false
   566  }
   567  
   568  func (db *oracle) SupportDropIfExists() bool {
   569  	return false
   570  }
   571  
   572  func (db *oracle) IndexOnTable() bool {
   573  	return false
   574  }
   575  
   576  func (db *oracle) DropTableSql(tableName string) string {
   577  	return fmt.Sprintf("DROP TABLE `%s`", tableName)
   578  }
   579  
   580  func (db *oracle) CreateTableSql(table *core.Table, tableName, storeEngine, charset string) string {
   581  	var sql string
   582  	sql = "CREATE TABLE "
   583  	if tableName == "" {
   584  		tableName = table.Name
   585  	}
   586  
   587  	sql += db.Quote(tableName) + " ("
   588  
   589  	pkList := table.PrimaryKeys
   590  
   591  	for _, colName := range table.ColumnsSeq() {
   592  		col := table.GetColumn(colName)
   593  		/*if col.IsPrimaryKey && len(pkList) == 1 {
   594  			sql += col.String(b.dialect)
   595  		} else {*/
   596  		sql += col.StringNoPk(db)
   597  		//}
   598  		sql = strings.TrimSpace(sql)
   599  		sql += ", "
   600  	}
   601  
   602  	if len(pkList) > 0 {
   603  		sql += "PRIMARY KEY ( "
   604  		sql += db.Quote(strings.Join(pkList, db.Quote(",")))
   605  		sql += " ), "
   606  	}
   607  
   608  	sql = sql[:len(sql)-2] + ")"
   609  	if db.SupportEngine() && storeEngine != "" {
   610  		sql += " ENGINE=" + storeEngine
   611  	}
   612  	if db.SupportCharset() {
   613  		if len(charset) == 0 {
   614  			charset = db.URI().Charset
   615  		}
   616  		if len(charset) > 0 {
   617  			sql += " DEFAULT CHARSET " + charset
   618  		}
   619  	}
   620  	return sql
   621  }
   622  
   623  func (db *oracle) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
   624  	args := []interface{}{tableName, idxName}
   625  	return `SELECT INDEX_NAME FROM USER_INDEXES ` +
   626  		`WHERE TABLE_NAME = :1 AND INDEX_NAME = :2`, args
   627  }
   628  
   629  func (db *oracle) TableCheckSql(tableName string) (string, []interface{}) {
   630  	args := []interface{}{tableName}
   631  	return `SELECT table_name FROM user_tables WHERE table_name = :1`, args
   632  }
   633  
   634  func (db *oracle) MustDropTable(tableName string) error {
   635  	sql, args := db.TableCheckSql(tableName)
   636  	db.LogSQL(sql, args)
   637  
   638  	rows, err := db.DB().Query(sql, args...)
   639  	if err != nil {
   640  		return err
   641  	}
   642  	defer rows.Close()
   643  
   644  	if !rows.Next() {
   645  		return nil
   646  	}
   647  
   648  	sql = "Drop Table \"" + tableName + "\""
   649  	db.LogSQL(sql, args)
   650  
   651  	_, err = db.DB().Exec(sql)
   652  	return err
   653  }
   654  
   655  /*func (db *oracle) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
   656  	args := []interface{}{strings.ToUpper(tableName), strings.ToUpper(colName)}
   657  	return "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = ?" +
   658  		" AND column_name = ?", args
   659  }*/
   660  
   661  func (db *oracle) IsColumnExist(tableName, colName string) (bool, error) {
   662  	args := []interface{}{tableName, colName}
   663  	query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = :1" +
   664  		" AND column_name = :2"
   665  	db.LogSQL(query, args)
   666  
   667  	rows, err := db.DB().Query(query, args...)
   668  	if err != nil {
   669  		return false, err
   670  	}
   671  	defer rows.Close()
   672  
   673  	if rows.Next() {
   674  		return true, nil
   675  	}
   676  	return false, nil
   677  }
   678  
   679  func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
   680  	args := []interface{}{tableName}
   681  	s := "SELECT column_name,data_default,data_type,data_length,data_precision,data_scale," +
   682  		"nullable FROM USER_TAB_COLUMNS WHERE table_name = :1"
   683  	db.LogSQL(s, args)
   684  
   685  	rows, err := db.DB().Query(s, args...)
   686  	if err != nil {
   687  		return nil, nil, err
   688  	}
   689  	defer rows.Close()
   690  
   691  	cols := make(map[string]*core.Column)
   692  	colSeq := make([]string, 0)
   693  	for rows.Next() {
   694  		col := new(core.Column)
   695  		col.Indexes = make(map[string]int)
   696  
   697  		var colName, colDefault, nullable, dataType, dataPrecision, dataScale *string
   698  		var dataLen int
   699  
   700  		err = rows.Scan(&colName, &colDefault, &dataType, &dataLen, &dataPrecision,
   701  			&dataScale, &nullable)
   702  		if err != nil {
   703  			return nil, nil, err
   704  		}
   705  
   706  		col.Name = strings.Trim(*colName, `" `)
   707  		if colDefault != nil {
   708  			col.Default = *colDefault
   709  			col.DefaultIsEmpty = false
   710  		}
   711  
   712  		if *nullable == "Y" {
   713  			col.Nullable = true
   714  		} else {
   715  			col.Nullable = false
   716  		}
   717  
   718  		var ignore bool
   719  
   720  		var dt string
   721  		var len1, len2 int
   722  		dts := strings.Split(*dataType, "(")
   723  		dt = dts[0]
   724  		if len(dts) > 1 {
   725  			lens := strings.Split(dts[1][:len(dts[1])-1], ",")
   726  			if len(lens) > 1 {
   727  				len1, _ = strconv.Atoi(lens[0])
   728  				len2, _ = strconv.Atoi(lens[1])
   729  			} else {
   730  				len1, _ = strconv.Atoi(lens[0])
   731  			}
   732  		}
   733  
   734  		switch dt {
   735  		case "VARCHAR2":
   736  			col.SQLType = core.SQLType{Name: core.Varchar, DefaultLength: len1, DefaultLength2: len2}
   737  		case "NVARCHAR2":
   738  			col.SQLType = core.SQLType{Name: core.NVarchar, DefaultLength: len1, DefaultLength2: len2}
   739  		case "TIMESTAMP WITH TIME ZONE":
   740  			col.SQLType = core.SQLType{Name: core.TimeStampz, DefaultLength: 0, DefaultLength2: 0}
   741  		case "NUMBER":
   742  			col.SQLType = core.SQLType{Name: core.Double, DefaultLength: len1, DefaultLength2: len2}
   743  		case "LONG", "LONG RAW":
   744  			col.SQLType = core.SQLType{Name: core.Text, DefaultLength: 0, DefaultLength2: 0}
   745  		case "RAW":
   746  			col.SQLType = core.SQLType{Name: core.Binary, DefaultLength: 0, DefaultLength2: 0}
   747  		case "ROWID":
   748  			col.SQLType = core.SQLType{Name: core.Varchar, DefaultLength: 18, DefaultLength2: 0}
   749  		case "AQ$_SUBSCRIBERS":
   750  			ignore = true
   751  		default:
   752  			col.SQLType = core.SQLType{Name: strings.ToUpper(dt), DefaultLength: len1, DefaultLength2: len2}
   753  		}
   754  
   755  		if ignore {
   756  			continue
   757  		}
   758  
   759  		if _, ok := core.SqlTypes[col.SQLType.Name]; !ok {
   760  			return nil, nil, fmt.Errorf("Unknown colType %v %v", *dataType, col.SQLType)
   761  		}
   762  
   763  		col.Length = dataLen
   764  
   765  		if col.SQLType.IsText() || col.SQLType.IsTime() {
   766  			if !col.DefaultIsEmpty {
   767  				col.Default = "'" + col.Default + "'"
   768  			}
   769  		}
   770  		cols[col.Name] = col
   771  		colSeq = append(colSeq, col.Name)
   772  	}
   773  
   774  	return colSeq, cols, nil
   775  }
   776  
   777  func (db *oracle) GetTables() ([]*core.Table, error) {
   778  	args := []interface{}{}
   779  	s := "SELECT table_name FROM user_tables"
   780  	db.LogSQL(s, args)
   781  
   782  	rows, err := db.DB().Query(s, args...)
   783  	if err != nil {
   784  		return nil, err
   785  	}
   786  	defer rows.Close()
   787  
   788  	tables := make([]*core.Table, 0)
   789  	for rows.Next() {
   790  		table := core.NewEmptyTable()
   791  		err = rows.Scan(&table.Name)
   792  		if err != nil {
   793  			return nil, err
   794  		}
   795  
   796  		tables = append(tables, table)
   797  	}
   798  	return tables, nil
   799  }
   800  
   801  func (db *oracle) GetIndexes(tableName string) (map[string]*core.Index, error) {
   802  	args := []interface{}{tableName}
   803  	s := "SELECT t.column_name,i.uniqueness,i.index_name FROM user_ind_columns t,user_indexes i " +
   804  		"WHERE t.index_name = i.index_name and t.table_name = i.table_name and t.table_name =:1"
   805  	db.LogSQL(s, args)
   806  
   807  	rows, err := db.DB().Query(s, args...)
   808  	if err != nil {
   809  		return nil, err
   810  	}
   811  	defer rows.Close()
   812  
   813  	indexes := make(map[string]*core.Index, 0)
   814  	for rows.Next() {
   815  		var indexType int
   816  		var indexName, colName, uniqueness string
   817  
   818  		err = rows.Scan(&colName, &uniqueness, &indexName)
   819  		if err != nil {
   820  			return nil, err
   821  		}
   822  
   823  		indexName = strings.Trim(indexName, `" `)
   824  
   825  		if uniqueness == "UNIQUE" {
   826  			indexType = core.UniqueType
   827  		} else {
   828  			indexType = core.IndexType
   829  		}
   830  
   831  		var index *core.Index
   832  		var ok bool
   833  		if index, ok = indexes[indexName]; !ok {
   834  			index = new(core.Index)
   835  			index.Type = indexType
   836  			index.Name = indexName
   837  			indexes[indexName] = index
   838  		}
   839  		index.AddColumn(colName)
   840  	}
   841  	return indexes, nil
   842  }
   843  
   844  func (db *oracle) Filters() []core.Filter {
   845  	return []core.Filter{&core.QuoteFilter{}, &core.SeqFilter{Prefix: ":", Start: 1}, &core.IdFilter{}}
   846  }