feat: 支持db_path, 适配 sqlite3 的数据库文件
This commit is contained in:
@@ -53,6 +53,7 @@ type Database struct {
|
|||||||
// Date : 18:44 2022/5/14
|
// Date : 18:44 2022/5/14
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
DBType string `json:"db_type" yaml:"db_type" toml:"db_type" ini:"db_type"` // 数据库驱动类型
|
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"` // 数据库地址
|
Host string `json:"host" yaml:"host" toml:"host" ini:"host"` // 数据库地址
|
||||||
Port int `json:"port" yaml:"port" toml:"port" ini:"port"` // 数据库端口
|
Port int `json:"port" yaml:"port" toml:"port" ini:"port"` // 数据库端口
|
||||||
Username string `json:"username" yaml:"username" toml:"username" ini:"username"` // 用户名
|
Username string `json:"username" yaml:"username" toml:"username" ini:"username"` // 用户名
|
||||||
@@ -64,20 +65,12 @@ type Driver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connection 连接数配置
|
// Connection 连接数配置
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 15:18 2022/6/9
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
MaxOpen int `json:"max_open" yaml:"max_open" toml:"max_open" ini:"max_open"` // 最大打开连接数
|
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"` // 最大的处理连接数
|
MaxIdle int `json:"max_idle" yaml:"max_idle" toml:"max_idle" ini:"max_idle"` // 最大的处理连接数
|
||||||
}
|
}
|
||||||
|
|
||||||
// DescTableItem 表结构的描述
|
// DescTableItem 表结构的描述
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 22:45 2023/8/16
|
|
||||||
type DescTableItem struct {
|
type DescTableItem struct {
|
||||||
Default *string `json:"Default"` // 默认值
|
Default *string `json:"Default"` // 默认值
|
||||||
Extra string `json:"Extra"` // 扩展信息
|
Extra string `json:"Extra"` // 扩展信息
|
||||||
@@ -89,10 +82,6 @@ type DescTableItem struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ColumnInfo 表字段结构,INFORMATION_SCHEMA.COLUMNS 标的查询结果
|
// ColumnInfo 表字段结构,INFORMATION_SCHEMA.COLUMNS 标的查询结果
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 23:00 2023/8/16
|
|
||||||
type ColumnInfo struct {
|
type ColumnInfo struct {
|
||||||
TableCatalog string `json:"TABLE_CATALOG" gorm:"column:TABLE_CATALOG;default:;NOT NULL"` // TABLE_CATALOG
|
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
|
TableSchema string `json:"TABLE_SCHEMA" gorm:"column:TABLE_SCHEMA;default:;NOT NULL"` // TABLE_SCHEMA
|
||||||
|
|||||||
@@ -47,10 +47,6 @@ type wrapperClient struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddWithConfigFile 使用文件生成新的客户端,文件名去掉后缀作为flag
|
// 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 {
|
func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.Logger, extraFieldList []string) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
@@ -68,10 +64,6 @@ func (c *wrapperClient) AddWithConfigFile(cfgFilePath string, logInstance *zap.L
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddWithConfig ...
|
// 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 {
|
func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, databaseConfig *define.Database, extraFieldList []string) error {
|
||||||
dbClient := &DBClient{
|
dbClient := &DBClient{
|
||||||
DbFlag: flag,
|
DbFlag: flag,
|
||||||
@@ -90,10 +82,6 @@ func (c *wrapperClient) AddWithConfig(flag string, logInstance *zap.Logger, data
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BatchAddWithConfigDir 自动读取目录下配置文件, 生成客户端
|
// BatchAddWithConfigDir 自动读取目录下配置文件, 生成客户端
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 19:19 2022/6/5
|
|
||||||
func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error {
|
func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Logger, extraFieldList []string) error {
|
||||||
filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
|
filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
|
||||||
for i := range filepathNames {
|
for i := range filepathNames {
|
||||||
@@ -105,10 +93,6 @@ func (c *wrapperClient) BatchAddWithConfigDir(cfgDir string, logInstance *zap.Lo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getCfg 读取配置
|
// getCfg 读取配置
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 18:05 2022/6/11
|
|
||||||
func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
|
func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
|
||||||
fileArr := strings.Split(cfgPath, ".")
|
fileArr := strings.Split(cfgPath, ".")
|
||||||
if len(fileArr) < 2 {
|
if len(fileArr) < 2 {
|
||||||
@@ -154,10 +138,6 @@ func (c *wrapperClient) getCfg(cfgPath string) (*define.CfgFile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetDBClient 获取db client
|
// GetDBClient 获取db client
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 19:32 2022/6/5
|
|
||||||
func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseClient, error) {
|
func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseClient, error) {
|
||||||
c.lock.RLock()
|
c.lock.RLock()
|
||||||
defer c.lock.RUnlock()
|
defer c.lock.RUnlock()
|
||||||
@@ -172,10 +152,6 @@ func (c *wrapperClient) GetDBClient(dbFlag string) (abstract.IWrapperDatabaseCli
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetMasterClient 获取主库客户端
|
// GetMasterClient 获取主库客户端
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 19:36 2022/6/5
|
|
||||||
func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
|
func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
@@ -189,10 +165,6 @@ func (c *wrapperClient) GetMasterClient(ctx context.Context, dbFlag string) (*go
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetSlaveClient 获取从库客户端
|
// GetSlaveClient 获取从库客户端
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 19:37 2022/6/5
|
|
||||||
func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
|
func (c *wrapperClient) GetSlaveClient(ctx context.Context, dbFlag string) (*gorm.DB, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
|
|||||||
@@ -88,10 +88,6 @@ func (dc *DBClient) getLogger(ctx context.Context, dbClient *gorm.DB, node strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetDatabaseClient 获取数据库连接
|
// 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) {
|
func (dc *DBClient) GetDatabaseClient(conf *define.Driver, logInstance *zap.Logger) (*gorm.DB, error) {
|
||||||
var (
|
var (
|
||||||
instance *gorm.DB
|
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 {
|
func (dc *DBClient) buildConnectionDSN(conf *define.Driver) string {
|
||||||
if conf.DBType == consts.DatabaseDriverSqlite3 {
|
if conf.DBType == consts.DatabaseDriverSqlite3 {
|
||||||
// 兼容sqlite3
|
// 兼容sqlite3
|
||||||
return conf.Host
|
return conf.DBPath
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
|
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=%s",
|
||||||
|
|||||||
Reference in New Issue
Block a user