This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryCondition.go
151 lines (116 loc) · 3.08 KB
/
queryCondition.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
package mysqlann
import "strings"
type ConditionGlueOperator int
type queryConditionList []*queryCondition
//glue types
const (
OR = 1
AND = 2
)
type queryCondition struct {
operator ConditionGlueOperator
string_conditions []string
sub_conditions queryConditionList
}
func And() *queryCondition {
qc := &queryCondition{
operator: AND,
}
qc.init()
return qc
}
func Or() *queryCondition {
qc := &queryCondition{
operator: OR,
}
qc.init()
return qc
}
func (qc *queryCondition) init() {
qc.string_conditions = make([]string, 0, 1) //assuming there will be at least one simple string condition
qc.sub_conditions = make(queryConditionList, 0)
}
func (qc *queryCondition) IsEmpty() bool {
return len(qc.string_conditions) == 0 && len(qc.sub_conditions) == 0
}
func (qc *queryCondition) AddString(condition string) *queryCondition {
qc.string_conditions = append(qc.string_conditions, condition)
return qc //method chaining
}
func (qc *queryCondition) AddEquality(field string, value Anything) *queryCondition {
if value == nil {
qc.AddString(field + " IS NULL")
} else {
qc.AddWithOperator(field, "=", value)
}
return qc //method chaining
}
func (qc *queryCondition) AddWithOperator(field string, operator string, value Anything) *queryCondition {
var str = field + " " + operator + " " + AnythingToSql(value)
qc.AddString(str)
return qc //method chaining
}
func (qc *queryCondition) AddCondition(condition *queryCondition) *queryCondition {
qc.sub_conditions = append(qc.sub_conditions, condition)
return qc //method chaining
}
func (qc *queryCondition) Add(args ...Anything) *queryCondition {
if len(args) == 0 || len(args) > 3 {
panic("There should be 1, 2 or 3 args here")
}
if len(args) == 1 {
condition, isCondition := args[0].(*queryCondition)
if isCondition {
qc.AddCondition(condition)
} else {
qc.AddString(AnythingToString(args[0]))
}
} else if len(args) == 2 {
qc.AddEquality(args[0].(string), args[1])
} else if len(args) == 3 {
qc.AddWithOperator(args[0].(string), args[1].(string), args[2])
}
return qc //method chaining
}
func (qc *queryCondition) Sql(multiline bool) string {
//preapare conditions as strings
var cooked_string_conditions = make([]string, len(qc.string_conditions) + len(qc.sub_conditions))
i := 0
for _, string_condition:= range qc.string_conditions {
cooked_string_conditions[i] = "(" + string_condition + ")"
i++
}
i = len(qc.string_conditions)
for _, condition:= range qc.sub_conditions {
cooked_string_conditions[i] = "(" + condition.Sql(false) + ")"
i++
}
//join into one string
var sb strings.Builder
first := true
for _, string_condition := range cooked_string_conditions {
if first {
first = false
} else {
if multiline{
sb.WriteString("\n ")
} else{
sb.WriteString(" ")
}
sb.WriteString(qc.OperatorSql())
sb.WriteString(" ")
}
sb.WriteString(string_condition)
}
return sb.String()
}
func (qc *queryCondition) OperatorSql() string {
switch qc.operator {
case OR:
return "OR"
case AND:
return "AND"
default:
return "[unknown operator]"
}
}