feat 调整 mysql 、pgsql 初始化数据; 完善pgsql 兼容性,适配代码生成逻辑

This commit is contained in:
孟帅
2025-11-08 12:40:03 +08:00
parent 3989996448
commit a7234bc330
49 changed files with 5360 additions and 5201 deletions

View File

@@ -1,8 +1,12 @@
package db
import (
"hotgo/internal/consts"
"strings"
"unicode"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/text/gstr"
)
// 判断字符是否为字母、数字或下划线
@@ -26,4 +30,45 @@ func getLastWord(text string) string {
return fields[len(fields)-1]
}
return ""
}
}
// GetQuoteChar 获取数据库对应的引号字符
// MySQL使用反引号 `PostgreSQL使用双引号 "
func GetQuoteChar() string {
db := g.DB()
if db == nil {
return "`" // 默认MySQL
}
dbType := db.GetConfig().Type
if dbType == consts.DBPgsql {
return "\""
}
return "`"
}
// QuoteIdentifier 引用标识符(表名、字段名)
// 自动去除已有的引号,避免重复引用
func QuoteIdentifier(identifier string) string {
identifier = gstr.Trim(identifier, "`\"")
q := GetQuoteChar()
return q + identifier + q
}
// QuoteField 引用完整字段(表名.字段名)
// 自动去除表名和字段名中可能已有的引号
func QuoteField(table, field string) string {
table = gstr.Trim(table, "`\"")
field = gstr.Trim(field, "`\"")
q := GetQuoteChar()
return q + table + q + "." + q + field + q
}
// QuoteFieldAs 引用字段并添加别名(表名.字段名 as 别名)
// 自动去除表名、字段名、别名中可能已有的引号
func QuoteFieldAs(table, field, alias string) string {
table = gstr.Trim(table, "`\"")
field = gstr.Trim(field, "`\"")
alias = gstr.Trim(alias, "`\"")
q := GetQuoteChar()
return q + table + q + "." + q + field + q + " as " + q + alias + q
}