2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-08-08 09:09:28 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2020-01-06 20:43:59 +08:00
|
|
|
|
//
|
|
|
|
|
// Note:
|
|
|
|
|
// 1. It needs manually import: _ "github.com/mattn/go-sqlite3"
|
|
|
|
|
// 2. It does not support Save/Replace features.
|
2018-08-08 09:09:28 +08:00
|
|
|
|
|
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2020-04-08 21:26:14 +08:00
|
|
|
|
"errors"
|
2020-03-11 00:29:25 +08:00
|
|
|
|
"fmt"
|
2020-02-24 21:09:19 +08:00
|
|
|
|
"github.com/gogf/gf/internal/intlog"
|
2020-03-19 13:38:42 +08:00
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
2020-02-22 14:26:36 +08:00
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
2020-03-11 00:29:25 +08:00
|
|
|
|
"strings"
|
2018-08-08 09:09:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
// DriverSqlite is the driver for sqlite database.
|
|
|
|
|
type DriverSqlite struct {
|
|
|
|
|
*Core
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates and returns a database object for sqlite.
|
2020-03-08 11:03:18 +08:00
|
|
|
|
// It implements the interface of gdb.Driver for extra database driver installation.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverSqlite) New(core *Core, node *ConfigNode) (DB, error) {
|
|
|
|
|
return &DriverSqlite{
|
|
|
|
|
Core: core,
|
|
|
|
|
}, nil
|
2018-08-08 09:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
|
// Open creates and returns a underlying sql.DB object for sqlite.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverSqlite) Open(config *ConfigNode) (*sql.DB, error) {
|
2018-08-08 09:09:28 +08:00
|
|
|
|
var source string
|
2020-03-19 13:38:42 +08:00
|
|
|
|
var err error
|
2019-04-02 14:37:46 +08:00
|
|
|
|
if config.LinkInfo != "" {
|
|
|
|
|
source = config.LinkInfo
|
2018-08-08 09:09:28 +08:00
|
|
|
|
} else {
|
2018-12-15 15:50:39 +08:00
|
|
|
|
source = config.Name
|
2018-08-08 09:09:28 +08:00
|
|
|
|
}
|
2020-03-19 13:38:42 +08:00
|
|
|
|
source, err = gfile.Search(source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-02-24 21:09:19 +08:00
|
|
|
|
intlog.Printf("Open: %s", source)
|
2018-08-08 09:09:28 +08:00
|
|
|
|
if db, err := sql.Open("sqlite3", source); err == nil {
|
|
|
|
|
return db, nil
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-08 00:17:42 +08:00
|
|
|
|
// GetChars returns the security char for this type of database.
|
|
|
|
|
func (d *DriverSqlite) GetChars() (charLeft string, charRight string) {
|
2018-12-14 18:35:51 +08:00
|
|
|
|
return "`", "`"
|
2018-08-08 09:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-09 22:00:01 +08:00
|
|
|
|
// HandleSqlBeforeCommit deals with the sql string before commits it to underlying sql driver.
|
2020-03-23 20:44:20 +08:00
|
|
|
|
// TODO 需要增加对Save方法的支持,可使用正则来实现替换,
|
|
|
|
|
// TODO 将ON DUPLICATE KEY UPDATE触发器修改为两条SQL语句(INSERT OR IGNORE & UPDATE)
|
2020-03-09 22:00:01 +08:00
|
|
|
|
func (d *DriverSqlite) HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{}) {
|
2020-03-09 21:53:58 +08:00
|
|
|
|
return sql, args
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
|
// Tables retrieves and returns the tables of current schema.
|
2020-03-08 11:03:18 +08:00
|
|
|
|
// It's mainly used in cli tool chain for automatically generating the models.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverSqlite) Tables(schema ...string) (tables []string, err error) {
|
2020-03-11 00:29:25 +08:00
|
|
|
|
var result Result
|
2020-03-11 22:45:38 +08:00
|
|
|
|
link, err := d.DB.GetSlave(schema...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-03-11 00:29:25 +08:00
|
|
|
|
|
2020-03-11 22:45:38 +08:00
|
|
|
|
result, err = d.DB.DoGetAll(link, `SELECT NAME FROM SQLITE_MASTER WHERE TYPE='table' ORDER BY NAME`)
|
2020-03-11 00:29:25 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, m := range result {
|
|
|
|
|
for _, v := range m {
|
|
|
|
|
tables = append(tables, v.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-02 15:48:25 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 19:55:53 +08:00
|
|
|
|
// TableFields retrieves and returns the fields information of specified table of current schema.
|
2020-03-08 00:17:42 +08:00
|
|
|
|
func (d *DriverSqlite) TableFields(table string, schema ...string) (fields map[string]*TableField, err error) {
|
2020-04-08 21:26:14 +08:00
|
|
|
|
charL, charR := d.GetChars()
|
|
|
|
|
table = gstr.Trim(table, charL+charR)
|
2020-02-22 14:26:36 +08:00
|
|
|
|
if gstr.Contains(table, " ") {
|
2020-04-08 21:26:14 +08:00
|
|
|
|
return nil, errors.New("function TableFields supports only single table operations")
|
2020-02-22 14:26:36 +08:00
|
|
|
|
}
|
2020-03-11 00:29:25 +08:00
|
|
|
|
checkSchema := d.DB.GetSchema()
|
|
|
|
|
if len(schema) > 0 && schema[0] != "" {
|
|
|
|
|
checkSchema = schema[0]
|
|
|
|
|
}
|
|
|
|
|
v := d.DB.GetCache().GetOrSetFunc(
|
|
|
|
|
fmt.Sprintf(`sqlite_table_fields_%s_%s`, table, checkSchema), func() interface{} {
|
|
|
|
|
var result Result
|
2020-03-11 22:45:38 +08:00
|
|
|
|
var link *sql.DB
|
|
|
|
|
link, err = d.DB.GetSlave(checkSchema)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
result, err = d.DB.DoGetAll(link, fmt.Sprintf(`PRAGMA TABLE_INFO(%s)`, table))
|
2020-03-11 00:29:25 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
fields = make(map[string]*TableField)
|
|
|
|
|
for i, m := range result {
|
|
|
|
|
fields[strings.ToLower(m["name"].String())] = &TableField{
|
|
|
|
|
Index: i,
|
|
|
|
|
Name: strings.ToLower(m["name"].String()),
|
|
|
|
|
Type: strings.ToLower(m["type"].String()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fields
|
|
|
|
|
}, 0)
|
|
|
|
|
if err == nil {
|
|
|
|
|
fields = v.(map[string]*TableField)
|
|
|
|
|
}
|
2019-09-02 15:48:25 +08:00
|
|
|
|
return
|
|
|
|
|
}
|