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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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{'`', '`', schemas.AlwaysReserve}
  143. )
  144. type sqlite3 struct {
  145. Base
  146. }
  147. func (db *sqlite3) Init(uri *URI) error {
  148. db.quoter = sqlite3Quoter
  149. return db.Base.Init(db, uri)
  150. }
  151. func (db *sqlite3) SetQuotePolicy(quotePolicy QuotePolicy) {
  152. switch quotePolicy {
  153. case QuotePolicyNone:
  154. var q = sqlite3Quoter
  155. q.IsReserved = schemas.AlwaysNoReserve
  156. db.quoter = q
  157. case QuotePolicyReserved:
  158. var q = sqlite3Quoter
  159. q.IsReserved = db.IsReserved
  160. db.quoter = q
  161. case QuotePolicyAlways:
  162. fallthrough
  163. default:
  164. db.quoter = sqlite3Quoter
  165. }
  166. }
  167. func (db *sqlite3) SQLType(c *schemas.Column) string {
  168. switch t := c.SQLType.Name; t {
  169. case schemas.Bool:
  170. if c.Default == "true" {
  171. c.Default = "1"
  172. } else if c.Default == "false" {
  173. c.Default = "0"
  174. }
  175. return schemas.Integer
  176. case schemas.Date, schemas.DateTime, schemas.TimeStamp, schemas.Time:
  177. return schemas.DateTime
  178. case schemas.TimeStampz:
  179. return schemas.Text
  180. case schemas.Char, schemas.Varchar, schemas.NVarchar, schemas.TinyText,
  181. schemas.Text, schemas.MediumText, schemas.LongText, schemas.Json:
  182. return schemas.Text
  183. case schemas.Bit, schemas.TinyInt, schemas.SmallInt, schemas.MediumInt, schemas.Int, schemas.Integer, schemas.BigInt:
  184. return schemas.Integer
  185. case schemas.Float, schemas.Double, schemas.Real:
  186. return schemas.Real
  187. case schemas.Decimal, schemas.Numeric:
  188. return schemas.Numeric
  189. case schemas.TinyBlob, schemas.Blob, schemas.MediumBlob, schemas.LongBlob, schemas.Bytea, schemas.Binary, schemas.VarBinary:
  190. return schemas.Blob
  191. case schemas.Serial, schemas.BigSerial:
  192. c.IsPrimaryKey = true
  193. c.IsAutoIncrement = true
  194. c.Nullable = false
  195. return schemas.Integer
  196. default:
  197. return t
  198. }
  199. }
  200. func (db *sqlite3) FormatBytes(bs []byte) string {
  201. return fmt.Sprintf("X'%x'", bs)
  202. }
  203. func (db *sqlite3) IsReserved(name string) bool {
  204. _, ok := sqlite3ReservedWords[strings.ToUpper(name)]
  205. return ok
  206. }
  207. func (db *sqlite3) AutoIncrStr() string {
  208. return "AUTOINCREMENT"
  209. }
  210. func (db *sqlite3) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {
  211. args := []interface{}{idxName}
  212. return "SELECT name FROM sqlite_master WHERE type='index' and name = ?", args
  213. }
  214. func (db *sqlite3) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) {
  215. return db.HasRecords(queryer, ctx, "SELECT name FROM sqlite_master WHERE type='table' and name = ?", tableName)
  216. }
  217. func (db *sqlite3) DropIndexSQL(tableName string, index *schemas.Index) string {
  218. // var unique string
  219. idxName := index.Name
  220. if !strings.HasPrefix(idxName, "UQE_") &&
  221. !strings.HasPrefix(idxName, "IDX_") {
  222. if index.Type == schemas.UniqueType {
  223. idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
  224. } else {
  225. idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
  226. }
  227. }
  228. return fmt.Sprintf("DROP INDEX %v", db.Quoter().Quote(idxName))
  229. }
  230. func (db *sqlite3) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
  231. var sql string
  232. sql = "CREATE TABLE IF NOT EXISTS "
  233. if tableName == "" {
  234. tableName = table.Name
  235. }
  236. quoter := db.Quoter()
  237. sql += quoter.Quote(tableName)
  238. sql += " ("
  239. if len(table.ColumnsSeq()) > 0 {
  240. pkList := table.PrimaryKeys
  241. for _, colName := range table.ColumnsSeq() {
  242. col := table.GetColumn(colName)
  243. if col.IsPrimaryKey && len(pkList) == 1 {
  244. sql += db.String(col)
  245. } else {
  246. sql += db.StringNoPk(col)
  247. }
  248. sql = strings.TrimSpace(sql)
  249. sql += ", "
  250. }
  251. if len(pkList) > 1 {
  252. sql += "PRIMARY KEY ( "
  253. sql += quoter.Join(pkList, ",")
  254. sql += " ), "
  255. }
  256. sql = sql[:len(sql)-2]
  257. }
  258. sql += ")"
  259. return []string{sql}, true
  260. }
  261. func (db *sqlite3) ForUpdateSQL(query string) string {
  262. return query
  263. }
  264. func (db *sqlite3) IsColumnExist(queryer core.Queryer, ctx context.Context, tableName, colName string) (bool, error) {
  265. query := "SELECT * FROM " + tableName + " LIMIT 0"
  266. rows, err := queryer.QueryContext(ctx, query)
  267. if err != nil {
  268. return false, err
  269. }
  270. defer rows.Close()
  271. cols, err := rows.Columns()
  272. if err != nil {
  273. return false, err
  274. }
  275. for _, col := range cols {
  276. if strings.EqualFold(col, colName) {
  277. return true, nil
  278. }
  279. }
  280. return false, nil
  281. }
  282. // splitColStr splits a sqlite col strings as fields
  283. func splitColStr(colStr string) []string {
  284. colStr = strings.TrimSpace(colStr)
  285. var results = make([]string, 0, 10)
  286. var lastIdx int
  287. var hasC, hasQuote bool
  288. for i, c := range colStr {
  289. if c == ' ' && !hasQuote {
  290. if hasC {
  291. results = append(results, colStr[lastIdx:i])
  292. hasC = false
  293. }
  294. } else {
  295. if c == '\'' {
  296. hasQuote = !hasQuote
  297. }
  298. if !hasC {
  299. lastIdx = i
  300. }
  301. hasC = true
  302. if i == len(colStr)-1 {
  303. results = append(results, colStr[lastIdx:i+1])
  304. }
  305. }
  306. }
  307. return results
  308. }
  309. func parseString(colStr string) (*schemas.Column, error) {
  310. fields := splitColStr(colStr)
  311. col := new(schemas.Column)
  312. col.Indexes = make(map[string]int)
  313. col.Nullable = true
  314. col.DefaultIsEmpty = true
  315. for idx, field := range fields {
  316. if idx == 0 {
  317. col.Name = strings.Trim(strings.Trim(field, "`[] "), `"`)
  318. continue
  319. } else if idx == 1 {
  320. col.SQLType = schemas.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0}
  321. continue
  322. }
  323. switch field {
  324. case "PRIMARY":
  325. col.IsPrimaryKey = true
  326. case "AUTOINCREMENT":
  327. col.IsAutoIncrement = true
  328. case "NULL":
  329. if fields[idx-1] == "NOT" {
  330. col.Nullable = false
  331. } else {
  332. col.Nullable = true
  333. }
  334. case "DEFAULT":
  335. col.Default = fields[idx+1]
  336. col.DefaultIsEmpty = false
  337. }
  338. }
  339. return col, nil
  340. }
  341. func (db *sqlite3) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) {
  342. args := []interface{}{tableName}
  343. s := "SELECT sql FROM sqlite_master WHERE type='table' and name = ?"
  344. rows, err := queryer.QueryContext(ctx, s, args...)
  345. if err != nil {
  346. return nil, nil, err
  347. }
  348. defer rows.Close()
  349. var name string
  350. for rows.Next() {
  351. err = rows.Scan(&name)
  352. if err != nil {
  353. return nil, nil, err
  354. }
  355. break
  356. }
  357. if name == "" {
  358. return nil, nil, errors.New("no table named " + tableName)
  359. }
  360. nStart := strings.Index(name, "(")
  361. nEnd := strings.LastIndex(name, ")")
  362. reg := regexp.MustCompile(`[^\(,\)]*(\([^\(]*\))?`)
  363. colCreates := reg.FindAllString(name[nStart+1:nEnd], -1)
  364. cols := make(map[string]*schemas.Column)
  365. colSeq := make([]string, 0)
  366. for _, colStr := range colCreates {
  367. reg = regexp.MustCompile(`,\s`)
  368. colStr = reg.ReplaceAllString(colStr, ",")
  369. if strings.HasPrefix(strings.TrimSpace(colStr), "PRIMARY KEY") {
  370. parts := strings.Split(strings.TrimSpace(colStr), "(")
  371. if len(parts) == 2 {
  372. pkCols := strings.Split(strings.TrimRight(strings.TrimSpace(parts[1]), ")"), ",")
  373. for _, pk := range pkCols {
  374. if col, ok := cols[strings.Trim(strings.TrimSpace(pk), "`")]; ok {
  375. col.IsPrimaryKey = true
  376. }
  377. }
  378. }
  379. continue
  380. }
  381. col, err := parseString(colStr)
  382. if err != nil {
  383. return colSeq, cols, err
  384. }
  385. cols[col.Name] = col
  386. colSeq = append(colSeq, col.Name)
  387. }
  388. return colSeq, cols, nil
  389. }
  390. func (db *sqlite3) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) {
  391. args := []interface{}{}
  392. s := "SELECT name FROM sqlite_master WHERE type='table'"
  393. rows, err := queryer.QueryContext(ctx, s, args...)
  394. if err != nil {
  395. return nil, err
  396. }
  397. defer rows.Close()
  398. tables := make([]*schemas.Table, 0)
  399. for rows.Next() {
  400. table := schemas.NewEmptyTable()
  401. err = rows.Scan(&table.Name)
  402. if err != nil {
  403. return nil, err
  404. }
  405. if table.Name == "sqlite_sequence" {
  406. continue
  407. }
  408. tables = append(tables, table)
  409. }
  410. return tables, nil
  411. }
  412. func (db *sqlite3) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) {
  413. args := []interface{}{tableName}
  414. s := "SELECT sql FROM sqlite_master WHERE type='index' and tbl_name = ?"
  415. rows, err := queryer.QueryContext(ctx, s, args...)
  416. if err != nil {
  417. return nil, err
  418. }
  419. defer rows.Close()
  420. indexes := make(map[string]*schemas.Index, 0)
  421. for rows.Next() {
  422. var tmpSQL sql.NullString
  423. err = rows.Scan(&tmpSQL)
  424. if err != nil {
  425. return nil, err
  426. }
  427. if !tmpSQL.Valid {
  428. continue
  429. }
  430. sql := tmpSQL.String
  431. index := new(schemas.Index)
  432. nNStart := strings.Index(sql, "INDEX")
  433. nNEnd := strings.Index(sql, "ON")
  434. if nNStart == -1 || nNEnd == -1 {
  435. continue
  436. }
  437. indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []")
  438. var isRegular bool
  439. if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
  440. index.Name = indexName[5+len(tableName):]
  441. isRegular = true
  442. } else {
  443. index.Name = indexName
  444. }
  445. if strings.HasPrefix(sql, "CREATE UNIQUE INDEX") {
  446. index.Type = schemas.UniqueType
  447. } else {
  448. index.Type = schemas.IndexType
  449. }
  450. nStart := strings.Index(sql, "(")
  451. nEnd := strings.Index(sql, ")")
  452. colIndexes := strings.Split(sql[nStart+1:nEnd], ",")
  453. index.Cols = make([]string, 0)
  454. for _, col := range colIndexes {
  455. index.Cols = append(index.Cols, strings.Trim(col, "` []"))
  456. }
  457. index.IsRegular = isRegular
  458. indexes[index.Name] = index
  459. }
  460. return indexes, nil
  461. }
  462. func (db *sqlite3) Filters() []Filter {
  463. return []Filter{}
  464. }
  465. type sqlite3Driver struct {
  466. }
  467. func (p *sqlite3Driver) Parse(driverName, dataSourceName string) (*URI, error) {
  468. if strings.Contains(dataSourceName, "?") {
  469. dataSourceName = dataSourceName[:strings.Index(dataSourceName, "?")]
  470. }
  471. return &URI{DBType: schemas.SQLITE, DBName: dataSourceName}, nil
  472. }