-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_struct.go
171 lines (140 loc) · 3.5 KB
/
query_struct.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
package dal
import (
"bytes"
"fmt"
"reflect"
"strconv"
)
var _ Query = theQuery{}
var _ Query = (*theQuery)(nil)
// query holds definition of a query
type theQuery struct {
// From defines target table/collection
from *CollectionRef
// Where defines filter condition
where Condition
// GroupBy defines expressions to group by
groupBy []Expression
// OrderBy defines expressions to order by
orderBy []OrderExpression
// Columns defines what columns to return
columns []Column
into func() Record
// Offset specifies number of records to skip
offset int
// Limit specifies maximum number of records to be returned
limit int
idKind reflect.Kind
// StartCursor specifies the startCursor/point to start from
startCursor Cursor
}
func (q theQuery) From() *CollectionRef {
return q.from
}
func (q theQuery) Where() Condition {
return q.where
}
func (q theQuery) GroupBy() []Expression {
return q.groupBy[:]
}
func (q theQuery) OrderBy() []OrderExpression {
return q.orderBy[:]
}
func (q theQuery) Columns() []Column {
return q.columns[:]
}
func (q theQuery) Into() func() Record {
return q.into
}
func (q theQuery) IDKind() reflect.Kind {
return q.idKind
}
func (q theQuery) StartFrom() Cursor {
return q.startCursor
}
func (q theQuery) Offset() int {
return q.offset
}
func (q theQuery) Limit() int {
return q.limit
}
func (q theQuery) String() string {
writer := bytes.NewBuffer(make([]byte, 0, 1024))
writer.WriteString("SELECT")
if q.limit > 0 {
writer.WriteString(" TOP " + strconv.Itoa(q.limit))
}
is1liner := len(q.columns) <= 1 &&
(q.where == nil || reflect.TypeOf(q.where) == reflect.TypeOf(Comparison{}))
switch len(q.columns) {
case 0:
writer.WriteString(" *")
case 1:
_, _ = fmt.Fprint(writer, " ", q.columns[0].String())
default:
for i, col := range q.columns {
_, _ = fmt.Fprint(writer, "\n\t", col.String())
if i < len(q.columns)-1 {
writer.WriteString(",")
}
}
}
if q.from != nil {
if is1liner {
writer.WriteString(" ")
} else {
writer.WriteString("\n")
}
writer.WriteString(fmt.Sprintf("FROM [%v]", q.from.Path()))
}
if q.where != nil {
if is1liner {
writer.WriteString(" ")
} else {
writer.WriteString("\n")
}
writer.WriteString("WHERE " + q.where.String())
}
if len(q.groupBy) > 0 {
writer.WriteString("\nGROUP BY ")
for i, expr := range q.groupBy {
if i > 0 {
writer.WriteString(", ")
}
writer.WriteString(expr.String())
}
}
if len(q.orderBy) > 0 {
writer.WriteString("\nORDER BY ")
for i, expr := range q.orderBy {
if i > 0 {
writer.WriteString(", ")
}
writer.WriteString(expr.String())
}
}
if q.offset > 0 {
writer.WriteString("\nOFFSET " + strconv.Itoa(q.offset))
}
return writer.String()
}
var _ fmt.Stringer = (*theQuery)(nil)
// And creates a new query by adding a condition to a predefined query
func (q theQuery) groupWithConditions(operator Operator, conditions ...Condition) theQuery {
qry := theQuery{from: q.from}
and := GroupCondition{operator: operator, conditions: make([]Condition, len(conditions)+1)}
and.conditions[0] = q.where
for i, condition := range conditions {
and.conditions[i+1] = condition
}
qry.where = and
return qry
}
// And creates an inherited query by adding AND conditions
func (q theQuery) And(conditions ...Condition) theQuery {
return q.groupWithConditions(And, conditions...)
}
// Or creates an inherited query by adding OR conditions
func (q theQuery) Or(conditions ...Condition) theQuery {
return q.groupWithConditions(Or, conditions...)
}