-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_base.go
More file actions
267 lines (221 loc) · 9.6 KB
/
select_base.go
File metadata and controls
267 lines (221 loc) · 9.6 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package query
import (
"fmt"
"strings"
)
// BaseSelectBuilder helps to build table queries.
// It does not provide pagination or ordering itself
// but contains helper methods to generate SelectListBuilder or SelectOneBuilder.
// It also provides method Count() to generate SelectCountBuilder.
type BaseSelectBuilder struct {
BaseBuilder
baseTable TableIdent
joins []TableJoiner
fields Fields
where WhereClause
}
// String returns a string representation of BaseSelectBuilder.
func (query BaseSelectBuilder) String() string {
return strings.Join([]string{
fmt.Sprintf("%T(%v)", query, query.RenderFrom()),
query.fields.String(),
query.where.String(),
}, ",")
}
func (query BaseSelectBuilder) RenderSQL() (sql string) {
tokens := append([]string{},
DoSelect.String(),
query.fields.FieldList(),
kwFrom.String(),
query.baseTable.RenderFrom(),
)
for _, joiner := range query.joins {
tokens = append(tokens, joiner.RenderFrom())
}
if len(query.where.Conditions()) > 0 {
tokens = append(tokens, kwWhere.String(), query.where.RenderSQL())
}
return strings.Join(tokens, " ")
}
func (query BaseSelectBuilder) Render(parametersCount int) (sql string) {
tokens := append([]string{},
DoSelect.String(),
query.fields.FieldList(),
kwFrom.String(),
query.baseTable.RenderFrom(),
)
for _, joiner := range query.joins {
tokens = append(tokens, joiner.RenderFrom())
}
if len(query.where.Conditions()) > 0 {
tokens = append(tokens, kwWhere.String(), query.where.Render(parametersCount))
}
return strings.Join(tokens, " ")
}
func (query BaseSelectBuilder) Values() (params []any) {
params = make([]any, 0)
if len(query.where.Conditions()) > 0 {
params = append(params, query.where.Values()...)
}
return params
}
func (query BaseSelectBuilder) RenderFrom() (fromClause string) {
fromClauseItems := make([]string, 1+len(query.joins))
fromClauseItems[0] = query.baseTable.RenderFrom()
for idx, tableJoiner := range query.joins {
fromClauseItems[1+idx] = tableJoiner.RenderFrom()
}
return strings.Join(fromClauseItems, " ")
}
// BuildQueryAndParams generates sql query string with desired parameters set.
// If query generation failed returns empty query and parameters set or non-nil error.
func (query BaseSelectBuilder) BuildQueryAndParams() (sql string, params []interface{}, err error) {
params = make([]any, 0)
tokens := append([]string{},
DoSelect.String(),
query.fields.FieldList(),
kwFrom.String(),
query.baseTable.RenderFrom(),
)
for _, joiner := range query.joins {
tokens = append(tokens, joiner.RenderFrom())
}
if len(query.where.Conditions()) > 0 {
tokens = append(tokens, kwWhere.String(), query.where.Render(0))
params = append(params, query.where.Values()...)
}
return strings.Join(tokens, " "), params, nil
}
// TableName returns table name to fetch records from.
func (query BaseSelectBuilder) TableName() TableName {
return query.baseTable.TableName()
}
// Fields returns a copy of SelectManyBuilder having mustField list to retrieve updated with a list of specified FieldDefinition`s.
func (query BaseSelectBuilder) Fields(fieldSpecs ...FieldDefinition) (updated BaseSelectBuilder) {
updated = query
updated.fields = updated.fields.Fields(fieldSpecs...)
return updated
}
// FieldDefinitions returns a copy of attached FieldDefinition list.
func (query BaseSelectBuilder) FieldDefinitions() (res []FieldDefinition) {
return query.fields.FieldDefinitions()
}
// Where adds fields conditions GroupAND returns modified SelectManyBuilder.
// If any conditions are already added, adds new conditions group joined with logical AND.
func (query BaseSelectBuilder) Where(fieldConditions ...Condition) (updated BaseSelectBuilder) {
updated = query
updated.where = updated.where.GroupAND(fieldConditions...)
return updated
}
// InsertInto generates SQL INSERT query builder using stored table name.
// Insert values are optional and could be set later with InsertBuilder.Values.
// Note generated InsertInto will receive only base TableIdent to generate insert into it.
func (query BaseSelectBuilder) InsertInto(insertValues ...FieldValue) InsertBuilder {
return InsertInto(query.baseTable).Values(insertValues...)
}
// Update generates table UpdateBuilder.
// Note generated UpdateBuilder will receive only base TableIdent to generate update on it.
func (query BaseSelectBuilder) Update(values ...FieldValue) UpdateBuilder {
return Update(query.baseTable).Where(query.where.Conditions()...).Set(values...)
}
// Delete generates table DeleteBuilder.
// Note generated DeleteBuilder will receive only base TableIdent to generate update on it.
func (query BaseSelectBuilder) Delete() DeleteBuilder {
return Delete(query.baseTable).Where(query.where.Conditions()...)
}
// Single makes a SelectSingleBuilder instance from BaseSelectBuilder.
func (query BaseSelectBuilder) Single() SelectSingleBuilder {
return SelectSingleBuilder{BaseSelectBuilder: query}
}
// Many makes a SelectManyBuilder instance from BaseSelectBuilder setting ordering and pagination to its default values.
// See also Offset, Limit and OrderBy.
func (query BaseSelectBuilder) Many() SelectManyBuilder {
return SelectManyFromBase(query)
}
// Offset makes SelectManyBuilder instance with offset set to specified value.
func (query BaseSelectBuilder) Offset(offset uint) SelectManyBuilder {
return query.Many().Offset(offset)
}
// Limit makes a SelectManyBuilder instance from BaseSelectBuilder setting required records limit to specified value.
func (query BaseSelectBuilder) Limit(limit int) SelectManyBuilder {
return query.Many().Limit(limit)
}
// OrderBy makes a SelectManyBuilder instance from BaseSelectBuilder setting ordering to specified value.
func (query BaseSelectBuilder) OrderBy(orderByFields ...FieldSorting) SelectManyBuilder {
return query.Many().OrderBy(orderByFields...)
}
// BaseSelectBuilder query builder provides methods to generate SQL SELECT clauses having joined tables source.
// joinIdent generates intermediate IncompleteSelectJoin instance.
// Takes TableIdent to join and TableJoinType constant defining required join type to produce.
// Call IncompleteSelectJoin.On will return updated BaseSelectBuilder with join builder data finished.
func (query BaseSelectBuilder) joinIdent(rightTable TableIdent, joinType TableJoinType) IncompleteSelectJoin {
updated := query
updated.joins = append(updated.joins, TableJoiner{rightTable: rightTable, joinType: joinType})
return incompleteBaseJoiner{BaseSelectBuilder: updated}
}
// Join generates intermediate IncompleteSelectJoin instance.
// Takes TableName to join and TableJoinType constant defining required join type to produce.
// Takes right table name or ident to join.
// Panics if rightTable is not instance of string, TableName or TableIdent types.
// Call IncompleteSelectJoin.On will return updated BaseSelectBuilder with join builder data finished.
func (query BaseSelectBuilder) Join(rightTable TableIdent, joinType TableJoinType) IncompleteSelectJoin {
return query.joinIdent(rightTable, joinType)
}
// InnerJoin generates intermediate IncompleteSelectJoin instance for InnerJoin type.
// Takes right table name or ident to join.
// Panics if rightTable is not instance of string, TableName or TableIdent types.
// Shortcut to Join(rightTable TableName, InnerJoin).
// Call IncompleteSelectJoin.On will return updated BaseSelectBuilder with join builder data finished.
func (query BaseSelectBuilder) InnerJoin(rightTable TableIdent) IncompleteSelectJoin {
return query.Join(rightTable, InnerJoin)
}
// LeftJoin generates intermediate IncompleteSelectJoin instance for LeftJoin type.
// Takes right table name or ident to join.
// Panics if rightTable is not instance of string, TableName or TableIdent types.
// Shortcut to Join(rightTable TableName, LeftJoin).
// Call IncompleteSelectJoin.On will return updated BaseSelectBuilder with join builder data finished.
func (query BaseSelectBuilder) LeftJoin(rightTable TableIdent) IncompleteSelectJoin {
return query.Join(rightTable, LeftJoin)
}
// RightJoin generates intermediate IncompleteSelectJoin instance for RightJoin type.
// Takes right table name or ident to join.
// Panics if rightTable is not instance of string, TableName or TableIdent types.
// Shortcut to Join(rightTable TableName, RightJoin).
// Call IncompleteSelectJoin.On will return updated BaseSelectBuilder with join builder data finished.
func (query BaseSelectBuilder) RightJoin(rightTable TableIdent) IncompleteSelectJoin {
return query.Join(rightTable, RightJoin)
}
// FullJoin generates intermediate IncompleteSelectJoin instance for FullJoin type.
// Takes right table name or ident to join.
// Panics if rightTable is not instance of string, TableName or TableIdent types.
// Shortcut to Join(rightTable TableName, FullJoin).
// Call IncompleteSelectJoin.On will return updated BaseSelectBuilder with join builder data finished.
func (query BaseSelectBuilder) FullJoin(rightTable TableIdent) IncompleteSelectJoin {
return query.Join(rightTable, FullJoin)
}
// Count creates an CountBuilder using parameters of this SelectManyBuilder.
func (query BaseSelectBuilder) Count() CountBuilder {
return Count(query)
}
// SelectFrom makes a new BaseSelectBuilder instance.
// Takes string, TableName or TableIdent to build BaseSelectBuilder over it.
func SelectFrom[T TableNameParameter](tableNameProvider T) BaseSelectBuilder {
var (
p any = tableNameProvider
table TableIdent
)
switch typedValue := p.(type) {
case string:
table = Table(TableName(typedValue))
case TableName:
table = Table(typedValue)
case TableIdent:
table = typedValue
}
return BaseSelectBuilder{
baseTable: table,
joins: make([]TableJoiner, 0),
where: NewWhere(),
fields: NewFields(),
}
}