-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.go
296 lines (269 loc) · 6.35 KB
/
join.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package pgtalk
import (
"context"
"fmt"
"io"
"github.com/jackc/pgx/v5"
)
type joinType int
const (
innerJoinType joinType = iota
leftOuterJoinType
rightOuterJoinType
fullOuterJoinType
)
type join struct {
preparedName string
leftSet querySet
rightSet querySet
onLeft ColumnAccessor
onRight ColumnAccessor
condition SQLExpression
joinType joinType
limit int
offset int
}
func (i join) SQLOn(w WriteContext) {
fmt.Fprint(w, "SELECT\n")
if i.leftSet == nil {
fmt.Fprint(w, "ERROR: no left queryset set")
return
}
if i.rightSet == nil {
fmt.Fprint(w, "ERROR: no right queryset set")
return
}
left := i.leftSet.selectAccessors()
wl := i.leftSet.augmentedContext(w)
wr := i.rightSet.augmentedContext(w)
writeAccessOn(left, wl)
right := i.rightSet.selectAccessors()
if len(right) > 0 {
fmt.Fprint(wl, ",")
}
writeAccessOn(right, wr)
fmt.Fprint(w, "\nFROM ")
i.leftSet.fromSectionOn(wl)
writeJoinType(i.joinType, w)
i.rightSet.fromSectionOn(wr)
fmt.Fprint(w, "\nON ")
if i.condition == nil {
fmt.Fprint(w, "ERROR: no condition set")
return
}
i.condition.SQLOn(w) // TODO which tableInfo to use?
if _, ok := i.leftSet.whereCondition().(noCondition); !ok {
fmt.Fprint(wl, "\nWHERE ")
i.leftSet.whereCondition().SQLOn(wl)
}
if i.limit > 0 {
fmt.Fprintf(wl, "\nLIMIT %d", i.limit)
}
if i.offset > 0 {
fmt.Fprintf(wl, "\nOFFSET %d", i.offset)
}
// TODO RightSet where
}
func writeJoinType(t joinType, w io.Writer) {
switch t {
case innerJoinType:
fmt.Fprint(w, "\nINNER JOIN ")
case leftOuterJoinType:
fmt.Fprint(w, "\nLEFT OUTER JOIN ")
case rightOuterJoinType:
fmt.Fprint(w, "\nRIGHT OUTER JOIN ")
case fullOuterJoinType:
fmt.Fprint(w, "\nFULL OUTER JOIN ")
}
}
func (i join) Named(preparedName string) join {
i.preparedName = preparedName
return i
}
func (i join) On(condition SQLExpression) join {
i.condition = condition
return i
}
func (i join) Limit(limit int) join {
i.limit = limit
return i
}
func (i join) Offset(offset int) join {
i.offset = offset
return i
}
func (i join) LeftOuterJoin(q querySet) (m multiJoin) {
m.sets = append(m.sets, i.leftSet, i.rightSet, q)
m.joinTypes = append(m.joinTypes, i.joinType, leftOuterJoinType)
m.conditions = append(m.conditions, i.condition)
return
}
func (i join) Exec(ctx context.Context, conn querier, parameters ...*QueryParameter) (it joinResultIterator, err error) {
params := argumentValues(parameters)
sql := SQL(i)
if i.preparedName != "" {
if p, ok := conn.(preparer); ok {
_, err := p.Prepare(ctx, i.preparedName, sql)
if err != nil {
return joinResultIterator{queryError: err}, err
}
}
}
rows, err := conn.Query(ctx, sql, params...)
return joinResultIterator{queryError: err, leftSet: i.leftSet, rightSet: i.rightSet, rows: rows}, nil
}
type joinResultIterator struct {
queryError error
leftSet querySet
rightSet querySet
rows pgx.Rows
}
func (i *joinResultIterator) HasNext() bool {
if i.queryError != nil {
return false
}
if i.rows.Next() {
return true
} else {
i.rows.Close()
}
return false
}
func (i *joinResultIterator) Err() error {
if i.queryError != nil {
return i.queryError
}
return i.rows.Err()
}
func (i *joinResultIterator) Next(left any, right any) error {
if i.queryError != nil {
return i.queryError
}
sw := []any{}
// left
for _, each := range i.leftSet.selectAccessors() {
sw = append(sw, each.FieldValueToScan(left))
}
// right
for _, each := range i.rightSet.selectAccessors() {
sw = append(sw, each.FieldValueToScan(right))
}
return i.rows.Scan(sw...)
}
type multiJoin struct {
preparedName string
sets []querySet
joinTypes []joinType
conditions []SQLExpression
}
func (m multiJoin) On(condition SQLExpression) multiJoin {
m.conditions = append(m.conditions, condition)
return m
}
func (m multiJoin) LeftOuterJoin(q querySet) multiJoin {
m.sets = append(m.sets, q)
m.joinTypes = append(m.joinTypes, leftOuterJoinType)
return m
}
func (m multiJoin) Exec(ctx context.Context, conn querier) (*multiJoinResultIterator, error) {
sql := SQL(m)
if m.preparedName != "" {
if p, ok := conn.(preparer); ok {
_, err := p.Prepare(ctx, m.preparedName, sql)
if err != nil {
return &multiJoinResultIterator{queryError: err}, nil
}
}
}
rows, err := conn.Query(ctx, sql)
return &multiJoinResultIterator{queryError: err, querySets: m.sets, rows: rows}, nil
}
type tableWhere struct {
expression SQLExpression
tableInfo TableInfo
}
func (m multiJoin) SQLOn(w WriteContext) {
fmt.Fprint(w, "SELECT ")
for i, each := range m.sets {
if i > 0 && len(each.selectAccessors()) > 0 {
fmt.Fprint(w, ",")
}
writeAccessOn(each.selectAccessors(), w)
}
fmt.Fprint(w, " FROM ")
first := m.sets[0]
first.fromSectionOn(w)
// collect all conditions from all sets
wheres := []SQLWriter{}
for _, each := range m.sets {
if each.whereCondition() != EmptyCondition {
wheres = append(wheres, each.whereCondition())
}
}
for j := 0; j < len(m.joinTypes); j++ {
jt := m.joinTypes[j]
writeJoinType(jt, w)
set := m.sets[j+1]
set.fromSectionOn(w)
if j < len(m.conditions) {
fmt.Fprint(w, " ON ")
m.conditions[j].SQLOn(w)
}
}
if len(wheres) > 0 {
fmt.Fprint(w, " WHERE ")
for i, each := range wheres {
if i > 0 {
fmt.Fprint(w, " AND ")
}
each.SQLOn(w)
}
}
}
type multiJoinResultIterator struct {
queryError error
querySets []querySet
rows pgx.Rows
}
func (i *multiJoinResultIterator) Err() error {
if i.queryError != nil {
return i.queryError
}
return i.rows.Err()
}
func (i *multiJoinResultIterator) HasNext() bool {
if i.queryError != nil {
return false
}
if i.rows.Next() {
return true
} else {
i.rows.Close()
}
return false
}
func (i *multiJoinResultIterator) Next(models ...any) error {
if i.queryError != nil {
return i.queryError
}
// count non-empty querysets
countNonEmpty := 0
for _, each := range i.querySets {
if len(each.selectAccessors()) != 0 {
countNonEmpty++
}
}
// check models count matches
if mc, qc := len(models), countNonEmpty; mc != qc {
return fmt.Errorf("number of models [%d] does not match select count [%d]", mc, qc)
}
// TODO how to check model types?
sw := []any{}
// all sets
for m, eachSet := range i.querySets {
for _, each := range eachSet.selectAccessors() {
sw = append(sw, each.FieldValueToScan(models[m]))
}
}
return i.rows.Scan(sw...)
}