检查Dialect设置是否正确

This commit is contained in:
springrain
2025-11-07 11:02:37 +08:00
parent 11a165fbc8
commit ba3b285c9b
2 changed files with 31 additions and 5 deletions

View File

@@ -1,3 +1,7 @@
v1.8.0
- 检查Dialect设置是否正确
- 完善文档,注释
v1.7.9 v1.7.9
- 感谢 @encircles 的pr,优化FuncWrapFieldTagName方法,添加context参数,解决多类型数据库时Tag列名的bug - 感谢 @encircles 的pr,优化FuncWrapFieldTagName方法,添加context参数,解决多类型数据库时Tag列名的bug
- 完善文档,注释 - 完善文档,注释

View File

@@ -33,6 +33,22 @@ type dataSource struct {
// config *DataSourceConfig // config *DataSourceConfig
} }
// dbDialectMap 数据库方言的map,用来检查Dialect设置是否正确
// dbDialectMap is a map of database dialects, used to check whether the Dialect setting is correct
var dbDialectMap = map[string]string{
"mysql": "mysql",
"postgresql": "postgres",
"oracle": "oracle",
"mssql": "sqlserver",
"sqlite": "sqlite3",
"db2": "go_ibm_db",
"clickhouse": "clickhouse",
"dm": "dm",
"kingbase": "kingbase",
"shentong": "aci",
"tdengine": "taosSql",
}
// newDataSource 创建一个新的datasource,内部调用,避免外部直接使用datasource // newDataSource 创建一个新的datasource,内部调用,避免外部直接使用datasource
// newDAtaSource Create a new datasource and call it internally to avoid direct external use of the datasource // newDAtaSource Create a new datasource and call it internally to avoid direct external use of the datasource
func newDataSource(config *DataSourceConfig) (*dataSource, error) { func newDataSource(config *DataSourceConfig) (*dataSource, error) {
@@ -53,6 +69,12 @@ func newDataSource(config *DataSourceConfig) (*dataSource, error) {
if config.Dialect == "" { if config.Dialect == "" {
return nil, errors.New("->newDataSource-->Dialect cannot be empty") return nil, errors.New("->newDataSource-->Dialect cannot be empty")
} }
_, has := dbDialectMap[config.Dialect]
if !has {
return nil, errors.New("->newDataSource-->Dialect not supported, please check the Dialect configuration. Supported Dialect include mysql,postgresql,oracle,mssql,sqlite,db2,clickhouse,dm,kingbase,shentong,tdengine ")
}
var db *sql.DB var db *sql.DB
var errSQLOpen error var errSQLOpen error
@@ -63,7 +85,7 @@ func newDataSource(config *DataSourceConfig) (*dataSource, error) {
db, errSQLOpen = sql.Open(config.DriverName, config.DSN) db, errSQLOpen = sql.Open(config.DriverName, config.DSN)
if errSQLOpen != nil { if errSQLOpen != nil {
errSQLOpen = fmt.Errorf("->newDataSource-->open数据库打开失败:%w", errSQLOpen) errSQLOpen = fmt.Errorf("->newDataSource-->open数据库打开失败:%w", errSQLOpen)
FuncLogError(nil, errSQLOpen) //FuncLogError(nil, errSQLOpen)
return nil, errSQLOpen return nil, errSQLOpen
} }
} else { // 使用已经存在的数据库连接 } else { // 使用已经存在的数据库连接
@@ -93,11 +115,11 @@ func newDataSource(config *DataSourceConfig) (*dataSource, error) {
db.SetConnMaxLifetime(time.Second * time.Duration(config.ConnMaxLifetimeSecond)) db.SetConnMaxLifetime(time.Second * time.Duration(config.ConnMaxLifetimeSecond))
// 验证连接 // 验证连接
if pingerr := db.Ping(); pingerr != nil { if err := db.Ping(); err != nil {
pingerr = fmt.Errorf("->newDataSource-->ping数据库失败:%w", pingerr) err = fmt.Errorf("->newDataSource-->ping数据库失败:%w", err)
FuncLogError(nil, pingerr) //FuncLogError(nil, err)
db.Close() db.Close()
return nil, pingerr return nil, err
} }
return &dataSource{db}, nil return &dataSource{db}, nil