Files
gf/database/gdb/gdb_driver_sqlite.go

76 lines
2.3 KiB
Go
Raw Normal View History

// 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,
// 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"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/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.LinkInfo != "" {
source = config.LinkInfo
2018-08-08 09:09:28 +08:00
} else {
source = config.Name
2018-08-08 09:09:28 +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
}
// Tables retrieves and returns the tables of current schema.
// It's mainly used in cli tool chain for automatically generating the models.
// TODO
2020-03-08 00:17:42 +08:00
func (d *DriverSqlite) Tables(schema ...string) (tables []string, err error) {
return
}
// TableFields retrieves and returns the fields information of specified table of current schema.
// TODO
2020-03-08 00:17:42 +08:00
func (d *DriverSqlite) TableFields(table string, schema ...string) (fields map[string]*TableField, err error) {
table = gstr.Trim(table)
if gstr.Contains(table, " ") {
panic("function TableFields supports only single table operations")
}
return
}
2020-03-08 00:17:42 +08:00
// HandleSqlBeforeExec deals with the sql string before commits it to underlying sql driver.
// @todo 需要增加对Save方法的支持可使用正则来实现替换
// @todo 将ON DUPLICATE KEY UPDATE触发器修改为两条SQL语句(INSERT OR IGNORE & UPDATE)
2020-03-08 00:17:42 +08:00
func (d *DriverSqlite) HandleSqlBeforeExec(sql string) string {
2020-01-07 22:14:32 +08:00
return sql
2019-06-19 09:06:52 +08:00
}