gf/database/gdb/gdb_driver_sqlite.go

154 lines
4.5 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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,
// 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 (
"context"
2018-08-08 09:09:28 +08:00
"database/sql"
2020-03-11 00:29:25 +08:00
"fmt"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/errors/gcode"
"strings"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
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.
// 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
}
// 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
if config.Link != "" {
source = config.Link
2018-08-08 09:09:28 +08:00
} else {
source = config.Name
2018-08-08 09:09:28 +08:00
}
2020-11-19 20:37:27 +08:00
// It searches the source file to locate its absolute path..
if absolutePath, _ := gfile.Search(source); absolutePath != "" {
source = absolutePath
}
intlog.Printf(d.GetCtx(), "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
}
}
// FilteredLink retrieves and returns filtered `linkInfo` that can be using for
2021-01-25 21:17:32 +08:00
// logging or tracing purpose.
func (d *DriverSqlite) FilteredLink() string {
return d.GetConfig().Link
2021-01-25 21:17:32 +08:00
}
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
}
// DoCommit deals with the sql string before commits it to underlying sql driver.
2021-06-26 18:20:55 +08:00
func (d *DriverSqlite) DoCommit(ctx context.Context, link Link, sql string, args []interface{}) (newSql string, newArgs []interface{}, err error) {
return d.Core.DoCommit(ctx, link, sql, args)
}
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
func (d *DriverSqlite) Tables(ctx context.Context, schema ...string) (tables []string, err error) {
2020-03-11 00:29:25 +08:00
var result Result
2021-05-21 15:30:21 +08:00
link, err := d.SlaveLink(schema...)
if err != nil {
return nil, err
}
2020-03-11 00:29:25 +08:00
result, err = d.DoGetAll(ctx, 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())
}
}
return
}
// TableFields retrieves and returns the fields information of specified table of current schema.
//
// Also see DriverMysql.TableFields.
func (d *DriverSqlite) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*TableField, err error) {
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "function TableFields supports only single table operations")
}
2021-05-21 15:30:21 +08:00
useSchema := d.db.GetSchema()
2020-03-11 00:29:25 +08:00
if len(schema) > 0 && schema[0] != "" {
2021-05-21 15:30:21 +08:00
useSchema = schema[0]
2020-03-11 00:29:25 +08:00
}
tableFieldsCacheKey := fmt.Sprintf(
`sqlite_table_fields_%s_%s@group:%s`,
2021-05-21 15:30:21 +08:00
table, useSchema, d.GetGroup(),
)
v := tableFieldsMap.GetOrSetFuncLock(tableFieldsCacheKey, func() interface{} {
var (
result Result
2021-05-21 15:30:21 +08:00
link, err = d.SlaveLink(useSchema)
)
if err != nil {
return nil
}
result, err = d.DoGetAll(ctx, link, fmt.Sprintf(`PRAGMA TABLE_INFO(%s)`, table))
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()),
2020-03-11 00:29:25 +08:00
}
}
return fields
})
if v != nil {
2020-03-11 00:29:25 +08:00
fields = v.(map[string]*TableField)
}
return
}
// DoInsert is not supported in sqlite.
func (d *DriverSqlite) DoInsert(ctx context.Context, link Link, table string, list List, option DoInsertOption) (result sql.Result, err error) {
switch option.InsertOption {
case insertOptionSave:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Save operation is not supported by sqlite driver`)
case insertOptionReplace:
return nil, gerror.NewCode(gcode.CodeNotSupported, `Replace operation is not supported by sqlite driver`)
default:
return d.Core.DoInsert(ctx, link, table, list, option)
}
}