-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlExpressionGenerator.go
More file actions
93 lines (74 loc) · 2.12 KB
/
SqlExpressionGenerator.go
File metadata and controls
93 lines (74 loc) · 2.12 KB
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
package main
import (
"bytes"
"fmt"
)
type SqlExprGenerator struct {
CurrentVal string
}
func newSqlExpr(input_column string) SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "`" + input_column + "`",
}
}
func CheckNull(input_column string, expr SqlExprGenerator) SqlExprGenerator {
return newSqlExpr(input_column).IfNotNull(expr)
}
func EmptyIfNull(input_column string) SqlExprGenerator {
return newSqlExpr(input_column).IfNull(SqlExprGenerator{CurrentVal: "''"})
}
func SqlConcat(vals ...SqlExprGenerator) SqlExprGenerator {
var output bytes.Buffer
output.WriteString("CONCAT(")
for i, val := range vals {
if i != 0 {
output.WriteByte(',')
}
output.WriteString(val.String())
}
output.WriteByte(')')
return SqlExprGenerator{CurrentVal: output.String()}
}
func (e SqlExprGenerator) IfNull(in SqlExprGenerator) SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "IFNULL(" + e.CurrentVal + ", " + in.String() + ")",
}
}
func (e SqlExprGenerator) SplitBefore(text string) SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "SUBSTRING_INDEX(" + e.CurrentVal + ", " + EscapeString(text) + " , 1)",
}
}
func (e SqlExprGenerator) Reverse() SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "REVERSE(" + e.CurrentVal + ")",
}
}
func (e SqlExprGenerator) ToLower() SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "LOWER(" + e.CurrentVal + ")",
}
}
func (e SqlExprGenerator) IfNotNull(expr SqlExprGenerator) SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "IF (" + e.CurrentVal + " IS NOT NULL, " + expr.String() + ", NULL)",
}
}
func (e SqlExprGenerator) OnlyAlphaNum() SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "REGEXP_REPLACE(" + e.CurrentVal + ", '[^a-zA-Z0-9]', '')",
}
}
func (e SqlExprGenerator) OnlyNum() SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "REGEXP_REPLACE(" + e.CurrentVal + ", '[^0-9]', '')",
}
}
func (e SqlExprGenerator) MaxLen(len uint) SqlExprGenerator {
return SqlExprGenerator{
CurrentVal: "LEFT(" + e.CurrentVal + ", " + fmt.Sprint(len) + ")",
}
}
func (e SqlExprGenerator) String() string {
return e.CurrentVal
}