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