mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-14 05:03:49 +08:00
feat 调整 mysql 、pgsql 初始化数据; 完善pgsql 兼容性,适配代码生成逻辑
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user