-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.go
157 lines (133 loc) · 3.02 KB
/
operators.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
package ceous
import (
"database/sql/driver"
"fmt"
sq "github.com/elgris/sqrl"
)
// eqOperator implements the equalty operation in SQL. The preference would be
// to use the already implemented `sqrl.Eq`, however its implementation is notOperator
// very effective.
type (
eqOperator [2]interface{}
neOperator eqOperator
notOperator struct {
cond Sqlizer
}
sqlCondition struct {
sql string
args []interface{}
}
)
func Asc(field interface{}) string {
switch f := field.(type) {
case string:
return f + " ASC"
case *string:
return *f + " ASC"
case AliasedSchemaField:
return f.Reference() + " ASC"
case fmt.Stringer:
return f.String() + " ASC"
}
return fmt.Sprint(field, " ASC")
}
func Desc(field interface{}) string {
switch f := field.(type) {
case string:
return f + " DESC"
case *string:
return *f + " DESC"
case AliasedSchemaField:
return f.Reference() + " DESC"
case fmt.Stringer:
return f.String() + " DESC"
}
return fmt.Sprint(field, " DESC")
}
func OpEq(a, b interface{}) *eqOperator {
return &eqOperator{a, b}
}
func OpNe(a, b interface{}) *neOperator {
return &neOperator{a, b}
}
func OpNot(a Sqlizer) *notOperator {
return ¬Operator{a}
}
const (
equalOprStr = "="
inOprStr = "IN"
nullOprStr = "IS"
inEmptyExprStr = "(1=0)" // Portable FALSE
notEqualOprStr = "<>"
notInOprStr = "NOT IN"
notNullOprStr = "IS NOT"
notInEmptyExprStr = "(1=1)" // Portable TRUE
)
func equalOpr(useNot bool) string {
if useNot {
return notEqualOprStr
}
return equalOprStr
}
func inOpr(useNot bool) string {
if useNot {
return notInOprStr
}
return inOprStr
}
func nullOpr(useNot bool) string {
if useNot {
return notNullOprStr
}
return nullOprStr
}
func inEmptyExpr(useNot bool) string {
if useNot {
return notInEmptyExprStr
}
return inEmptyExprStr
}
func (operator *eqOperator) toSql(useNot bool) (sql string, args []interface{}, err error) {
key := operator[0]
val := operator[1]
switch v := val.(type) {
case driver.Valuer:
if val, err = v.Value(); err != nil {
return
}
}
if val == nil {
sql = fmt.Sprintf("%s %s NULL", key, nullOpr(useNot))
return
}
switch list := val.(type) {
case []interface{}:
if len(list) == 0 {
sql = inEmptyExpr(useNot)
} else {
args = list
sql = fmt.Sprintf("%s %s (%s)", key, inOpr(useNot), sq.Placeholders(len(list)))
}
return
}
sql = fmt.Sprintf("%s %s ?", key, equalOpr(useNot))
args = []interface{}{val}
return
}
func (operator *eqOperator) ToSql() (string, []interface{}, error) {
return operator.toSql(false)
}
func (operator *neOperator) ToSql() (string, []interface{}, error) {
return (*eqOperator)(operator).toSql(true)
}
func (operator *notOperator) ToSql() (string, []interface{}, error) {
sql, args, err := operator.cond.ToSql()
if err != nil {
return "", nil, err
}
return "NOT (" + sql + ")", args, err
}
// ToSql will return the saved condition sql for operators...
func (operator *sqlCondition) ToSql() (string, []interface{}, error) {
return operator.sql, operator.args, nil
}