1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
from.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-06-26 14:04 . option
package wrapper
import (
"gitee.com/h79/goutils/dao/option"
"strings"
)
var _ IFrom = (*From)(nil)
type From struct {
child bool //子源
From string `json:"from" yaml:"from"`
As string `json:"as" yaml:"as"`
}
func (f *From) Is() bool {
return f.From != ""
}
func (f *From) SetChild(child bool) {
f.child = child
}
func (f *From) SetAs(as string) {
f.As = as
}
func (f *From) Build(opts ...option.Option) string {
if !f.Is() {
return ""
}
var builder = strings.Builder{}
builder.WriteString(" FROM ")
if f.child {
builder.WriteByte('(')
}
f.from(&builder, f.From)
if f.child {
builder.WriteByte(')')
}
AddAlias(&builder, f.As)
return builder.String()
}
func (f *From) from(builder *strings.Builder, v string) bool {
if v == "" {
return false
}
quo := true
first := v[0]
end := v[len(v)-1]
if first == '`' {
quo = false
} else {
l := len(v)
if l > 9 {
l = 9
}
up := strings.ToUpper(v[0:l])
if strings.Contains(up, "DROP") || strings.Contains(up, "DELETE") {
panic("may be illegal,lead to serious consequences")
}
quo = !strings.Contains(up, "SELECT")
}
if quo {
builder.WriteByte('`')
first = '`'
}
builder.WriteString(v)
if first == '`' && end != '`' {
builder.WriteByte('`')
}
return true
}
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.65

搜索帮助