-
Notifications
You must be signed in to change notification settings - Fork 3
/
condition.go
134 lines (108 loc) · 3.42 KB
/
condition.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
package sqlbuilder
import (
"bytes"
)
//
// Author: 陈永佳 [email protected], [email protected]
//
// 条件之间的关联关系
type ConditionRelation struct {
ctx *SQLContext
buffer *bytes.Buffer
}
func newConditionBuilder(ctx *SQLContext) *ConditionRelation {
return &ConditionRelation{
ctx: ctx,
buffer: new(bytes.Buffer),
}
}
// And
func (slf *ConditionRelation) And() *ConditionRelation {
slf.buffer.WriteString(" AND ")
return slf
}
// Or
func (slf *ConditionRelation) Or() *ConditionRelation {
slf.buffer.WriteString(" OR ")
return slf
}
func (slf *ConditionRelation) Compile() string {
return slf.buffer.String()
}
//// Conditions
// 等于
func (slf *ConditionRelation) Eq(column string) *ConditionRelation {
return slf.EqTo(column, slf.ctx.placeHolder)
}
// 等于指定值
func (slf *ConditionRelation) EqTo(column string, value interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " = ", value))
return slf
}
// 不等于
func (slf *ConditionRelation) NEq(column string) *ConditionRelation {
return slf.NEqTo(column, slf.ctx.placeHolder)
}
// 不等于指定值
func (slf *ConditionRelation) NEqTo(column string, value interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " <> ", value))
return slf
}
// 大于
func (slf *ConditionRelation) Gt(column string) *ConditionRelation {
return slf.GtTo(column, slf.ctx.placeHolder)
}
// 大于指定值
func (slf *ConditionRelation) GtTo(column string, value interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " > ", value))
return slf
}
// 大于或等于
func (slf *ConditionRelation) GEt(column string) *ConditionRelation {
return slf.GEtTo(column, slf.ctx.placeHolder)
}
// 大于或等于指定值
func (slf *ConditionRelation) GEtTo(column string, value interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " >= ", value))
return slf
}
// 小于
func (slf *ConditionRelation) Lt(column string) *ConditionRelation {
return slf.LEtTo(column, slf.ctx.placeHolder)
}
// 小于指定值
func (slf *ConditionRelation) LtTo(column string, value interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " < ", value))
return slf
}
// 小于或者等于
func (slf *ConditionRelation) LEt(column string) *ConditionRelation {
return slf.LEtTo(column, slf.ctx.placeHolder)
}
// 小于或者等于指定值
func (slf *ConditionRelation) LEtTo(column string, value interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " <= ", value))
return slf
}
// Like
func (slf *ConditionRelation) Like(column string, pattern string) *ConditionRelation {
slf.buffer.WriteString(slf.op(column, " LIKE ", pattern))
return slf
}
// Between
func (slf *ConditionRelation) Between(column string, start interface{}, end interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.opv(column, " BETWEEN ", slf.ctx.escapeValue(start)+" AND "+slf.ctx.escapeValue(end)))
return slf
}
// 在指定集合中
func (slf *ConditionRelation) In(column string, items ...interface{}) *ConditionRelation {
slf.buffer.WriteString(slf.opv(column, " IN ", brackets(slf.ctx.joinValues(items))))
return slf
}
////
func (slf *ConditionRelation) opv(name string, op string, value string) string {
return slf.ctx.escapeName(name) + op + value
}
func (slf *ConditionRelation) op(name string, op string, value interface{}) string {
return slf.ctx.escapeName(name) + op + slf.ctx.escapeValue(value)
}