feat(db): 添加对达梦数据库的支持
- 在应用程序中新增对达梦数据库(DM)的配置和连接支持 - 实现 SetDmDB 函数以配置达梦数据库连接 - 更新数据库操作逻辑,支持达梦特有的 SQL 语法和功能 - 在相关文件中添加达梦数据库的处理逻辑,包括表创建、数据插入和查询 - 更新 go.mod 和 go.sum 文件以引入达梦数据库驱动 - 增强文档,详细说明达梦数据库的配置和使用方法
This commit is contained in:
+10
-26
@@ -83,41 +83,33 @@ func (p *IdentifierProcessor) ProcessTableNameNoPrefix(name string) string {
|
||||
|
||||
// ProcessColumn 处理 table.column 格式
|
||||
// 输入: "name" 或 "order.name" 或 "`order`.name" 或 "`order`.`name`"
|
||||
// 输出: "`name`" 或 "app_order.name"
|
||||
// 注意: 单独的列名加引号(避免关键字冲突),table.column 格式不加引号
|
||||
// 输出: "`name`" 或 "`app_order`.`name`" (MySQL) / "\"app_order\".\"name\"" (DM/PG)
|
||||
func (p *IdentifierProcessor) ProcessColumn(name string) string {
|
||||
// 检查是否包含点号
|
||||
if !strings.Contains(name, ".") {
|
||||
// 单独的列名,需要加引号(避免关键字冲突)
|
||||
return p.dialect.QuoteIdentifier(p.stripQuotes(name))
|
||||
}
|
||||
|
||||
// 处理 table.column 格式,不加引号,只添加前缀
|
||||
parts := p.splitTableColumn(name)
|
||||
if len(parts) == 2 {
|
||||
tableName := p.stripQuotes(parts[0])
|
||||
columnName := p.stripQuotes(parts[1])
|
||||
// table.column 格式不加反引号
|
||||
return p.prefix + tableName + "." + columnName
|
||||
return p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(columnName)
|
||||
}
|
||||
|
||||
// 无法解析,返回原样但转换引号
|
||||
return p.convertQuotes(name)
|
||||
}
|
||||
|
||||
// ProcessColumnNoPrefix 处理 table.column 格式(不添加前缀)
|
||||
func (p *IdentifierProcessor) ProcessColumnNoPrefix(name string) string {
|
||||
if !strings.Contains(name, ".") {
|
||||
// 单独的列名,需要加引号(避免关键字冲突)
|
||||
return p.dialect.QuoteIdentifier(p.stripQuotes(name))
|
||||
}
|
||||
|
||||
// table.column 格式不加引号
|
||||
parts := p.splitTableColumn(name)
|
||||
if len(parts) == 2 {
|
||||
tableName := p.stripQuotes(parts[0])
|
||||
columnName := p.stripQuotes(parts[1])
|
||||
return tableName + "." + columnName
|
||||
return p.dialect.QuoteIdentifier(tableName) + "." + p.dialect.QuoteIdentifier(columnName)
|
||||
}
|
||||
|
||||
return p.convertQuotes(name)
|
||||
@@ -125,9 +117,8 @@ func (p *IdentifierProcessor) ProcessColumnNoPrefix(name string) string {
|
||||
|
||||
// ProcessConditionString 智能解析条件字符串(如 ON 条件)
|
||||
// 输入: "user.id = order.user_id AND order.status = 1"
|
||||
// 输出: "app_user.id = app_order.user_id AND app_order.status = 1"
|
||||
// 注意: table.column 格式不加反引号,因为 MySQL/SQLite/PostgreSQL 都能正确解析
|
||||
// 这样可以保持返回的列名与原始列名一致,便于聚合函数等场景读取结果
|
||||
// 输出: "`app_user`.`id` = `app_order`.`user_id` AND `app_order`.`status` = 1" (MySQL)
|
||||
// 输出: "\"app_user\".\"id\" = \"app_order\".\"user_id\" AND \"app_order\".\"status\" = 1" (DM/PG)
|
||||
func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
if condition == "" {
|
||||
return condition
|
||||
@@ -136,28 +127,24 @@ func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
result := condition
|
||||
|
||||
// 首先处理已有完整引号的情况 `table`.`column` 或 "table"."column"
|
||||
// 去除引号,只添加前缀
|
||||
fullyQuotedPattern := regexp.MustCompile("[`\"]([a-zA-Z_][a-zA-Z0-9_]*)[`\"]\\.[`\"]([a-zA-Z_][a-zA-Z0-9_]*)[`\"]")
|
||||
result = fullyQuotedPattern.ReplaceAllStringFunc(result, func(match string) string {
|
||||
parts := fullyQuotedPattern.FindStringSubmatch(match)
|
||||
if len(parts) == 3 {
|
||||
tableName := parts[1]
|
||||
colName := parts[2]
|
||||
// table.column 格式不加反引号,只添加前缀
|
||||
return p.prefix + tableName + "." + colName
|
||||
return p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(colName)
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
// 然后处理部分引号的情况 `table`.column 或 "table".column
|
||||
// 去除引号,只添加前缀
|
||||
quotedTablePattern := regexp.MustCompile("[`\"]([a-zA-Z_][a-zA-Z0-9_]*)[`\"]\\.([a-zA-Z_][a-zA-Z0-9_]*)(?:[^`\"]|$)")
|
||||
result = quotedTablePattern.ReplaceAllStringFunc(result, func(match string) string {
|
||||
parts := quotedTablePattern.FindStringSubmatch(match)
|
||||
if len(parts) >= 3 {
|
||||
tableName := parts[1]
|
||||
colName := parts[2]
|
||||
// 保留末尾字符(如果有)
|
||||
suffix := ""
|
||||
if len(match) > len(parts[0])-1 {
|
||||
lastChar := match[len(match)-1]
|
||||
@@ -165,24 +152,21 @@ func (p *IdentifierProcessor) ProcessConditionString(condition string) string {
|
||||
suffix = string(lastChar)
|
||||
}
|
||||
}
|
||||
// table.column 格式不加反引号,只添加前缀
|
||||
return p.prefix + tableName + "." + colName + suffix
|
||||
return p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(colName) + suffix
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
// 最后处理无引号的情况 table.column
|
||||
// 使用更精确的正则,确保不匹配已处理的内容
|
||||
unquotedPattern := regexp.MustCompile(`([^` + "`" + `"\w]|^)([a-zA-Z_][a-zA-Z0-9_]*)\.([a-zA-Z_][a-zA-Z0-9_]*)([^` + "`" + `"\w(]|$)`)
|
||||
result = unquotedPattern.ReplaceAllStringFunc(result, func(match string) string {
|
||||
parts := unquotedPattern.FindStringSubmatch(match)
|
||||
if len(parts) >= 5 {
|
||||
prefix := parts[1] // 前面的边界字符
|
||||
prefix := parts[1]
|
||||
tableName := parts[2]
|
||||
colName := parts[3]
|
||||
suffix := parts[4] // 后面的边界字符
|
||||
// table.column 格式不加反引号,只添加前缀
|
||||
return prefix + p.prefix + tableName + "." + colName + suffix
|
||||
suffix := parts[4]
|
||||
return prefix + p.dialect.QuoteIdentifier(p.prefix+tableName) + "." + p.dialect.QuoteIdentifier(colName) + suffix
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user