oracle_dialect.go 26 KB

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