|
|
@@ -30,87 +30,97 @@ var (
|
|
30
|
30
|
|
|
31
|
31
|
// CreateDBFactory 获取数据库工厂单例
|
|
32
|
32
|
func CreateDBFactory(cfg config.IConfig) *DBFactory {
|
|
|
33
|
+
|
|
33
|
34
|
config := cfg.GetDatabaseConfig()
|
|
34
|
35
|
instanceDBOnce.Do(func() {
|
|
35
|
36
|
|
|
36
|
|
- if config == nil {
|
|
37
|
|
- log.Fatal("配置未初始化,请先在yaml进行配置")
|
|
38
|
|
- }
|
|
|
37
|
+ instanceDB, initErrDB = createDBFactoryNew(config)
|
|
39
|
38
|
|
|
40
|
|
- // 设置默认值
|
|
41
|
|
- if config.MaxOpenConns == 0 {
|
|
42
|
|
- config.MaxOpenConns = 100
|
|
43
|
|
- }
|
|
44
|
|
- if config.MaxIdleConns == 0 {
|
|
45
|
|
- config.MaxIdleConns = 10
|
|
46
|
|
- }
|
|
47
|
|
- if config.ConnMaxLifetime == 0 {
|
|
48
|
|
- config.ConnMaxLifetime = 5 * 60 // 5分钟,单位秒
|
|
49
|
|
- }
|
|
|
39
|
+ })
|
|
50
|
40
|
|
|
51
|
|
- // 验证配置
|
|
52
|
|
- if config.Type == "" {
|
|
53
|
|
- initErrDB = fmt.Errorf("database type must be configured")
|
|
54
|
|
- return
|
|
55
|
|
- }
|
|
56
|
|
- if config.Host == "" {
|
|
57
|
|
- initErrDB = fmt.Errorf("database host must be configured")
|
|
58
|
|
- return
|
|
59
|
|
- }
|
|
60
|
|
- if config.Database == "" {
|
|
61
|
|
- initErrDB = fmt.Errorf("database name must be configured")
|
|
62
|
|
- return
|
|
63
|
|
- }
|
|
|
41
|
+ if initErrDB != nil {
|
|
|
42
|
+ log.Fatalf("DBFactory is error: '%v'", initErrDB)
|
|
|
43
|
+ }
|
|
64
|
44
|
|
|
65
|
|
- log.Printf("Creating database connection...")
|
|
|
45
|
+ return instanceDB
|
|
|
46
|
+}
|
|
66
|
47
|
|
|
67
|
|
- // 获取对应的驱动
|
|
68
|
|
- dbDriver, err := drivers.Get(config.Type)
|
|
69
|
|
- if err != nil {
|
|
70
|
|
- initErrDB = fmt.Errorf("failed to get database driver: %v", err)
|
|
71
|
|
- return
|
|
72
|
|
- }
|
|
|
48
|
+// createDBFactoryNew 获取数据库工厂单例
|
|
|
49
|
+func createDBFactoryNew(config *subconfigs.DatabaseConfig) (*DBFactory, error) {
|
|
73
|
50
|
|
|
74
|
|
- // 将内部 DBConfig 转换为 drivers.DBConfig
|
|
75
|
|
- driverConfig := drivers.DBConfig{
|
|
76
|
|
- Type: config.Type,
|
|
77
|
|
- Host: config.Host,
|
|
78
|
|
- Port: config.Port,
|
|
79
|
|
- Username: config.Username,
|
|
80
|
|
- Password: config.Password,
|
|
81
|
|
- Database: config.Database,
|
|
82
|
|
- MaxOpenConns: config.MaxOpenConns,
|
|
83
|
|
- MaxIdleConns: config.MaxIdleConns,
|
|
84
|
|
- ConnMaxLifetime: config.ConnMaxLifetime,
|
|
85
|
|
- }
|
|
|
51
|
+ if config == nil {
|
|
|
52
|
+ log.Fatal("配置未初始化,请先在yaml进行配置")
|
|
|
53
|
+ }
|
|
86
|
54
|
|
|
87
|
|
- // 创建数据库连接
|
|
88
|
|
- db, err := dbDriver.Open(driverConfig)
|
|
89
|
|
- if err != nil {
|
|
90
|
|
- initErrDB = fmt.Errorf("failed to open database connection: %v", err)
|
|
91
|
|
- return
|
|
92
|
|
- }
|
|
|
55
|
+ // 设置默认值
|
|
|
56
|
+ if config.MaxOpenConns == 0 {
|
|
|
57
|
+ config.MaxOpenConns = 100
|
|
|
58
|
+ }
|
|
|
59
|
+ if config.MaxIdleConns == 0 {
|
|
|
60
|
+ config.MaxIdleConns = 10
|
|
|
61
|
+ }
|
|
|
62
|
+ if config.ConnMaxLifetime == 0 {
|
|
|
63
|
+ config.ConnMaxLifetime = 5 * 60 // 5分钟,单位秒
|
|
|
64
|
+ }
|
|
93
|
65
|
|
|
94
|
|
- // 测试连接
|
|
95
|
|
- if err := functions.TestConnection(db, config.Type); err != nil {
|
|
96
|
|
- db.Close()
|
|
97
|
|
- initErrDB = fmt.Errorf("database connection test failed: %v", err)
|
|
98
|
|
- return
|
|
99
|
|
- }
|
|
|
66
|
+ // 验证配置
|
|
|
67
|
+ if config.Type == "" {
|
|
|
68
|
+ initErrDB = fmt.Errorf("database type must be configured")
|
|
|
69
|
+ return nil, initErrDB
|
|
|
70
|
+ }
|
|
|
71
|
+ if config.Host == "" {
|
|
|
72
|
+ initErrDB = fmt.Errorf("database host must be configured")
|
|
|
73
|
+ return nil, initErrDB
|
|
|
74
|
+ }
|
|
|
75
|
+ if config.Database == "" {
|
|
|
76
|
+ initErrDB = fmt.Errorf("database name must be configured")
|
|
|
77
|
+ return nil, initErrDB
|
|
|
78
|
+ }
|
|
100
|
79
|
|
|
101
|
|
- log.Printf("DBFactory is successfully created.\n")
|
|
|
80
|
+ log.Printf("Creating database connection...")
|
|
102
|
81
|
|
|
103
|
|
- instanceDB = &DBFactory{
|
|
104
|
|
- db: db,
|
|
105
|
|
- config: config,
|
|
106
|
|
- }
|
|
107
|
|
- })
|
|
|
82
|
+ // 获取对应的驱动
|
|
|
83
|
+ dbDriver, err := drivers.Get(config.Type)
|
|
|
84
|
+ if err != nil {
|
|
|
85
|
+ initErrDB = fmt.Errorf("failed to get database driver: %v", err)
|
|
|
86
|
+ return nil, initErrDB
|
|
|
87
|
+ }
|
|
108
|
88
|
|
|
109
|
|
- if initErrDB != nil {
|
|
110
|
|
- log.Fatalf("DBFactory is error: '%v'", initErrDB)
|
|
|
89
|
+ // 将内部 DBConfig 转换为 drivers.DBConfig
|
|
|
90
|
+ driverConfig := drivers.DBConfig{
|
|
|
91
|
+ Type: config.Type,
|
|
|
92
|
+ Host: config.Host,
|
|
|
93
|
+ Port: config.Port,
|
|
|
94
|
+ Username: config.Username,
|
|
|
95
|
+ Password: config.Password,
|
|
|
96
|
+ Database: config.Database,
|
|
|
97
|
+ MaxOpenConns: config.MaxOpenConns,
|
|
|
98
|
+ MaxIdleConns: config.MaxIdleConns,
|
|
|
99
|
+ ConnMaxLifetime: config.ConnMaxLifetime,
|
|
111
|
100
|
}
|
|
112
|
101
|
|
|
113
|
|
- return instanceDB
|
|
|
102
|
+ // 创建数据库连接
|
|
|
103
|
+ db, err := dbDriver.Open(driverConfig)
|
|
|
104
|
+ if err != nil {
|
|
|
105
|
+ initErrDB = fmt.Errorf("failed to open database connection: %v", err)
|
|
|
106
|
+ return nil, initErrDB
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ // 测试连接
|
|
|
110
|
+ if err := functions.TestConnection(db, config.Type); err != nil {
|
|
|
111
|
+ db.Close()
|
|
|
112
|
+ initErrDB = fmt.Errorf("database connection test failed: %v", err)
|
|
|
113
|
+ return nil, initErrDB
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ log.Printf("DBFactory is successfully created.\n")
|
|
|
117
|
+
|
|
|
118
|
+ instanceDB = &DBFactory{
|
|
|
119
|
+ db: db,
|
|
|
120
|
+ config: config,
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ return instanceDB, initErrDB
|
|
114
|
124
|
}
|
|
115
|
125
|
|
|
116
|
126
|
// ========== DBFactory 实例方法 ==========
|