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.

sqlite3.go 13KB

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