1 Star 0 Fork 0

h79 / goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
join.go 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-06-26 14:04 . option
package wrapper
import (
"gitee.com/h79/goutils/dao/option"
"strings"
)
// 内连接(inner join…on…)
// 全外连接(full join…on…)
// 左连接(left join…on…)
// 右连接(right join…on…)
// 交叉连接(cross join …on…)
const (
JoinLeft = "LEFT"
JoinRight = "RIGHT"
JoinInner = "INNER"
JoinFull = "FULL"
JonnCross = "CROSS"
)
var _ IBuilder = (*Join)(nil)
type Join struct {
Type string `json:"type" yaml:"type"`
Table string `json:"table" yaml:"table"`
Alias string `json:"as" yaml:"as"`
ON string `json:"expr" yaml:"expr"`
Using []string `json:"using" yaml:"using"`
}
func (jn *Join) Is() bool {
return jn.Type != "" && jn.Table != "" && (jn.ON != "" || len(jn.Using) > 0)
}
func (jn *Join) JoinType() (string, bool) {
jt := strings.ToUpper(jn.Type)
return jt, (jt == JoinLeft ||
jt == JoinRight ||
jt == JoinFull ||
jt == JoinInner ||
jt == JonnCross) && jn.Is()
}
func (jn *Join) Build(opts ...option.Option) string {
joinType, ok := jn.JoinType()
if !ok {
return ""
}
var builder = strings.Builder{}
builder.WriteByte(' ')
builder.WriteString(joinType)
builder.WriteString(" JOIN ")
AddQuoted(&builder, jn.Table)
AddAlias(&builder, jn.Alias)
if len(jn.ON) > 0 {
builder.WriteString(" ON ")
builder.WriteString(jn.ON)
} else if len(jn.Using) > 0 {
builder.WriteString(" USING (")
for idx, c := range jn.Using {
if idx > 0 {
builder.WriteByte(',')
}
builder.WriteByte('\'')
builder.WriteString(c)
builder.WriteByte('\'')
}
builder.WriteByte(')')
}
return builder.String()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.65

搜索帮助