forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclause.go
163 lines (144 loc) · 3.51 KB
/
clause.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package helper
import (
"strings"
"gorm.io/gorm/clause"
)
type Cond struct {
Cond bool
Result string
}
func IfClause(conds []Cond) string {
judge := func(c Cond) string {
if c.Cond {
return c.Result
}
return ""
}
clauses := make([]string, len(conds))
for i, cond := range conds {
clauses[i] = strings.Trim(judge(cond), " ")
}
return " " + strings.Join(clauses, " ")
}
func WhereClause(conds []string) string {
return joinClause(conds, "WHERE", whereValue, " ")
}
func SetClause(conds []string) string {
return joinClause(conds, "SET", setValue, ",")
}
func joinClause(conds []string, keyword string, deal func(string) string, sep string) string {
clauses := make([]string, len(conds))
for i, clause := range conds {
clauses[i] = deal(clause)
}
sql := trimAll(strings.Join(clauses, sep))
if sql != "" {
sql = " " + keyword + " " + sql
}
return sql
}
func trimAll(input string) string {
return trimRight(trimLeft(input))
}
func trimLeft(input string) string {
input = strings.TrimSpace(input)
lowercase := strings.ToLower(input)
switch {
case strings.HasPrefix(lowercase, "and "):
return input[4:]
case strings.HasPrefix(lowercase, "or"):
return input[3:]
case strings.HasPrefix(lowercase, "xor "):
return input[4:]
case strings.HasPrefix(lowercase, ","):
return input[1:]
default:
return input
}
}
func trimRight(input string) string {
input = strings.TrimSpace(input)
lowercase := strings.ToLower(input)
switch {
case strings.HasSuffix(lowercase, " and"):
return input[:len(input)-3]
case strings.HasSuffix(lowercase, " or"):
return input[:len(input)-2]
case strings.HasSuffix(lowercase, " xor"):
return input[:len(input)-3]
case strings.HasSuffix(lowercase, ","):
return input[:len(input)-1]
default:
return input
}
}
// whereValue append a new condition with prefix "AND"
func whereValue(value string) string {
value = strings.Trim(value, " ")
lowercase := strings.ToLower(value)
switch {
case lowercase == "":
return ""
case strings.HasPrefix(lowercase, "and "):
return value
case strings.HasPrefix(lowercase, "or "):
return value
case strings.HasPrefix(lowercase, "xor "):
return value
default:
return "AND " + value
}
}
func setValue(value string) string {
return strings.Trim(value, ", ")
}
func JoinWhereBuilder(src *strings.Builder, whereValue strings.Builder) {
value := trimAll(whereValue.String())
if value != "" {
src.WriteString("WHERE ")
src.WriteString(value)
src.WriteString(" ")
}
}
func JoinSetBuilder(src *strings.Builder, setValue strings.Builder) {
value := trimAll(setValue.String())
if value != "" {
src.WriteString("SET ")
src.WriteString(value)
src.WriteString(" ")
}
}
// JoinTblExpr join clause with table expression(sub query...)
type JoinTblExpr struct {
clause.Join
TableExpr clause.Expression
}
func NewJoinTblExpr(join clause.Join, tbExpr clause.Expression) JoinTblExpr {
return JoinTblExpr{Join: join, TableExpr: tbExpr}
}
func (join JoinTblExpr) Build(builder clause.Builder) {
if builder == nil {
return
}
if join.Type != "" {
_, _ = builder.WriteString(string(join.Type))
_ = builder.WriteByte(' ')
}
_, _ = builder.WriteString("JOIN ")
if join.TableExpr != nil {
join.TableExpr.Build(builder)
}
if len(join.ON.Exprs) > 0 {
_, _ = builder.WriteString(" ON ")
join.ON.Build(builder)
} else if len(join.Using) > 0 {
_, _ = builder.WriteString(" USING (")
for idx, c := range join.Using {
if idx > 0 {
_ = builder.WriteByte(',')
}
builder.WriteQuoted(c)
}
_ = builder.WriteByte(')')
}
}