|
|
@@ -4,92 +4,39 @@ import (
|
|
4
|
4
|
"database/sql"
|
|
5
|
5
|
"fmt"
|
|
6
|
6
|
"io"
|
|
7
|
|
- "os"
|
|
8
|
|
- "path/filepath"
|
|
9
|
7
|
|
|
|
8
|
+ "git.x2erp.com/qdy/go-base/config"
|
|
10
|
9
|
"git.x2erp.com/qdy/go-base/types"
|
|
11
|
10
|
"git.x2erp.com/qdy/go-db/drivers"
|
|
12
|
|
- "gopkg.in/yaml.v2"
|
|
13
|
11
|
)
|
|
14
|
12
|
|
|
15
|
|
-// Config 总配置
|
|
16
|
|
-type Config struct {
|
|
17
|
|
- Database drivers.DBConfig `yaml:"database"`
|
|
18
|
|
-}
|
|
19
|
|
-
|
|
20
|
13
|
// DBFactory 数据库工厂
|
|
21
|
14
|
type DBFactory struct {
|
|
22
|
|
- config *Config
|
|
|
15
|
+ config config.IConfig
|
|
23
|
16
|
}
|
|
24
|
17
|
|
|
25
|
18
|
// NewDBFactory 创建数据库工厂
|
|
26
|
19
|
func NewDBFactory() (*DBFactory, error) {
|
|
27
|
|
- configFile, err := findConfigFile()
|
|
28
|
|
- if err != nil {
|
|
29
|
|
- return nil, err
|
|
30
|
|
- }
|
|
31
|
|
-
|
|
32
|
|
- fmt.Printf("✅ Using config file: %s\n", configFile)
|
|
33
|
|
-
|
|
34
|
|
- // 读取配置文件
|
|
35
|
|
- data, err := os.ReadFile(configFile)
|
|
36
|
|
- if err != nil {
|
|
37
|
|
- return nil, fmt.Errorf("failed to read config file %s: %v", configFile, err)
|
|
38
|
|
- }
|
|
39
|
|
-
|
|
40
|
|
- var config Config
|
|
41
|
|
- err = yaml.Unmarshal(data, &config)
|
|
42
|
|
- if err != nil {
|
|
43
|
|
- return nil, fmt.Errorf("failed to parse config file: %v", err)
|
|
44
|
|
- }
|
|
45
|
|
-
|
|
46
|
|
- return &DBFactory{config: &config}, nil
|
|
47
|
|
-}
|
|
48
|
|
-
|
|
49
|
|
-// findConfigFile 查找配置文件
|
|
50
|
|
-func findConfigFile() (string, error) {
|
|
51
|
|
- // 1. 首先尝试可执行文件同目录
|
|
52
|
|
- exePath, err := os.Executable()
|
|
53
|
|
- if err == nil {
|
|
54
|
|
- exeDir := filepath.Dir(exePath)
|
|
55
|
|
- configFile := filepath.Join(exeDir, "db.yaml")
|
|
56
|
|
- if _, err := os.Stat(configFile); err == nil {
|
|
57
|
|
- return configFile, nil
|
|
58
|
|
- }
|
|
59
|
|
- }
|
|
|
20
|
+ // 使用配置单例
|
|
|
21
|
+ cfg := config.GetConfig()
|
|
60
|
22
|
|
|
61
|
|
- // 2. 尝试环境变量指定的路径
|
|
62
|
|
- envConfigPath := os.Getenv("DB_CONFIG_PATH")
|
|
63
|
|
- if envConfigPath != "" {
|
|
64
|
|
- if _, err := os.Stat(envConfigPath); err == nil {
|
|
65
|
|
- return envConfigPath, nil
|
|
66
|
|
- }
|
|
67
|
|
- return "", fmt.Errorf("DB_CONFIG_PATH file not found: %s", envConfigPath)
|
|
|
23
|
+ // 检查配置初始化是否有错误
|
|
|
24
|
+ if err := config.GetInitError(); err != nil {
|
|
|
25
|
+ return nil, fmt.Errorf("failed to load config: %v", err)
|
|
68
|
26
|
}
|
|
69
|
27
|
|
|
70
|
|
- // 3. 如果都没有找到,返回错误
|
|
71
|
|
- exeDir := "unknown"
|
|
72
|
|
- if exePath, err := os.Executable(); err == nil {
|
|
73
|
|
- exeDir = filepath.Dir(exePath)
|
|
|
28
|
+ // 检查数据库配置是否完整
|
|
|
29
|
+ if !cfg.IsDatabaseConfigured() {
|
|
|
30
|
+ return nil, fmt.Errorf("database configuration is incomplete")
|
|
74
|
31
|
}
|
|
75
|
32
|
|
|
76
|
|
- return "", fmt.Errorf(`No configuration file found!
|
|
77
|
|
-
|
|
78
|
|
-Tried locations:
|
|
79
|
|
-1. Executable directory: %s/db.yaml
|
|
80
|
|
-2. Environment variable: DB_CONFIG_PATH
|
|
81
|
|
-
|
|
82
|
|
-Solutions:
|
|
83
|
|
-- Place db.yaml in the same directory as the executable
|
|
84
|
|
-- Or set DB_CONFIG_PATH environment variable to config file path
|
|
85
|
|
-
|
|
86
|
|
-Example:
|
|
87
|
|
- export DB_CONFIG_PATH=/path/to/your/db.yaml`, exeDir)
|
|
|
33
|
+ return &DBFactory{config: cfg}, nil
|
|
88
|
34
|
}
|
|
89
|
35
|
|
|
90
|
36
|
// CreateDB 创建数据库连接
|
|
91
|
37
|
func (f *DBFactory) CreateDB() (*sql.DB, error) {
|
|
92
|
|
- dbType := f.config.Database.Type
|
|
|
38
|
+ dbConfig := f.config.GetDatabase()
|
|
|
39
|
+ dbType := dbConfig.Type
|
|
93
|
40
|
|
|
94
|
41
|
// 获取对应的驱动
|
|
95
|
42
|
dbDriver, err := drivers.Get(dbType)
|
|
|
@@ -97,8 +44,21 @@ func (f *DBFactory) CreateDB() (*sql.DB, error) {
|
|
97
|
44
|
return nil, fmt.Errorf("failed to get database driver: %v", err)
|
|
98
|
45
|
}
|
|
99
|
46
|
|
|
|
47
|
+ // 将内部 DBConfig 转换为 drivers.DBConfig
|
|
|
48
|
+ driverConfig := drivers.DBConfig{
|
|
|
49
|
+ Type: dbConfig.Type,
|
|
|
50
|
+ Host: dbConfig.Host,
|
|
|
51
|
+ Port: dbConfig.Port,
|
|
|
52
|
+ Username: dbConfig.Username,
|
|
|
53
|
+ Password: dbConfig.Password,
|
|
|
54
|
+ Database: dbConfig.Database,
|
|
|
55
|
+ MaxOpenConns: dbConfig.MaxOpenConns,
|
|
|
56
|
+ MaxIdleConns: dbConfig.MaxIdleConns,
|
|
|
57
|
+ ConnMaxLifetime: dbConfig.ConnMaxLifetime,
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
100
|
60
|
// 创建数据库连接
|
|
101
|
|
- db, err := dbDriver.Open(f.config.Database)
|
|
|
61
|
+ db, err := dbDriver.Open(driverConfig)
|
|
102
|
62
|
if err != nil {
|
|
103
|
63
|
return nil, fmt.Errorf("failed to open database connection: %v", err)
|
|
104
|
64
|
}
|
|
|
@@ -107,7 +67,7 @@ func (f *DBFactory) CreateDB() (*sql.DB, error) {
|
|
107
|
67
|
}
|
|
108
|
68
|
|
|
109
|
69
|
// GetConfig 获取配置信息
|
|
110
|
|
-func (f *DBFactory) GetConfig() *Config {
|
|
|
70
|
+func (f *DBFactory) GetConfig() config.IConfig {
|
|
111
|
71
|
return f.config
|
|
112
|
72
|
}
|
|
113
|
73
|
|
|
|
@@ -116,7 +76,7 @@ func (f *DBFactory) GetAvailableDrivers() []string {
|
|
116
|
76
|
return drivers.GetAllDrivers()
|
|
117
|
77
|
}
|
|
118
|
78
|
|
|
119
|
|
-// CreateQueryExecutor 创建查询执行器(新增方法)
|
|
|
79
|
+// CreateQueryExecutor 创建查询执行器
|
|
120
|
80
|
func (f *DBFactory) CreateQueryExecutor(db *sql.DB) *QueryExecutor {
|
|
121
|
81
|
return NewQueryExecutor(db)
|
|
122
|
82
|
}
|
|
|
@@ -125,7 +85,6 @@ func (f *DBFactory) CreateQueryExecutor(db *sql.DB) *QueryExecutor {
|
|
125
|
85
|
// NewDBQuery 初始化查询实例(对外提供唯一初始化入口)
|
|
126
|
86
|
// db: 已初始化的数据库连接(由调用方传入,解耦数据库配置)
|
|
127
|
87
|
func NewDBQuery(db *sql.DB) *QueryExecutor {
|
|
128
|
|
- // 直接复用原文件的构造函数,对外隐藏实现细节
|
|
129
|
88
|
return NewQueryExecutor(db)
|
|
130
|
89
|
}
|
|
131
|
90
|
|