-
Notifications
You must be signed in to change notification settings - Fork 3
/
select.go
184 lines (156 loc) · 4.11 KB
/
select.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
package sqlbuilder
import (
"strconv"
"strings"
)
var nullDest interface{}
// Select returns a new SELECT statement with the default dialect.
func Select() SelectStatement {
return SelectStatement{dialect: DefaultDialect}
}
// SelectStatement represents a SELECT statement.
type SelectStatement struct {
dialect Dialect
table string
selects []sel
joins []join
wheres []where
lock bool
limit *int
offset *int
order string
group string
having string
}
type sel struct {
col string
dest interface{}
}
type join struct {
sql string
args []interface{}
}
// Dialect returns a new statement with dialect set to 'dialect'.
func (s SelectStatement) Dialect(dialect Dialect) SelectStatement {
s.dialect = dialect
return s
}
// From returns a new statement with the table to select from set to 'table'.
func (s SelectStatement) From(table string) SelectStatement {
s.table = table
return s
}
// Map returns a new statement with column 'col' selected and scanned
// into 'dest'. 'dest' may be nil if the value should not be scanned.
func (s SelectStatement) Map(col string, dest interface{}) SelectStatement {
if dest == nil {
dest = nullDest
}
s.selects = append(s.selects, sel{col, dest})
return s
}
// Join returns a new statement with JOIN expression 'sql'.
func (s SelectStatement) Join(sql string, args ...interface{}) SelectStatement {
s.joins = append(s.joins, join{sql, args})
return s
}
// Where returns a new statement with condition 'cond'. Multiple conditions
// are combined with AND.
func (s SelectStatement) Where(cond string, args ...interface{}) SelectStatement {
s.wheres = append(s.wheres, where{cond, args})
return s
}
// Limit returns a new statement with the limit set to 'limit'.
func (s SelectStatement) Limit(limit int) SelectStatement {
s.limit = &limit
return s
}
// Offset returns a new statement with the offset set to 'offset'.
func (s SelectStatement) Offset(offset int) SelectStatement {
s.offset = &offset
return s
}
// Order returns a new statement with ordering 'order'.
// Only the last Order() is used.
func (s SelectStatement) Order(order string) SelectStatement {
s.order = order
return s
}
// Group returns a new statement with grouping 'group'.
// Only the last Group() is used.
func (s SelectStatement) Group(group string) SelectStatement {
s.group = group
return s
}
// Having returns a new statement with HAVING condition 'having'.
// Only the last Having() is used.
func (s SelectStatement) Having(having string) SelectStatement {
s.having = having
return s
}
// Lock returns a new statement with FOR UPDATE locking.
func (s SelectStatement) Lock() SelectStatement {
s.lock = true
return s
}
// Build builds the SQL query. It returns the query, the argument slice,
// and the destination slice.
func (s SelectStatement) Build() (query string, args []interface{}, dest []interface{}) {
var cols []string
idx := 0
if len(s.selects) > 0 {
for _, sel := range s.selects {
cols = append(cols, sel.col)
if sel.dest == nil {
dest = append(dest, &nullDest)
} else {
dest = append(dest, sel.dest)
}
}
} else {
cols = append(cols, "1")
dest = append(dest, &nullDest)
}
query = "SELECT " + strings.Join(cols, ", ") + " FROM " + s.table
for _, join := range s.joins {
sql := join.sql
for _, arg := range join.args {
sql = strings.Replace(sql, "?", s.dialect.Placeholder(idx), 1)
idx++
args = append(args, arg)
}
query += " " + sql
}
if len(s.wheres) > 0 {
var sqls []string
for _, where := range s.wheres {
sql := "(" + where.sql + ")"
for _, arg := range where.args {
sql = strings.Replace(sql, "?", s.dialect.Placeholder(idx), 1)
idx++
args = append(args, arg)
}
sqls = append(sqls, sql)
}
query += " WHERE " + strings.Join(sqls, " AND ")
}
if s.order != "" {
query += " ORDER BY " + s.order
}
if s.group != "" {
query += " GROUP BY " + s.group
}
if s.having != "" {
query += " HAVING " + s.having
}
if s.limit != nil {
query += " LIMIT " + strconv.Itoa(*s.limit)
}
if s.offset != nil {
query += " OFFSET " + strconv.Itoa(*s.offset)
}
if s.lock {
query += " FOR UPDATE"
}
return
}