2023-09-18 15:51:20 +08:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
2023-09-18 15:58:52 +08:00
|
|
|
"lab.evlic.cn/go/pkg/db"
|
|
|
|
"lab.evlic.cn/go/pkg/log"
|
2023-09-18 15:51:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DBType = "sqlite"
|
|
|
|
// DSN as default dsn
|
|
|
|
DSN = ":memory:"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Open(dsn string, debug bool) (*db.Client, error) {
|
|
|
|
// 全局模式
|
|
|
|
if len(dsn) == 0 {
|
|
|
|
dsn = DSN
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &gorm.Config{
|
|
|
|
PrepareStmt: true,
|
|
|
|
Logger: log.GormZapLogger(debug),
|
|
|
|
}
|
|
|
|
|
|
|
|
dbc, err := gorm.Open(sqlite.Open(dsn), opts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf(db.ErrOpenFormat, DBType, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &db.Client{
|
|
|
|
Type: DBType,
|
|
|
|
DB: dbc,
|
|
|
|
}, err
|
|
|
|
}
|