feat(db): 添加对达梦数据库的支持
- 在应用程序中新增对达梦数据库(DM)的配置和连接支持 - 实现 SetDmDB 函数以配置达梦数据库连接 - 更新数据库操作逻辑,支持达梦特有的 SQL 语法和功能 - 在相关文件中添加达梦数据库的处理逻辑,包括表创建、数据插入和查询 - 更新 go.mod 和 go.sum 文件以引入达梦数据库驱动 - 增强文档,详细说明达梦数据库的配置和使用方法
This commit is contained in:
+62
-6
@@ -285,13 +285,21 @@ func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64 {
|
||||
|
||||
query := "INSERT INTO " + processor.ProcessTableName(table) + " " + queryString + "VALUES" + valueString
|
||||
|
||||
res, err := that.Exec(query, values...)
|
||||
|
||||
id := int64(0)
|
||||
if err.GetError() == nil && res != nil {
|
||||
id1, err := res.LastInsertId()
|
||||
that.LastErr.SetError(err)
|
||||
id = id1
|
||||
if that.Dialect != nil && !that.Dialect.SupportsLastInsertId() {
|
||||
returningClause := that.Dialect.ReturningClause("id")
|
||||
query = strings.TrimSuffix(query, ";") + returningClause
|
||||
rows := that.Query(query, values...)
|
||||
if len(rows) > 0 {
|
||||
id = rows[0].GetInt64("id")
|
||||
}
|
||||
} else {
|
||||
res, err := that.Exec(query, values...)
|
||||
if err.GetError() == nil && res != nil {
|
||||
id1, e := res.LastInsertId()
|
||||
that.LastErr.SetError(e)
|
||||
id = id1
|
||||
}
|
||||
}
|
||||
|
||||
// 如果插入成功,删除缓存
|
||||
@@ -475,6 +483,8 @@ func (that *HoTimeDB) Upsert(table string, data Map, uniqueKeys Slice, updateCol
|
||||
query = that.buildPostgresUpsert(table, columns, uniqueKeyStrs, updateColumnStrs, rawValues)
|
||||
case "sqlite3", "sqlite":
|
||||
query = that.buildSQLiteUpsert(table, columns, uniqueKeyStrs, updateColumnStrs, rawValues)
|
||||
case "dm", "dameng":
|
||||
query = that.buildDMUpsert(table, columns, uniqueKeyStrs, updateColumnStrs, rawValues)
|
||||
default: // mysql
|
||||
query = that.buildMySQLUpsert(table, columns, uniqueKeyStrs, updateColumnStrs, rawValues)
|
||||
}
|
||||
@@ -608,6 +618,52 @@ func (that *HoTimeDB) buildSQLiteUpsert(table string, columns []string, uniqueKe
|
||||
") DO UPDATE SET " + strings.Join(updateParts, ", ")
|
||||
}
|
||||
|
||||
// buildDMUpsert 构建达梦的 Upsert 语句(MERGE INTO)
|
||||
func (that *HoTimeDB) buildDMUpsert(table string, columns []string, uniqueKeys []string, updateColumns []string, rawValues map[string]string) string {
|
||||
processor := that.GetProcessor()
|
||||
dialect := that.GetDialect()
|
||||
quotedTable := processor.ProcessTableName(table)
|
||||
|
||||
srcParts := make([]string, len(columns))
|
||||
for i, col := range columns {
|
||||
if raw, ok := rawValues[col]; ok {
|
||||
srcParts[i] = raw + " AS " + dialect.QuoteIdentifier(col)
|
||||
} else {
|
||||
srcParts[i] = "? AS " + dialect.QuoteIdentifier(col)
|
||||
}
|
||||
}
|
||||
|
||||
onParts := make([]string, len(uniqueKeys))
|
||||
for i, key := range uniqueKeys {
|
||||
qk := dialect.QuoteIdentifier(key)
|
||||
onParts[i] = quotedTable + "." + qk + " = src." + qk
|
||||
}
|
||||
|
||||
updateParts := make([]string, len(updateColumns))
|
||||
for i, col := range updateColumns {
|
||||
qc := dialect.QuoteIdentifier(col)
|
||||
if raw, ok := rawValues[col]; ok {
|
||||
updateParts[i] = qc + " = " + raw
|
||||
} else {
|
||||
updateParts[i] = qc + " = src." + qc
|
||||
}
|
||||
}
|
||||
|
||||
insertCols := make([]string, len(columns))
|
||||
insertVals := make([]string, len(columns))
|
||||
for i, col := range columns {
|
||||
qc := dialect.QuoteIdentifier(col)
|
||||
insertCols[i] = qc
|
||||
insertVals[i] = "src." + qc
|
||||
}
|
||||
|
||||
return "MERGE INTO " + quotedTable + " USING (SELECT " + strings.Join(srcParts, ", ") +
|
||||
") src ON (" + strings.Join(onParts, " AND ") +
|
||||
") WHEN MATCHED THEN UPDATE SET " + strings.Join(updateParts, ", ") +
|
||||
" WHEN NOT MATCHED THEN INSERT (" + strings.Join(insertCols, ", ") +
|
||||
") VALUES (" + strings.Join(insertVals, ", ") + ")"
|
||||
}
|
||||
|
||||
// Update 更新数据
|
||||
func (that *HoTimeDB) Update(table string, data Map, where Map) int64 {
|
||||
processor := that.GetProcessor()
|
||||
|
||||
Reference in New Issue
Block a user