qdy il y a 3 mois
Parent
révision
fdab7515cf
6 fichiers modifiés avec 101 ajouts et 76 suppressions
  1. 9
    1
      db.yaml
  2. 29
    70
      factory/factory.go
  3. 55
    0
      gct.sh
  4. 1
    1
      go.mod
  5. 2
    0
      go.sum
  6. 5
    4
      test.go

+ 9
- 1
db.yaml Voir le fichier

1
+auth:
2
+  token: "aaaaaawww"
3
+
4
+service:
5
+  read_timeout: 30
6
+  write_timeout: 30
7
+  idle_timeout: 60
8
+
1
 database:
9
 database:
2
   type: "oracle"  # mysql, postgres, oracle, sqlserver
10
   type: "oracle"  # mysql, postgres, oracle, sqlserver
3
   host: "161.189.7.134"
11
   host: "161.189.7.134"
7
   database: "ORCL"
15
   database: "ORCL"
8
   max_open_conns: 100
16
   max_open_conns: 100
9
   max_idle_conns: 10
17
   max_idle_conns: 10
10
-  conn_max_lifetime: 300
18
+  conn_max_lifetime: 300

+ 29
- 70
factory/factory.go Voir le fichier

4
 	"database/sql"
4
 	"database/sql"
5
 	"fmt"
5
 	"fmt"
6
 	"io"
6
 	"io"
7
-	"os"
8
-	"path/filepath"
9
 
7
 
8
+	"git.x2erp.com/qdy/go-base/config"
10
 	"git.x2erp.com/qdy/go-base/types"
9
 	"git.x2erp.com/qdy/go-base/types"
11
 	"git.x2erp.com/qdy/go-db/drivers"
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
 // DBFactory 数据库工厂
13
 // DBFactory 数据库工厂
21
 type DBFactory struct {
14
 type DBFactory struct {
22
-	config *Config
15
+	config config.IConfig
23
 }
16
 }
24
 
17
 
25
 // NewDBFactory 创建数据库工厂
18
 // NewDBFactory 创建数据库工厂
26
 func NewDBFactory() (*DBFactory, error) {
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
 // CreateDB 创建数据库连接
36
 // CreateDB 创建数据库连接
91
 func (f *DBFactory) CreateDB() (*sql.DB, error) {
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
 	dbDriver, err := drivers.Get(dbType)
42
 	dbDriver, err := drivers.Get(dbType)
97
 		return nil, fmt.Errorf("failed to get database driver: %v", err)
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
 	if err != nil {
62
 	if err != nil {
103
 		return nil, fmt.Errorf("failed to open database connection: %v", err)
63
 		return nil, fmt.Errorf("failed to open database connection: %v", err)
104
 	}
64
 	}
107
 }
67
 }
108
 
68
 
109
 // GetConfig 获取配置信息
69
 // GetConfig 获取配置信息
110
-func (f *DBFactory) GetConfig() *Config {
70
+func (f *DBFactory) GetConfig() config.IConfig {
111
 	return f.config
71
 	return f.config
112
 }
72
 }
113
 
73
 
116
 	return drivers.GetAllDrivers()
76
 	return drivers.GetAllDrivers()
117
 }
77
 }
118
 
78
 
119
-// CreateQueryExecutor 创建查询执行器(新增方法)
79
+// CreateQueryExecutor 创建查询执行器
120
 func (f *DBFactory) CreateQueryExecutor(db *sql.DB) *QueryExecutor {
80
 func (f *DBFactory) CreateQueryExecutor(db *sql.DB) *QueryExecutor {
121
 	return NewQueryExecutor(db)
81
 	return NewQueryExecutor(db)
122
 }
82
 }
125
 // NewDBQuery 初始化查询实例(对外提供唯一初始化入口)
85
 // NewDBQuery 初始化查询实例(对外提供唯一初始化入口)
126
 // db: 已初始化的数据库连接(由调用方传入,解耦数据库配置)
86
 // db: 已初始化的数据库连接(由调用方传入,解耦数据库配置)
127
 func NewDBQuery(db *sql.DB) *QueryExecutor {
87
 func NewDBQuery(db *sql.DB) *QueryExecutor {
128
-	// 直接复用原文件的构造函数,对外隐藏实现细节
129
 	return NewQueryExecutor(db)
88
 	return NewQueryExecutor(db)
130
 }
89
 }
131
 
90
 

+ 55
- 0
gct.sh Voir le fichier

1
+#!/bin/bash
2
+
3
+# 脚本用法:./git-commit-and-tag.sh "你的提交描述" "版本号"
4
+
5
+# 检查参数数量
6
+if [ $# -ne 2 ]; then
7
+    echo "错误: 脚本需要2个参数。"
8
+    echo "用法: $0 \"提交描述\" \"版本号\""
9
+    echo "示例: $0 \"修复了登录问题\" \"v1.2.3\""
10
+    exit 1
11
+fi
12
+
13
+# 分配参数
14
+COMMIT_MESSAGE="$1"
15
+VERSION_TAG="$2"
16
+
17
+# 检查当前目录是否为Git仓库
18
+if ! git rev-parse --git-dir > /dev/null 2>&1; then
19
+    echo "错误: 当前目录不是一个Git仓库。"
20
+    exit 1
21
+fi
22
+
23
+# 检查是否有未暂存的更改
24
+if [ -z "$(git status --porcelain)" ]; then
25
+    echo "提示: 没有检测到任何更改需要提交。"
26
+    exit 0
27
+fi
28
+
29
+echo "开始提交并打版本标签..."
30
+echo "提交描述: $COMMIT_MESSAGE"
31
+echo "版本标签: $VERSION_TAG"
32
+
33
+# 添加所有更改到暂存区[citation:4][citation:6]
34
+git add .
35
+
36
+# 进行提交[citation:4][citation:10]
37
+git commit -m "$COMMIT_MESSAGE"
38
+if [ $? -ne 0 ]; then
39
+    echo "错误: 提交失败。"
40
+    exit 1
41
+fi
42
+
43
+# 创建轻量标签或附注标签(这里创建轻量标签)[citation:4]
44
+git tag "$VERSION_TAG"
45
+if [ $? -ne 0 ]; then
46
+    echo "错误: 创建标签失败。"
47
+    exit 1
48
+fi
49
+
50
+# 推送到远程仓库并推送标签[citation:4]
51
+echo "正在推送到远程仓库..."
52
+git push
53
+git push origin "$VERSION_TAG"
54
+
55
+echo "✅ 完成!提交已推送,版本标签 $VERSION_TAG 已创建并推送。"

+ 1
- 1
go.mod Voir le fichier

12
 
12
 
13
 require (
13
 require (
14
 	filippo.io/edwards25519 v1.1.0 // indirect
14
 	filippo.io/edwards25519 v1.1.0 // indirect
15
-	git.x2erp.com/qdy/go-base v0.1.1
15
+	git.x2erp.com/qdy/go-base v0.1.2
16
 	github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
16
 	github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
17
 	github.com/golang-sql/sqlexp v0.1.0 // indirect
17
 	github.com/golang-sql/sqlexp v0.1.0 // indirect
18
 	github.com/google/uuid v1.6.0 // indirect
18
 	github.com/google/uuid v1.6.0 // indirect

+ 2
- 0
go.sum Voir le fichier

2
 filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
2
 filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
3
 git.x2erp.com/qdy/go-base v0.1.1 h1:Yj6rFTCL9CShqdQeE6mUYQMugUpI25nWNL0i09SFtXo=
3
 git.x2erp.com/qdy/go-base v0.1.1 h1:Yj6rFTCL9CShqdQeE6mUYQMugUpI25nWNL0i09SFtXo=
4
 git.x2erp.com/qdy/go-base v0.1.1/go.mod h1:+NdHouWcxqex8sJUwDTGSl/OIh5fKOT6tJteefD1MMA=
4
 git.x2erp.com/qdy/go-base v0.1.1/go.mod h1:+NdHouWcxqex8sJUwDTGSl/OIh5fKOT6tJteefD1MMA=
5
+git.x2erp.com/qdy/go-base v0.1.2 h1:4bdkh+WYV+61/qqf1RvqtK0jPKen4BndLGEHjo2opgY=
6
+git.x2erp.com/qdy/go-base v0.1.2/go.mod h1:Q+YLwpCoU8CVSnzATLdz2LAzVMlz/CEGzo8DePf7cug=
5
 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.0 h1:Gt0j3wceWMwPmiazCa8MzMA0MfhmPIz0Qp0FJ6qcM0U=
7
 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.0 h1:Gt0j3wceWMwPmiazCa8MzMA0MfhmPIz0Qp0FJ6qcM0U=
6
 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.0/go.mod h1:Ot/6aikWnKWi4l9QB7qVSwa8iMphQNqkWALMoNT3rzM=
8
 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.18.0/go.mod h1:Ot/6aikWnKWi4l9QB7qVSwa8iMphQNqkWALMoNT3rzM=
7
 github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.10.1 h1:B+blDbyVIG3WaikNxPnhPiJ1MThR03b3vKGtER95TP4=
9
 github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.10.1 h1:B+blDbyVIG3WaikNxPnhPiJ1MThR03b3vKGtER95TP4=

+ 5
- 4
test.go Voir le fichier

21
 
21
 
22
 	// 显示当前使用的数据库配置
22
 	// 显示当前使用的数据库配置
23
 	config := dbFactory.GetConfig()
23
 	config := dbFactory.GetConfig()
24
-	fmt.Printf("Using database type: %s\n", config.Database.Type)
25
-	fmt.Printf("Database host: %s:%d\n", config.Database.Host, config.Database.Port) // 修正这里
26
-	fmt.Printf("Database name: %s\n", config.Database.Database)                      // 修正这里
24
+	dbConfig := config.GetDatabase() // 通过接口方法获取数据库配置
25
+	fmt.Printf("Using database type: %s\n", dbConfig.Type)
26
+	fmt.Printf("Database host: %s:%d\n", dbConfig.Host, dbConfig.Port)
27
+	fmt.Printf("Database name: %s\n", dbConfig.Database)
27
 
28
 
28
 	// 创建数据库连接
29
 	// 创建数据库连接
29
 	db, err := dbFactory.CreateDB()
30
 	db, err := dbFactory.CreateDB()
35
 	fmt.Println("Successfully connected to database!")
36
 	fmt.Println("Successfully connected to database!")
36
 
37
 
37
 	// 测试连接
38
 	// 测试连接
38
-	if err := testConnection(db, config.Database.Type); err != nil { // 修正这里
39
+	if err := testConnection(db, dbConfig.Type); err != nil {
39
 		log.Printf("Query test failed: %v", err)
40
 		log.Printf("Query test failed: %v", err)
40
 	} else {
41
 	} else {
41
 		fmt.Println("Database connection test passed!")
42
 		fmt.Println("Database connection test passed!")

Loading…
Annuler
Enregistrer