1
0

db_sqlite.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "database/sql"
  17. "fmt"
  18. )
  19. // sqlite operators.
  20. var sqliteOperators = map[string]string{
  21. "exact": "= ?",
  22. "iexact": "LIKE ? ESCAPE '\\'",
  23. "contains": "LIKE ? ESCAPE '\\'",
  24. "icontains": "LIKE ? ESCAPE '\\'",
  25. "gt": "> ?",
  26. "gte": ">= ?",
  27. "lt": "< ?",
  28. "lte": "<= ?",
  29. "eq": "= ?",
  30. "ne": "!= ?",
  31. "startswith": "LIKE ? ESCAPE '\\'",
  32. "endswith": "LIKE ? ESCAPE '\\'",
  33. "istartswith": "LIKE ? ESCAPE '\\'",
  34. "iendswith": "LIKE ? ESCAPE '\\'",
  35. }
  36. // sqlite column types.
  37. var sqliteTypes = map[string]string{
  38. "auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT",
  39. "pk": "NOT NULL PRIMARY KEY",
  40. "bool": "bool",
  41. "string": "varchar(%d)",
  42. "string-text": "text",
  43. "time.Time-date": "date",
  44. "time.Time": "datetime",
  45. "int8": "tinyint",
  46. "int16": "smallint",
  47. "int32": "integer",
  48. "int64": "bigint",
  49. "uint8": "tinyint unsigned",
  50. "uint16": "smallint unsigned",
  51. "uint32": "integer unsigned",
  52. "uint64": "bigint unsigned",
  53. "float64": "real",
  54. "float64-decimal": "decimal",
  55. }
  56. // sqlite dbBaser.
  57. type dbBaseSqlite struct {
  58. dbBase
  59. }
  60. var _ dbBaser = new(dbBaseSqlite)
  61. // get sqlite operator.
  62. func (d *dbBaseSqlite) OperatorSQL(operator string) string {
  63. return sqliteOperators[operator]
  64. }
  65. // generate functioned sql for sqlite.
  66. // only support DATE(text).
  67. func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) {
  68. if fi.fieldType == TypeDateField {
  69. *leftCol = fmt.Sprintf("DATE(%s)", *leftCol)
  70. }
  71. }
  72. // unable updating joined record in sqlite.
  73. func (d *dbBaseSqlite) SupportUpdateJoin() bool {
  74. return false
  75. }
  76. // max int in sqlite.
  77. func (d *dbBaseSqlite) MaxLimit() uint64 {
  78. return 9223372036854775807
  79. }
  80. // get column types in sqlite.
  81. func (d *dbBaseSqlite) DbTypes() map[string]string {
  82. return sqliteTypes
  83. }
  84. // get show tables sql in sqlite.
  85. func (d *dbBaseSqlite) ShowTablesQuery() string {
  86. return "SELECT name FROM sqlite_master WHERE type = 'table'"
  87. }
  88. // get columns in sqlite.
  89. func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) {
  90. query := d.ins.ShowColumnsQuery(table)
  91. rows, err := db.Query(query)
  92. if err != nil {
  93. return nil, err
  94. }
  95. columns := make(map[string][3]string)
  96. for rows.Next() {
  97. var tmp, name, typ, null sql.NullString
  98. err := rows.Scan(&tmp, &name, &typ, &null, &tmp, &tmp)
  99. if err != nil {
  100. return nil, err
  101. }
  102. columns[name.String] = [3]string{name.String, typ.String, null.String}
  103. }
  104. return columns, nil
  105. }
  106. // get show columns sql in sqlite.
  107. func (d *dbBaseSqlite) ShowColumnsQuery(table string) string {
  108. return fmt.Sprintf("pragma table_info('%s')", table)
  109. }
  110. // check index exist in sqlite.
  111. func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool {
  112. query := fmt.Sprintf("PRAGMA index_list('%s')", table)
  113. rows, err := db.Query(query)
  114. if err != nil {
  115. panic(err)
  116. }
  117. defer rows.Close()
  118. for rows.Next() {
  119. var tmp, index sql.NullString
  120. rows.Scan(&tmp, &index, &tmp)
  121. if name == index.String {
  122. return true
  123. }
  124. }
  125. return false
  126. }
  127. // create new sqlite dbBaser.
  128. func newdbBaseSqlite() dbBaser {
  129. b := new(dbBaseSqlite)
  130. b.ins = b
  131. return b
  132. }