dialect_oracle.go 29 KB

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