-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext.go
204 lines (162 loc) · 4.62 KB
/
context.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package sqlbuilder
import (
"fmt"
"strings"
)
//
// Author: 陈永佳 [email protected], [email protected]
// SQL Builder
//
const SQLComma = ", "
const SQLSpace = " "
type SQLContext struct {
prepare SQLPrepare
placeHolder string
nameEscape string
valueEscape string
}
func NewContext() *SQLContext {
return NewContextWith(nil)
}
func NewContextWith(prepare SQLPrepare) *SQLContext {
return &SQLContext{
prepare: prepare,
placeHolder: "?",
nameEscape: "`",
valueEscape: "'",
}
}
func (slf *SQLContext) SetPlaceHolder(ph string) *SQLContext {
slf.placeHolder = ph
return slf
}
func (slf *SQLContext) SetNameEscapeChar(char string) *SQLContext {
slf.nameEscape = char
return slf
}
func (slf *SQLContext) SetValueEscapeChar(char string) *SQLContext {
slf.valueEscape = char
return slf
}
func (slf *SQLContext) Insert(table string) *InsertBuilder {
return newInsertBuilder(slf, table)
}
func (slf *SQLContext) Delete(table string) *DeleteBuilder {
return newDeleteBuilder(slf, table)
}
func (slf *SQLContext) Update(table string) *UpdateBuilder {
return newUpdateBuilder(slf, table)
}
func (slf *SQLContext) Select(column string, otherColumns ...string) *SelectBuilder {
return newSelectBuilder(slf, column, otherColumns...)
}
func (slf *SQLContext) CreateIndex(indexName string) *CreateIndexBuilder {
return newCreateIndex(slf, indexName)
}
func (slf *SQLContext) DropIndex(indexName string) *DropIndexBuilder {
return newDropIndexBuilder(slf, indexName)
}
func (slf *SQLContext) CreateTable(tableName string) *CreateTableBuilder {
return newCreateTableBuilder(slf, tableName)
}
//// Conditions
// 等于
func (slf *SQLContext) Eq(column string) *ConditionRelation {
return newConditionBuilder(slf).Eq(column)
}
// 等于指定值
func (slf *SQLContext) EqTo(column string, value interface{}) *ConditionRelation {
return newConditionBuilder(slf).EqTo(column, value)
}
// 不等于
func (slf *SQLContext) NEq(column string) *ConditionRelation {
return newConditionBuilder(slf).NEq(column)
}
// 不等于指定值
func (slf *SQLContext) NEqTo(column string, value interface{}) *ConditionRelation {
return newConditionBuilder(slf).NEqTo(column, value)
}
// 大于
func (slf *SQLContext) Gt(column string) *ConditionRelation {
return newConditionBuilder(slf).Gt(column)
}
// 大于指定值
func (slf *SQLContext) GtTo(column string, value interface{}) *ConditionRelation {
return newConditionBuilder(slf).GtTo(column, value)
}
// 大于或等于
func (slf *SQLContext) GEt(column string) *ConditionRelation {
return newConditionBuilder(slf).GEt(column)
}
// 大于或等于指定值
func (slf *SQLContext) GEtTo(column string, value interface{}) *ConditionRelation {
return newConditionBuilder(slf).GEtTo(column, value)
}
// 小于
func (slf *SQLContext) Lt(column string) *ConditionRelation {
return newConditionBuilder(slf).Lt(column)
}
// 小于指定值
func (slf *SQLContext) LtTo(column string, value interface{}) *ConditionRelation {
return newConditionBuilder(slf).LtTo(column, value)
}
// 小于或者等于
func (slf *SQLContext) LEt(column string) *ConditionRelation {
return newConditionBuilder(slf).LEt(column)
}
// 小于或者等于指定值
func (slf *SQLContext) LEtTo(column string, value interface{}) *ConditionRelation {
return newConditionBuilder(slf).LEtTo(column, value)
}
////
func (slf *SQLContext) escapeName(name string) string {
if len(slf.nameEscape) <= 0 {
return name
}
if len(name) == 0 {
panic("Empty name")
}
if "*" == name {
return name
}
// : count(id) as count
if strings.IndexAny(name, " ") > 0 {
return name
}
return slf.nameEscape + name + slf.nameEscape
}
func (slf *SQLContext) escapeValue(val interface{}) string {
if strValue, ok := val.(string); ok {
if slf.placeHolder == strValue {
return strValue
} else {
return slf.valueEscape + strValue + slf.valueEscape
}
} else {
return fmt.Sprintf("%v", val)
}
}
////
func (slf *SQLContext) joinNames(items []string) string {
return strings.Join(MapStr(items, slf.escapeName), SQLComma)
}
func (slf *SQLContext) joinValues(values []interface{}) string {
return strings.Join(MapAny(values, slf.escapeValue), SQLComma)
}
func (slf *SQLContext) wrapOP(name string, op string, value interface{}) string {
return slf.escapeName(name) + op + slf.escapeValue(value)
}
func MapStr(items []string, mapper func(string) string) []string {
out := make([]string, len(items))
for i, v := range items {
out[i] = mapper(v)
}
return out
}
func MapAny(items []interface{}, mapper func(interface{}) string) []string {
out := make([]string, len(items))
for i, v := range items {
out[i] = mapper(v)
}
return out
}