```
refactor(database): 调整数据库初始化函数参数传递方式 将 Init 函数参数从全局变量改为通过 NewDatabase 函数参数传入, 使初始化逻辑更清晰、可控。同时优化代码格式,去除多余空行, 提升代码可读性。 ```
This commit is contained in:
parent
f2d8ae26f6
commit
cf0ee224f7
|
@ -118,12 +118,12 @@ func PKCS7UnPadding(origData []byte, blocksize int) []byte {
|
||||||
if blocksize <= 0 {
|
if blocksize <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查原始数据是否为空
|
// 检查原始数据是否为空
|
||||||
if origData == nil || len(origData) == 0 {
|
if origData == nil || len(origData) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查数据长度是否为块大小的整数倍
|
// 检查数据长度是否为块大小的整数倍
|
||||||
if len(origData)%blocksize != 0 {
|
if len(origData)%blocksize != 0 {
|
||||||
return nil
|
return nil
|
||||||
|
@ -137,7 +137,7 @@ func PKCS7UnPadding(origData []byte, blocksize int) []byte {
|
||||||
if length-unpadding <= 0 {
|
if length-unpadding <= 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回去除填充后的数据
|
// 返回去除填充后的数据
|
||||||
return origData[:(length - unpadding)]
|
return origData[:(length - unpadding)]
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,13 +15,11 @@ import (
|
||||||
var (
|
var (
|
||||||
// MigrateTables holds the tables that need to be auto-migrated on database initialization
|
// MigrateTables holds the tables that need to be auto-migrated on database initialization
|
||||||
MigrateTables []any
|
MigrateTables []any
|
||||||
// Init is an optional initialization function that can be executed after database connection is established
|
|
||||||
Init *func() = nil
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDatabase creates a new database connection based on the provided driver type
|
// NewDatabase creates a new database connection based on the provided driver type
|
||||||
// It supports both MySQL and PostgreSQL databases
|
// It supports both MySQL and PostgreSQL databases
|
||||||
func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *gorm.DB, err error) {
|
func NewDatabase(driver string, dsn []string, options *types.SqlOptions, Init *func()) (db *gorm.DB, err error) {
|
||||||
driver = strings.ToLower(driver)
|
driver = strings.ToLower(driver)
|
||||||
switch driver {
|
switch driver {
|
||||||
case "mysql":
|
case "mysql":
|
||||||
|
@ -73,7 +71,7 @@ func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err err
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
||||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||||
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
||||||
|
@ -118,7 +116,7 @@ func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
// SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。
|
||||||
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
sqlDB.SetMaxIdleConns(options.MaxIdleConns)
|
||||||
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
// SetMaxOpenConns 设置打开数据库连接的最大数量。
|
||||||
|
@ -134,4 +132,4 @@ func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue