-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
152 lines (118 loc) · 2.97 KB
/
expression.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
package sqlb
import (
"fmt"
"strings"
)
type Expr interface {
ToExpr() (string, []interface{}, error)
}
type Operator string
const (
EqualOperator Operator = "="
NotEqualOperator Operator = "<>"
GreaterOperator Operator = ">"
GreaterOrEqualOperator Operator = ">="
LessOperator Operator = "<"
LessOrEqualOperator Operator = "<="
InOperator Operator = "IN"
LikeOperator Operator = "LIKE"
)
type Nested struct {
Column string
Operator Operator
Value Expr
}
func (e Nested) ToExpr() (string, []interface{}, error) {
expr, args, _ := e.Value.ToExpr()
return fmt.Sprintf("%s %s (%s)", e.Column, e.Operator, expr), args, nil
}
type Eq struct {
Column string
Value interface{}
}
func (e Eq) ToExpr() (string, []interface{}, error) {
return fmt.Sprintf("%s = ?", e.Column), []interface{}{e.Value}, nil
}
type NotEq struct {
Column string
Value interface{}
}
func (e NotEq) ToExpr() (string, []interface{}, error) {
return fmt.Sprintf("%s <> ?", e.Column), []interface{}{e.Value}, nil
}
type Like struct {
Column string
Value interface{}
}
func (e Like) ToExpr() (string, []interface{}, error) {
return fmt.Sprintf("%s LIKE ?", e.Column), []interface{}{e.Value}, nil
}
type In struct {
Column string
Value []interface{}
}
func (e In) ToExpr() (string, []interface{}, error) {
placeholders := []string{}
for i := 0; i < len(e.Value); i++ {
placeholders = append(placeholders, "?")
}
return fmt.Sprintf("%s IN (%s)", e.Column, strings.Join(placeholders, ",")), e.Value, nil
}
type Order struct {
Column string
Descending bool
// Flag to specific null order for Postgresql
IsPQNullsLast *bool
}
func (e Order) ToExpr() (string, []interface{}, error) {
order := "ASC"
if e.Descending {
order = "DESC"
}
sql := fmt.Sprintf("%s %s", e.Column, order)
if e.IsPQNullsLast != nil {
var nullsOrder string
if *e.IsPQNullsLast {
nullsOrder = "NULLS LAST"
} else {
nullsOrder = "NULLS FIRST"
}
sql = sql + " " + nullsOrder
}
return sql, nil, nil
}
type ConjunctionOperator string
const (
AndOperator ConjunctionOperator = "AND"
OrOperator ConjunctionOperator = "OR"
)
type conjunctionExpr struct {
Operator ConjunctionOperator
Exprs []Expr
}
func (c conjunctionExpr) ToExpr() (string, []interface{}, error) {
if len(c.Exprs) == 0 {
return "", nil, nil
}
if len(c.Exprs) == 1 {
return c.Exprs[0].ToExpr()
}
exprs := []string{}
aggregatedArgs := []interface{}{}
for _, e := range c.Exprs {
expr, args, _ := e.ToExpr()
exprs = append(exprs, expr)
aggregatedArgs = append(aggregatedArgs, args...)
}
return fmt.Sprintf("(%s)", strings.Join(exprs, fmt.Sprintf(" %s ", c.Operator))), aggregatedArgs, nil
}
type And []Expr
func (e And) ToExpr() (string, []interface{}, error) {
conj := conjunctionExpr{AndOperator, e}
return conj.ToExpr()
}
type Or []Expr
func (e Or) ToExpr() (string, []interface{}, error) {
conj := conjunctionExpr{OrOperator, e}
return conj.ToExpr()
}