gdb_driver_sqlite.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. //
  7. // Note:
  8. // 1. It needs manually import: _ "github.com/mattn/go-sqlite3"
  9. // 2. It does not support Save/Replace features.
  10. package gdb
  11. import (
  12. "database/sql"
  13. "fmt"
  14. "github.com/gogf/gf/errors/gerror"
  15. "github.com/gogf/gf/internal/intlog"
  16. "github.com/gogf/gf/os/gfile"
  17. "github.com/gogf/gf/text/gstr"
  18. "strings"
  19. )
  20. // DriverSqlite is the driver for sqlite database.
  21. type DriverSqlite struct {
  22. *Core
  23. }
  24. // New creates and returns a database object for sqlite.
  25. // It implements the interface of gdb.Driver for extra database driver installation.
  26. func (d *DriverSqlite) New(core *Core, node *ConfigNode) (DB, error) {
  27. return &DriverSqlite{
  28. Core: core,
  29. }, nil
  30. }
  31. // Open creates and returns a underlying sql.DB object for sqlite.
  32. func (d *DriverSqlite) Open(config *ConfigNode) (*sql.DB, error) {
  33. var source string
  34. if config.LinkInfo != "" {
  35. source = config.LinkInfo
  36. } else {
  37. source = config.Name
  38. }
  39. // It searches the source file to locate its absolute path..
  40. if absolutePath, _ := gfile.Search(source); absolutePath != "" {
  41. source = absolutePath
  42. }
  43. intlog.Printf("Open: %s", source)
  44. if db, err := sql.Open("sqlite3", source); err == nil {
  45. return db, nil
  46. } else {
  47. return nil, err
  48. }
  49. }
  50. // GetChars returns the security char for this type of database.
  51. func (d *DriverSqlite) GetChars() (charLeft string, charRight string) {
  52. return "`", "`"
  53. }
  54. // HandleSqlBeforeCommit deals with the sql string before commits it to underlying sql driver.
  55. // TODO 需要增加对Save方法的支持,可使用正则来实现替换,
  56. // TODO 将ON DUPLICATE KEY UPDATE触发器修改为两条SQL语句(INSERT OR IGNORE & UPDATE)
  57. func (d *DriverSqlite) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
  58. return sql, args
  59. }
  60. // Tables retrieves and returns the tables of current schema.
  61. // It's mainly used in cli tool chain for automatically generating the models.
  62. func (d *DriverSqlite) Tables(schema ...string) (tables []string, err error) {
  63. var result Result
  64. link, err := d.DB.GetSlave(schema...)
  65. if err != nil {
  66. return nil, err
  67. }
  68. result, err = d.DB.DoGetAll(link, `SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' ORDER BY NAME`)
  69. if err != nil {
  70. return
  71. }
  72. for _, m := range result {
  73. for _, v := range m {
  74. tables = append(tables, v.String())
  75. }
  76. }
  77. return
  78. }
  79. // TableFields retrieves and returns the fields information of specified table of current schema.
  80. func (d *DriverSqlite) TableFields(table string, schema ...string) (fields map[string]*TableField, err error) {
  81. charL, charR := d.GetChars()
  82. table = gstr.Trim(table, charL+charR)
  83. if gstr.Contains(table, " ") {
  84. return nil, gerror.New("function TableFields supports only single table operations")
  85. }
  86. checkSchema := d.DB.GetSchema()
  87. if len(schema) > 0 && schema[0] != "" {
  88. checkSchema = schema[0]
  89. }
  90. v, _ := internalCache.GetOrSetFunc(
  91. fmt.Sprintf(`sqlite_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
  92. func() (interface{}, error) {
  93. var (
  94. result Result
  95. link *sql.DB
  96. )
  97. link, err = d.DB.GetSlave(checkSchema)
  98. if err != nil {
  99. return nil, err
  100. }
  101. result, err = d.DB.DoGetAll(link, fmt.Sprintf(`PRAGMA TABLE_INFO(%s)`, table))
  102. if err != nil {
  103. return nil, err
  104. }
  105. fields = make(map[string]*TableField)
  106. for i, m := range result {
  107. fields[strings.ToLower(m["name"].String())] = &TableField{
  108. Index: i,
  109. Name: strings.ToLower(m["name"].String()),
  110. Type: strings.ToLower(m["type"].String()),
  111. }
  112. }
  113. return fields, nil
  114. }, 0)
  115. if err == nil {
  116. fields = v.(map[string]*TableField)
  117. }
  118. return
  119. }