You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dialect_sqlite3.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. "database/sql"
  7. "errors"
  8. "fmt"
  9. "regexp"
  10. "strings"
  11. "xorm.io/core"
  12. )
  13. var (
  14. sqlite3ReservedWords = map[string]bool{
  15. "ABORT": true,
  16. "ACTION": true,
  17. "ADD": true,
  18. "AFTER": true,
  19. "ALL": true,
  20. "ALTER": true,
  21. "ANALYZE": true,
  22. "AND": true,
  23. "AS": true,
  24. "ASC": true,
  25. "ATTACH": true,
  26. "AUTOINCREMENT": true,
  27. "BEFORE": true,
  28. "BEGIN": true,
  29. "BETWEEN": true,
  30. "BY": true,
  31. "CASCADE": true,
  32. "CASE": true,
  33. "CAST": true,
  34. "CHECK": true,
  35. "COLLATE": true,
  36. "COLUMN": true,
  37. "COMMIT": true,
  38. "CONFLICT": true,
  39. "CONSTRAINT": true,
  40. "CREATE": true,
  41. "CROSS": true,
  42. "CURRENT_DATE": true,
  43. "CURRENT_TIME": true,
  44. "CURRENT_TIMESTAMP": true,
  45. "DATABASE": true,
  46. "DEFAULT": true,
  47. "DEFERRABLE": true,
  48. "DEFERRED": true,
  49. "DELETE": true,
  50. "DESC": true,
  51. "DETACH": true,
  52. "DISTINCT": true,
  53. "DROP": true,
  54. "EACH": true,
  55. "ELSE": true,
  56. "END": true,
  57. "ESCAPE": true,
  58. "EXCEPT": true,
  59. "EXCLUSIVE": true,
  60. "EXISTS": true,
  61. "EXPLAIN": true,
  62. "FAIL": true,
  63. "FOR": true,
  64. "FOREIGN": true,
  65. "FROM": true,
  66. "FULL": true,
  67. "GLOB": true,
  68. "GROUP": true,
  69. "HAVING": true,
  70. "IF": true,
  71. "IGNORE": true,
  72. "IMMEDIATE": true,
  73. "IN": true,
  74. "INDEX": true,
  75. "INDEXED": true,
  76. "INITIALLY": true,
  77. "INNER": true,
  78. "INSERT": true,
  79. "INSTEAD": true,
  80. "INTERSECT": true,
  81. "INTO": true,
  82. "IS": true,
  83. "ISNULL": true,
  84. "JOIN": true,
  85. "KEY": true,
  86. "LEFT": true,
  87. "LIKE": true,
  88. "LIMIT": true,
  89. "MATCH": true,
  90. "NATURAL": true,
  91. "NO": true,
  92. "NOT": true,
  93. "NOTNULL": true,
  94. "NULL": true,
  95. "OF": true,
  96. "OFFSET": true,
  97. "ON": true,
  98. "OR": true,
  99. "ORDER": true,
  100. "OUTER": true,
  101. "PLAN": true,
  102. "PRAGMA": true,
  103. "PRIMARY": true,
  104. "QUERY": true,
  105. "RAISE": true,
  106. "RECURSIVE": true,
  107. "REFERENCES": true,
  108. "REGEXP": true,
  109. "REINDEX": true,
  110. "RELEASE": true,
  111. "RENAME": true,
  112. "REPLACE": true,
  113. "RESTRICT": true,
  114. "RIGHT": true,
  115. "ROLLBACK": true,
  116. "ROW": true,
  117. "SAVEPOINT": true,
  118. "SELECT": true,
  119. "SET": true,
  120. "TABLE": true,
  121. "TEMP": true,
  122. "TEMPORARY": true,
  123. "THEN": true,
  124. "TO": true,
  125. "TRANSACTI": true,
  126. "TRIGGER": true,
  127. "UNION": true,
  128. "UNIQUE": true,
  129. "UPDATE": true,
  130. "USING": true,
  131. "VACUUM": true,
  132. "VALUES": true,
  133. "VIEW": true,
  134. "VIRTUAL": true,
  135. "WHEN": true,
  136. "WHERE": true,
  137. "WITH": true,
  138. "WITHOUT": true,
  139. }
  140. )
  141. type sqlite3 struct {
  142. core.Base
  143. }
  144. func (db *sqlite3) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName string) error {
  145. return db.Base.Init(d, db, uri, drivername, dataSourceName)
  146. }
  147. func (db *sqlite3) SqlType(c *core.Column) string {
  148. switch t := c.SQLType.Name; t {
  149. case core.Bool:
  150. if c.Default == "true" {
  151. c.Default = "1"
  152. } else if c.Default == "false" {
  153. c.Default = "0"
  154. }
  155. return core.Integer
  156. case core.Date, core.DateTime, core.TimeStamp, core.Time:
  157. return core.DateTime
  158. case core.TimeStampz:
  159. return core.Text
  160. case core.Char, core.Varchar, core.NVarchar, core.TinyText,
  161. core.Text, core.MediumText, core.LongText, core.Json:
  162. return core.Text
  163. case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.BigInt:
  164. return core.Integer
  165. case core.Float, core.Double, core.Real:
  166. return core.Real
  167. case core.Decimal, core.Numeric:
  168. return core.Numeric
  169. case core.TinyBlob, core.Blob, core.MediumBlob, core.LongBlob, core.Bytea, core.Binary, core.VarBinary:
  170. return core.Blob
  171. case core.Serial, core.BigSerial:
  172. c.IsPrimaryKey = true
  173. c.IsAutoIncrement = true
  174. c.Nullable = false
  175. return core.Integer
  176. default:
  177. return t
  178. }
  179. }
  180. func (db *sqlite3) FormatBytes(bs []byte) string {
  181. return fmt.Sprintf("X'%x'", bs)
  182. }
  183. func (db *sqlite3) SupportInsertMany() bool {
  184. return true
  185. }
  186. func (db *sqlite3) IsReserved(name string) bool {
  187. _, ok := sqlite3ReservedWords[name]
  188. return ok
  189. }
  190. func (db *sqlite3) Quote(name string) string {
  191. return "`" + name + "`"
  192. }
  193. func (db *sqlite3) AutoIncrStr() string {
  194. return "AUTOINCREMENT"
  195. }
  196. func (db *sqlite3) SupportEngine() bool {
  197. return false
  198. }
  199. func (db *sqlite3) SupportCharset() bool {
  200. return false
  201. }
  202. func (db *sqlite3) IndexOnTable() bool {
  203. return false
  204. }
  205. func (db *sqlite3) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
  206. args := []interface{}{idxName}
  207. return "SELECT name FROM sqlite_master WHERE type='index' and name = ?", args
  208. }
  209. func (db *sqlite3) TableCheckSql(tableName string) (string, []interface{}) {
  210. args := []interface{}{tableName}
  211. return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
  212. }
  213. func (db *sqlite3) DropIndexSql(tableName string, index *core.Index) string {
  214. // var unique string
  215. quote := db.Quote
  216. idxName := index.Name
  217. if !strings.HasPrefix(idxName, "UQE_") &&
  218. !strings.HasPrefix(idxName, "IDX_") {
  219. if index.Type == core.UniqueType {
  220. idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
  221. } else {
  222. idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
  223. }
  224. }
  225. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  226. }
  227. func (db *sqlite3) ForUpdateSql(query string) string {
  228. return query
  229. }
  230. /*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
  231. args := []interface{}{tableName}
  232. sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
  233. return sql, args
  234. }*/
  235. func (db *sqlite3) IsColumnExist(tableName, colName string) (bool, error) {
  236. args := []interface{}{tableName}
  237. query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
  238. db.LogSQL(query, args)
  239. rows, err := db.DB().Query(query, args...)
  240. if err != nil {
  241. return false, err
  242. }
  243. defer rows.Close()
  244. if rows.Next() {
  245. return true, nil
  246. }
  247. return false, nil
  248. }
  249. func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
  250. args := []interface{}{tableName}
  251. s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
  252. db.LogSQL(s, args)
  253. rows, err := db.DB().Query(s, args...)
  254. if err != nil {
  255. return nil, nil, err
  256. }
  257. defer rows.Close()
  258. var name string
  259. for rows.Next() {
  260. err = rows.Scan(&name)
  261. if err != nil {
  262. return nil, nil, err
  263. }
  264. break
  265. }
  266. if name == "" {
  267. return nil, nil, errors.New("no table named " + tableName)
  268. }
  269. nStart := strings.Index(name, "(")
  270. nEnd := strings.LastIndex(name, ")")
  271. reg := regexp.MustCompile(`[^\(,\)]*(\([^\(]*\))?`)
  272. colCreates := reg.FindAllString(name[nStart+1:nEnd], -1)
  273. cols := make(map[string]*core.Column)
  274. colSeq := make([]string, 0)
  275. for _, colStr := range colCreates {
  276. reg = regexp.MustCompile(`,\s`)
  277. colStr = reg.ReplaceAllString(colStr, ",")
  278. if strings.HasPrefix(strings.TrimSpace(colStr), "PRIMARY KEY") {
  279. parts := strings.Split(strings.TrimSpace(colStr), "(")
  280. if len(parts) == 2 {
  281. pkCols := strings.Split(strings.TrimRight(strings.TrimSpace(parts[1]), ")"), ",")
  282. for _, pk := range pkCols {
  283. if col, ok := cols[strings.Trim(strings.TrimSpace(pk), "`")]; ok {
  284. col.IsPrimaryKey = true
  285. }
  286. }
  287. }
  288. continue
  289. }
  290. fields := strings.Fields(strings.TrimSpace(colStr))
  291. col := new(core.Column)
  292. col.Indexes = make(map[string]int)
  293. col.Nullable = true
  294. col.DefaultIsEmpty = true
  295. for idx, field := range fields {
  296. if idx == 0 {
  297. col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
  298. continue
  299. } else if idx == 1 {
  300. col.SQLType = core.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0}
  301. }
  302. switch field {
  303. case "PRIMARY":
  304. col.IsPrimaryKey = true
  305. case "AUTOINCREMENT":
  306. col.IsAutoIncrement = true
  307. case "NULL":
  308. if fields[idx-1] == "NOT" {
  309. col.Nullable = false
  310. } else {
  311. col.Nullable = true
  312. }
  313. case "DEFAULT":
  314. col.Default = fields[idx+1]
  315. col.DefaultIsEmpty = false
  316. }
  317. }
  318. if !col.SQLType.IsNumeric() && !col.DefaultIsEmpty {
  319. col.Default = "'" + col.Default + "'"
  320. }
  321. cols[col.Name] = col
  322. colSeq = append(colSeq, col.Name)
  323. }
  324. return colSeq, cols, nil
  325. }
  326. func (db *sqlite3) GetTables() ([]*core.Table, error) {
  327. args := []interface{}{}
  328. s := "SELECT name FROM sqlite_master WHERE type='table'"
  329. db.LogSQL(s, args)
  330. rows, err := db.DB().Query(s, args...)
  331. if err != nil {
  332. return nil, err
  333. }
  334. defer rows.Close()
  335. tables := make([]*core.Table, 0)
  336. for rows.Next() {
  337. table := core.NewEmptyTable()
  338. err = rows.Scan(&table.Name)
  339. if err != nil {
  340. return nil, err
  341. }
  342. if table.Name == "sqlite_sequence" {
  343. continue
  344. }
  345. tables = append(tables, table)
  346. }
  347. return tables, nil
  348. }
  349. func (db *sqlite3) GetIndexes(tableName string) (map[string]*core.Index, error) {
  350. args := []interface{}{tableName}
  351. s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?"
  352. db.LogSQL(s, args)
  353. rows, err := db.DB().Query(s, args...)
  354. if err != nil {
  355. return nil, err
  356. }
  357. defer rows.Close()
  358. indexes := make(map[string]*core.Index, 0)
  359. for rows.Next() {
  360. var tmpSQL sql.NullString
  361. err = rows.Scan(&tmpSQL)
  362. if err != nil {
  363. return nil, err
  364. }
  365. if !tmpSQL.Valid {
  366. continue
  367. }
  368. sql := tmpSQL.String
  369. index := new(core.Index)
  370. nNStart := strings.Index(sql, "INDEX")
  371. nNEnd := strings.Index(sql, "ON")
  372. if nNStart == -1 || nNEnd == -1 {
  373. continue
  374. }
  375. indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []")
  376. var isRegular bool
  377. if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
  378. index.Name = indexName[5+len(tableName):]
  379. isRegular = true
  380. } else {
  381. index.Name = indexName
  382. }
  383. if strings.HasPrefix(sql, "CREATE UNIQUE INDEX") {
  384. index.Type = core.UniqueType
  385. } else {
  386. index.Type = core.IndexType
  387. }
  388. nStart := strings.Index(sql, "(")
  389. nEnd := strings.Index(sql, ")")
  390. colIndexes := strings.Split(sql[nStart+1:nEnd], ",")
  391. index.Cols = make([]string, 0)
  392. for _, col := range colIndexes {
  393. index.Cols = append(index.Cols, strings.Trim(col, "` []"))
  394. }
  395. index.IsRegular = isRegular
  396. indexes[index.Name] = index
  397. }
  398. return indexes, nil
  399. }
  400. func (db *sqlite3) Filters() []core.Filter {
  401. return []core.Filter{&core.IdFilter{}}
  402. }
  403. type sqlite3Driver struct {
  404. }
  405. func (p *sqlite3Driver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  406. if strings.Contains(dataSourceName, "?") {
  407. dataSourceName = dataSourceName[:strings.Index(dataSourceName, "?")]
  408. }
  409. return &core.Uri{DbType: core.SQLITE, DbName: dataSourceName}, nil
  410. }