-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombining.go
85 lines (73 loc) · 2.59 KB
/
combining.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
package pgtalk
import (
"context"
"fmt"
)
type QueryCombineable interface {
SQLWriter
// Every SELECT statement within UNION must have the same number of columns
// The columns must also have similar data types
// The columns in every SELECT statement must also be in the same order
Union(o QueryCombineable, all ...bool) QueryCombineable
// There are some mandatory rules for INTERSECT operations such as the
// number of columns, data types, and other columns must be the same
// in both SELECT statements for the INTERSECT operator to work correctly.
Intersect(o QueryCombineable, all ...bool) QueryCombineable
// There are some mandatory rules for EXCEPT operations such as the
// number of columns, data types, and other columns must be the same
// in both EXCEPT statements for the EXCEPT operator to work correctly.
Except(o QueryCombineable, all ...bool) QueryCombineable
// ExecIntoMaps executes the query and returns each rows as a map string->any .
// To include table information for each, append a custom sqlfunction to each select statement.
// For example, adding `pgtalk.SQLAs("'products'", "table")` will add an entry to the map.
ExecIntoMaps(ctx context.Context, conn querier, parameters ...*QueryParameter) (list []map[string]any, err error)
}
type queryCombination struct {
left QueryCombineable
operator string // UNION,INTERSECT,EXCEPT
right QueryCombineable
}
func combineOperator(combiner string, all ...bool) string {
if len(all) > 0 && all[0] {
return combiner + " ALL"
}
return combiner
}
func (q queryCombination) SQLOn(w WriteContext) {
fmt.Fprint(w, "((")
q.left.SQLOn(w)
fmt.Fprintf(w, ") %s (", q.operator)
q.right.SQLOn(w)
fmt.Fprint(w, "))")
}
func (q queryCombination) Union(o QueryCombineable, all ...bool) QueryCombineable {
return queryCombination{
left: q,
operator: "UNION",
right: o,
}
}
func (q queryCombination) Intersect(o QueryCombineable, all ...bool) QueryCombineable {
return queryCombination{
left: q,
operator: "INTERSECT",
right: o,
}
}
func (q queryCombination) Except(o QueryCombineable, all ...bool) QueryCombineable {
return queryCombination{
left: q,
operator: "EXCEPT",
right: o,
}
}
// Combine implements QueryCombineable
func (q queryCombination) ExecIntoMaps(ctx context.Context, conn querier, parameters ...*QueryParameter) (list []map[string]any, err error) {
return execIntoMaps(ctx, conn, SQL(q), q.findSet().selectAccessors(), parameters...)
}
func (q queryCombination) findSet() querySet {
if qc, ok := q.left.(queryCombination); ok {
return qc.findSet()
}
return q.left.(querySet)
}