|
|
@@ -10,8 +10,8 @@ import (
|
|
10
|
10
|
"git.x2erp.com/qdy/go-base/ctx"
|
|
11
|
11
|
"git.x2erp.com/qdy/go-base/logger"
|
|
12
|
12
|
"git.x2erp.com/qdy/go-base/model/response"
|
|
13
|
|
- "git.x2erp.com/qdy/go-db/drivers"
|
|
14
|
|
- "git.x2erp.com/qdy/go-db/functions"
|
|
|
13
|
+ "git.x2erp.com/qdy/go-db/driver"
|
|
|
14
|
+ "git.x2erp.com/qdy/go-db/function"
|
|
15
|
15
|
|
|
16
|
16
|
"github.com/jmoiron/sqlx"
|
|
17
|
17
|
)
|
|
|
@@ -80,14 +80,14 @@ func createDBFactoryNew(config *subconfigs.DatabaseConfig) (*DBFactory, error) {
|
|
80
|
80
|
log.Printf("Creating database connection...")
|
|
81
|
81
|
|
|
82
|
82
|
// 获取对应的驱动
|
|
83
|
|
- dbDriver, err := drivers.Get(config.Type)
|
|
|
83
|
+ dbDriver, err := driver.Get(config.Type)
|
|
84
|
84
|
if err != nil {
|
|
85
|
85
|
initErrDB = fmt.Errorf("failed to get database driver: %v", err)
|
|
86
|
86
|
return nil, initErrDB
|
|
87
|
87
|
}
|
|
88
|
88
|
|
|
89
|
89
|
// 将内部 DBConfig 转换为 drivers.DBConfig
|
|
90
|
|
- driverConfig := drivers.DBConfig{
|
|
|
90
|
+ driverConfig := driver.DBConfig{
|
|
91
|
91
|
Type: config.Type,
|
|
92
|
92
|
Host: config.Host,
|
|
93
|
93
|
Port: config.Port,
|
|
|
@@ -107,7 +107,7 @@ func createDBFactoryNew(config *subconfigs.DatabaseConfig) (*DBFactory, error) {
|
|
107
|
107
|
}
|
|
108
|
108
|
|
|
109
|
109
|
// 测试连接
|
|
110
|
|
- if err := functions.TestConnection(db, config.Type); err != nil {
|
|
|
110
|
+ if err := function.TestConnection(db, config.Type); err != nil {
|
|
111
|
111
|
db.Close()
|
|
112
|
112
|
initErrDB = fmt.Errorf("database connection test failed: %v", err)
|
|
113
|
113
|
return nil, initErrDB
|
|
|
@@ -153,54 +153,54 @@ func (f *DBFactory) GetConfig() subconfigs.DatabaseConfig {
|
|
153
|
153
|
|
|
154
|
154
|
// TestConnection 测试连接
|
|
155
|
155
|
func (f *DBFactory) TestConnection() error {
|
|
156
|
|
- return functions.TestConnection(f.db, f.config.Type)
|
|
|
156
|
+ return function.TestConnection(f.db, f.config.Type)
|
|
157
|
157
|
}
|
|
158
|
158
|
|
|
159
|
159
|
// ========== 快捷操作方法 ==========
|
|
160
|
160
|
|
|
161
|
161
|
// QueryToJSON 快捷查询,直接返回 JSON 字节流
|
|
162
|
162
|
func (f *DBFactory) QueryToJSON(sql string, reqCtx *ctx.RequestContext) *response.QueryResult[[]map[string]interface{}] {
|
|
163
|
|
- return functions.QueryToJSON(f.db, sql, reqCtx)
|
|
|
163
|
+ return function.QueryToJSON(f.db, sql, reqCtx)
|
|
164
|
164
|
}
|
|
165
|
165
|
|
|
166
|
166
|
// QueryPositionalToJSON 位置参数查询并返回 JSON 字节数据
|
|
167
|
167
|
func (f *DBFactory) QueryPositionalToJSON(sql string, params []interface{}, reqCtx *ctx.RequestContext) *response.QueryResult[[]map[string]interface{}] {
|
|
168
|
|
- return functions.QueryPositionalToJSON(f.db, sql, params, reqCtx)
|
|
|
168
|
+ return function.QueryPositionalToJSON(f.db, sql, params, reqCtx)
|
|
169
|
169
|
}
|
|
170
|
170
|
|
|
171
|
171
|
// QueryParamsNameToJSON 命名参数查询并返回 JSON 字节数据
|
|
172
|
172
|
func (f *DBFactory) QueryParamsNameToJSON(sql string, params map[string]interface{}, reqCtx *ctx.RequestContext) *response.QueryResult[[]map[string]interface{}] {
|
|
173
|
|
- return functions.QueryParamsNameToJSON(f.db, sql, params, reqCtx)
|
|
|
173
|
+ return function.QueryParamsNameToJSON(f.db, sql, params, reqCtx)
|
|
174
|
174
|
}
|
|
175
|
175
|
|
|
176
|
176
|
// QueryToCSV 快捷查询,直接返回 CSV 字符串(包含表头)
|
|
177
|
177
|
func (f *DBFactory) QueryToCSV(sql string, writerHeader bool, reqCtx *ctx.RequestContext) ([]byte, error) {
|
|
178
|
|
- return functions.QueryToCSV(f.db, sql, writerHeader, reqCtx)
|
|
|
178
|
+ return function.QueryToCSV(f.db, sql, writerHeader, reqCtx)
|
|
179
|
179
|
}
|
|
180
|
180
|
|
|
181
|
181
|
// QueryPositionalToCSV 位置参数查询并返回 CSV 字节数据
|
|
182
|
182
|
func (f *DBFactory) QueryPositionalToCSV(sql string, writerHeader bool, params []interface{}, reqCtx *ctx.RequestContext) ([]byte, error) {
|
|
183
|
|
- return functions.QueryPositionalToCSV(f.db, sql, writerHeader, params, reqCtx)
|
|
|
183
|
+ return function.QueryPositionalToCSV(f.db, sql, writerHeader, params, reqCtx)
|
|
184
|
184
|
}
|
|
185
|
185
|
|
|
186
|
186
|
// QueryParamsNameToCSV 命名参数查询并返回 CSV 字节数据
|
|
187
|
187
|
func (f *DBFactory) QueryParamsNameToCSV(sql string, writerHeader bool, params map[string]interface{}, reqCtx *ctx.RequestContext) ([]byte, error) {
|
|
188
|
|
- return functions.QueryParamsNameToCSV(f.db, sql, writerHeader, params, reqCtx)
|
|
|
188
|
+ return function.QueryParamsNameToCSV(f.db, sql, writerHeader, params, reqCtx)
|
|
189
|
189
|
}
|
|
190
|
190
|
|
|
191
|
191
|
// ExecuteDDL 快捷执行DDL语句
|
|
192
|
192
|
func (f *DBFactory) ExecuteDDL(ddlSQL string) error {
|
|
193
|
|
- return functions.ExecuteDDL(f.db, ddlSQL)
|
|
|
193
|
+ return function.ExecuteDDL(f.db, ddlSQL)
|
|
194
|
194
|
}
|
|
195
|
195
|
|
|
196
|
196
|
// ExecuteDDLWithTx 快捷在事务中执行DDL语句
|
|
197
|
197
|
func (f *DBFactory) ExecuteDDLWithTx(ddlSQL string) error {
|
|
198
|
|
- return functions.ExecuteDDLWithTx(f.db, ddlSQL)
|
|
|
198
|
+ return function.ExecuteDDLWithTx(f.db, ddlSQL)
|
|
199
|
199
|
}
|
|
200
|
200
|
|
|
201
|
201
|
// ExecuteMultipleDDL 快捷执行多个DDL语句
|
|
202
|
202
|
func (f *DBFactory) ExecuteMultipleDDL(ddlSQLs []string) error {
|
|
203
|
|
- return functions.ExecuteMultipleDDL(f.db, ddlSQLs)
|
|
|
203
|
+ return function.ExecuteMultipleDDL(f.db, ddlSQLs)
|
|
204
|
204
|
}
|
|
205
|
205
|
|
|
206
|
206
|
// GetDBType 得到当前使用数据库类型
|
|
|
@@ -240,7 +240,7 @@ func (f *DBFactory) Ping() error {
|
|
240
|
240
|
|
|
241
|
241
|
// GetAvailableDrivers 获取可用的数据库驱动
|
|
242
|
242
|
func (f *DBFactory) GetAvailableDrivers() []string {
|
|
243
|
|
- return drivers.GetAllDrivers()
|
|
|
243
|
+ return driver.GetAllDrivers()
|
|
244
|
244
|
}
|
|
245
|
245
|
|
|
246
|
246
|
// ========== 新增的简化操作方法 ==========
|