pkg/sqlite/db.go

39 lines
568 B
Go
Raw Normal View History

2023-09-18 15:51:20 +08:00
package sqlite
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"packet-flow/pkg/db"
"packet-flow/pkg/log"
)
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
}