39 lines
568 B
Go
39 lines
568 B
Go
|
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
|
||
|
}
|