Files
mayfly-go/server/internal/db/dbm/sqlite/meta.go
meilin.huang e56788af3e refactor: dbm
2024-12-08 13:04:23 +08:00

57 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sqlite
import (
"database/sql"
"errors"
"mayfly-go/internal/db/dbm/dbi"
"mayfly-go/pkg/utils/collx"
"os"
)
func init() {
dbi.Register(DbTypeSqlite, new(Meta))
}
const (
DbTypeSqlite dbi.DbType = "sqlite"
)
type Meta struct {
}
func (md *Meta) GetSqlDb(d *dbi.DbInfo) (*sql.DB, error) {
// 用host字段来存sqlite的文件路径
// 检查文件是否存在,否则报错基于sqlite会自动创建文件为了服务器文件安全所以先确定文件存在再连接不自动创建
if _, err := os.Stat(d.Host); err != nil {
return nil, errors.New("数据库文件不存在")
}
db, err := sql.Open("sqlite", d.Host)
if err != nil {
return nil, err
}
_, err = db.Exec("PRAGMA busy_timeout = 50000;")
return db, err
}
func (sm *Meta) GetDialect(conn *dbi.DbConn) dbi.Dialect {
return &SqliteDialect{dc: conn}
}
func (sm *Meta) GetMetadata(conn *dbi.DbConn) dbi.Metadata {
return &SqliteMetadata{dc: conn}
}
func (sm *Meta) GetDbDataTypes() []*dbi.DbDataType {
return collx.AsArray(
Integer, Real,
Text,
Blob,
DateTime, Date, Time,
)
}
func (sm *Meta) GetCommonTypeConverter() dbi.CommonTypeConverter {
return &commonTypeConverter{}
}