Skip to content

Commit c0f6538

Browse files
authored
Merge pull request #2000 from ByteZhou-2018/m_20250318_gorm_logic
GORM初始化数据库优化,通过抽取重复的逻辑避免代码冗余。
2 parents cef6fce + 4964518 commit c0f6538

File tree

4 files changed

+32
-62
lines changed

4 files changed

+32
-62
lines changed

server/initialize/gorm_mysql.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,33 @@ import (
1212
// GormMysql 初始化Mysql数据库
1313
// Author [piexlmax](https://github.com/piexlmax)
1414
// Author [SliverHorn](https://github.com/SliverHorn)
15+
// Author [ByteZhou-2018](https://github.com/ByteZhou-2018)
1516
func GormMysql() *gorm.DB {
1617
m := global.GVA_CONFIG.Mysql
17-
if m.Dbname == "" {
18-
return nil
19-
}
20-
mysqlConfig := mysql.Config{
21-
DSN: m.Dsn(), // DSN data source name
22-
DefaultStringSize: 191, // string 类型字段的默认长度
23-
SkipInitializeWithVersion: false, // 根据版本自动配置
24-
}
25-
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
26-
return nil
27-
} else {
28-
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
29-
sqlDB, _ := db.DB()
30-
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
31-
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
32-
return db
33-
}
18+
return initMysqlDatabase(m)
3419
}
3520

36-
// GormMysqlByConfig 初始化Mysql数据库用过传入配置
21+
// GormMysqlByConfig 通过传入配置初始化Mysql数据库
3722
func GormMysqlByConfig(m config.Mysql) *gorm.DB {
23+
return initMysqlDatabase(m)
24+
}
25+
26+
// initMysqlDatabase 初始化Mysql数据库的辅助函数
27+
func initMysqlDatabase(m config.Mysql) *gorm.DB {
3828
if m.Dbname == "" {
3929
return nil
4030
}
31+
4132
mysqlConfig := mysql.Config{
4233
DSN: m.Dsn(), // DSN data source name
4334
DefaultStringSize: 191, // string 类型字段的默认长度
4435
SkipInitializeWithVersion: false, // 根据版本自动配置
4536
}
37+
4638
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
4739
panic(err)
4840
} else {
49-
db.InstanceSet("gorm:table_options", "ENGINE=InnoDB")
41+
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
5042
sqlDB, _ := db.DB()
5143
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
5244
sqlDB.SetMaxOpenConns(m.MaxOpenConns)

server/initialize/gorm_oracle.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,25 @@ import (
1515
// 如果需要Oracle库 放开import里的注释 把下方 mysql.Config 改为 oracle.Config ; mysql.New 改为 oracle.New
1616
func GormOracle() *gorm.DB {
1717
m := global.GVA_CONFIG.Oracle
18-
if m.Dbname == "" {
19-
return nil
20-
}
21-
oracleConfig := mysql.Config{
22-
DSN: m.Dsn(), // DSN data source name
23-
DefaultStringSize: 191, // string 类型字段的默认长度
24-
}
25-
if db, err := gorm.Open(mysql.New(oracleConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
26-
panic(err)
27-
} else {
28-
sqlDB, _ := db.DB()
29-
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
30-
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
31-
return db
32-
}
18+
return initOracleDatabase(m)
3319
}
3420

3521
// GormOracleByConfig 初始化Oracle数据库用过传入配置
3622
func GormOracleByConfig(m config.Oracle) *gorm.DB {
23+
return initOracleDatabase(m)
24+
}
25+
26+
// initOracleDatabase 初始化Oracle数据库的辅助函数
27+
func initOracleDatabase(m config.Oracle) *gorm.DB {
3728
if m.Dbname == "" {
3829
return nil
3930
}
31+
4032
oracleConfig := mysql.Config{
4133
DSN: m.Dsn(), // DSN data source name
4234
DefaultStringSize: 191, // string 类型字段的默认长度
4335
}
36+
4437
if db, err := gorm.Open(mysql.New(oracleConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
4538
panic(err)
4639
} else {

server/initialize/gorm_pgsql.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,16 @@ import (
1313
// Author [SliverHorn](https://github.com/SliverHorn)
1414
func GormPgSql() *gorm.DB {
1515
p := global.GVA_CONFIG.Pgsql
16-
if p.Dbname == "" {
17-
return nil
18-
}
19-
pgsqlConfig := postgres.Config{
20-
DSN: p.Dsn(), // DSN data source name
21-
PreferSimpleProtocol: false,
22-
}
23-
if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config(p.Prefix, p.Singular)); err != nil {
24-
return nil
25-
} else {
26-
sqlDB, _ := db.DB()
27-
sqlDB.SetMaxIdleConns(p.MaxIdleConns)
28-
sqlDB.SetMaxOpenConns(p.MaxOpenConns)
29-
return db
30-
}
16+
return initPgSqlDatabase(p)
3117
}
3218

33-
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数
19+
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过指定参数
3420
func GormPgSqlByConfig(p config.Pgsql) *gorm.DB {
21+
return initPgSqlDatabase(p)
22+
}
23+
24+
// initPgSqlDatabase 初始化 Postgresql 数据库的辅助函数
25+
func initPgSqlDatabase(p config.Pgsql) *gorm.DB {
3526
if p.Dbname == "" {
3627
return nil
3728
}

server/initialize/gorm_sqlite.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@ import (
1111
// GormSqlite 初始化Sqlite数据库
1212
func GormSqlite() *gorm.DB {
1313
s := global.GVA_CONFIG.Sqlite
14-
if s.Dbname == "" {
15-
return nil
16-
}
17-
18-
if db, err := gorm.Open(sqlite.Open(s.Dsn()), internal.Gorm.Config(s.Prefix, s.Singular)); err != nil {
19-
panic(err)
20-
} else {
21-
sqlDB, _ := db.DB()
22-
sqlDB.SetMaxIdleConns(s.MaxIdleConns)
23-
sqlDB.SetMaxOpenConns(s.MaxOpenConns)
24-
return db
25-
}
14+
return initSqliteDatabase(s)
2615
}
2716

2817
// GormSqliteByConfig 初始化Sqlite数据库用过传入配置
2918
func GormSqliteByConfig(s config.Sqlite) *gorm.DB {
19+
return initSqliteDatabase(s)
20+
}
21+
22+
// initSqliteDatabase 初始化Sqlite数据库辅助函数
23+
func initSqliteDatabase(s config.Sqlite) *gorm.DB {
3024
if s.Dbname == "" {
3125
return nil
3226
}

0 commit comments

Comments
 (0)