1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
expr.go 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-07-22 21:04 . sql
package wrapper
import (
"reflect"
"strconv"
"strings"
)
func AddTable(builder *strings.Builder, table string) {
if table == "" {
return
}
AddQuoted(builder, table)
builder.WriteByte('.')
}
func IsQuoted(v string) bool {
if v == "" {
return false
}
first := v[0]
end := v[len(v)-1]
return first == '`' && end == '`'
}
func AddQuoted(builder *strings.Builder, v string) bool {
if v == "" {
return false
}
first := v[0]
end := v[len(v)-1]
if first != '`' { //引号
builder.WriteByte('`')
}
builder.WriteString(v)
if end != '`' {
builder.WriteByte('`')
}
return true
}
func AddAlias(builder *strings.Builder, v string) bool {
if v == "" {
return false
}
builder.WriteString(" AS ")
builder.WriteString(v)
return true
}
func AddVar(builder *strings.Builder, val interface{}) {
ref := reflect.Indirect(reflect.ValueOf(val))
switch ref.Kind() {
case reflect.String:
s := ref.String()
s = strings.ReplaceAll(s, "\"", "\\\"")
s = strings.ReplaceAll(s, "'", "\\'")
builder.WriteByte('\'')
builder.WriteString(s)
builder.WriteByte('\'')
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
builder.WriteString(strconv.FormatInt(ref.Int(), 10))
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
builder.WriteString(strconv.FormatUint(ref.Uint(), 10))
case reflect.Float32:
fallthrough
case reflect.Float64:
builder.WriteString(strconv.FormatFloat(ref.Float(), 'f', 5, 64))
case reflect.Bool:
if ref.Bool() {
builder.WriteString("true")
} else {
builder.WriteString("false")
}
case reflect.Struct:
if sql, ok := val.(*SQL); ok {
builder.WriteString(sql.Build())
return
}
if sql, ok := val.(SQL); ok {
builder.WriteString(sql.Build())
}
default:
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.114

搜索帮助