feat: 支持db_path, 适配 sqlite3 的数据库文件

This commit is contained in:
2025-12-31 16:12:53 +08:00
parent 8004cbb02c
commit 3f564974db
3 changed files with 3 additions and 46 deletions

View File

@@ -53,6 +53,7 @@ type Database struct {
// Date : 18:44 2022/5/14
type Driver struct {
DBType string `json:"db_type" yaml:"db_type" toml:"db_type" ini:"db_type"` // 数据库驱动类型
DBPath string `json:"db_path" yaml:"db_path" toml:"db_path" ini:"db_path"` // sqlite 数据库文件路径
Host string `json:"host" yaml:"host" toml:"host" ini:"host"` // 数据库地址
Port int `json:"port" yaml:"port" toml:"port" ini:"port"` // 数据库端口
Username string `json:"username" yaml:"username" toml:"username" ini:"username"` // 用户名
@@ -64,35 +65,23 @@ type Driver struct {
}
// Connection 连接数配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:18 2022/6/9
type Connection struct {
MaxOpen int `json:"max_open" yaml:"max_open" toml:"max_open" ini:"max_open"` // 最大打开连接数
MaxIdle int `json:"max_idle" yaml:"max_idle" toml:"max_idle" ini:"max_idle"` // 最大的处理连接数
}
// DescTableItem 表结构的描述
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:45 2023/8/16
type DescTableItem struct {
Default *string `json:"Default"` // 默认值
Extra string `json:"Extra"` // 扩展信息
Field string `json:"field"` // 字段名
Key string `json:"Key"` // 索引信息
Null string `json:"Null"` // 是否允许为NUll
Null string `json:"Null"` // 是否允许为 NUll
Type string `json:"Type"` // 字段类型
Comment string `json:"Comment"` // 字段注释
}
// ColumnInfo 表字段结构,INFORMATION_SCHEMA.COLUMNS 标的查询结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 23:00 2023/8/16
type ColumnInfo struct {
TableCatalog string `json:"TABLE_CATALOG" gorm:"column:TABLE_CATALOG;default:;NOT NULL"` // TABLE_CATALOG
TableSchema string `json:"TABLE_SCHEMA" gorm:"column:TABLE_SCHEMA;default:;NOT NULL"` // TABLE_SCHEMA

View File

@@ -47,10 +47,6 @@ type wrapperClient struct {
}
// AddWithConfigFile 使用文件生成新的客户端文件名去掉后缀作为flag
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:19 2022/6/5
func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error {
var (
err error
@@ -68,10 +64,6 @@ func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.L
}
// AddWithConfig ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:41 2023/4/18
func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error {
dbClient := &DBClient{
DbFlag: flag,
@@ -90,10 +82,6 @@ func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, data
}
// BatchAddWithConfigDir 自动读取目录下配置文件, 生成客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:19 2022/6/5
func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error {
filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
for i := range filepathNames {
@@ -105,10 +93,6 @@ func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Lo
}
// getCfg 读取配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:05 2022/6/11
func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
fileArr := strings.Split(cfgPath, ".")
if len(fileArr) < 2 {
@@ -154,10 +138,6 @@ func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
}
// GetDBClient 获取db client
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:32 2022/6/5
func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseClient, error) {
c.lock.RLock()
defer c.lock.RUnlock()
@@ -172,10 +152,6 @@ func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseCli
}
// GetMasterClient 获取主库客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:36 2022/6/5
func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
var (
err error
@@ -189,10 +165,6 @@ func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*go
}
// GetSlaveClient 获取从库客户端
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:37 2022/6/5
func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
var (
err error

View File

@@ -88,10 +88,6 @@ func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node strin
}
// GetDatabaseClient 获取数据库连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:41 2022/6/11
func (dc *DBClient) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error) {
var (
instance *gorm.DB
@@ -132,7 +128,7 @@ func (dc *DBClient) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logg
func (dc *DBClient) buildConnectionDSN(conf *define.Driver) string {
if conf.DBType == consts.DatabaseDriverSqlite3 {
// 兼容sqlite3
return conf.Host
return conf.DBPath
}
return fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",