forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
320 lines (271 loc) · 6.77 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
// This code was derived from https://github.com/youtube/vitess.
//
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package parser
import (
"bytes"
"fmt"
)
// SelectStatement any SELECT statement.
type SelectStatement interface {
Statement
selectStatement()
}
func (*ParenSelect) selectStatement() {}
func (*Select) selectStatement() {}
func (*Union) selectStatement() {}
func (Values) selectStatement() {}
// ParenSelect represents a parenthesized SELECT/UNION/VALUES statement.
type ParenSelect struct {
Select SelectStatement
}
func (node *ParenSelect) String() string {
return fmt.Sprintf("(%s)", node.Select)
}
// Select represents a SELECT statement.
type Select struct {
Distinct string
Exprs SelectExprs
From TableExprs
Where *Where
GroupBy GroupBy
Having *Where
OrderBy OrderBy
Limit *Limit
Lock string
tableSelect bool
}
// Select.Distinct
const (
astDistinct = " DISTINCT"
)
// Select.Lock
const (
astForUpdate = " FOR UPDATE"
astShareMode = " LOCK IN SHARE MODE"
)
func (node *Select) String() string {
if node.tableSelect && len(node.From) == 1 {
return fmt.Sprintf("TABLE %s", node.From[0])
}
return fmt.Sprintf("SELECT%s%s%s%s%s%s%s%s%s",
node.Distinct, node.Exprs,
node.From, node.Where,
node.GroupBy, node.Having, node.OrderBy,
node.Limit, node.Lock)
}
// SelectExprs represents SELECT expressions.
type SelectExprs []SelectExpr
func (node SelectExprs) String() string {
prefix := " "
var buf bytes.Buffer
for _, n := range node {
fmt.Fprintf(&buf, "%s%s", prefix, n)
prefix = ", "
}
return buf.String()
}
// SelectExpr represents a SELECT expression.
type SelectExpr struct {
Expr Expr
As Name
}
// StarSelectExpr is a convenience variable that represents an unqualified "*"
// in a select expression.
var StarSelectExpr = SelectExpr{Expr: StarExpr}
func (node SelectExpr) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s", node.Expr)
if node.As != "" {
fmt.Fprintf(&buf, " AS %s", node.As)
}
return buf.String()
}
// TableExprs represents a list of table expressions.
type TableExprs []TableExpr
func (node TableExprs) String() string {
if len(node) == 0 {
return ""
}
var prefix string
var buf bytes.Buffer
buf.WriteString(" FROM ")
for _, n := range node {
fmt.Fprintf(&buf, "%s%s", prefix, n)
prefix = ", "
}
return buf.String()
}
// TableExpr represents a table expression.
type TableExpr interface {
tableExpr()
}
func (*AliasedTableExpr) tableExpr() {}
func (*ParenTableExpr) tableExpr() {}
func (*JoinTableExpr) tableExpr() {}
// AliasedTableExpr represents a table expression coupled with an optional
// alias.
type AliasedTableExpr struct {
Expr SimpleTableExpr
As Name
}
func (node *AliasedTableExpr) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s", node.Expr)
if node.As != "" {
fmt.Fprintf(&buf, " AS %s", node.As)
}
return buf.String()
}
// SimpleTableExpr represents a simple table expression.
type SimpleTableExpr interface {
simpleTableExpr()
}
func (QualifiedName) simpleTableExpr() {}
func (*Subquery) simpleTableExpr() {}
// ParenTableExpr represents a parenthesized TableExpr.
type ParenTableExpr struct {
Expr TableExpr
}
func (node *ParenTableExpr) String() string {
return fmt.Sprintf("(%s)", node.Expr)
}
// JoinTableExpr represents a TableExpr that's a JOIN operation.
type JoinTableExpr struct {
Join string
Left TableExpr
Right TableExpr
Cond JoinCond
}
// JoinTableExpr.Join
const (
astJoin = "JOIN"
astFullJoin = "FULL JOIN"
astLeftJoin = "LEFT JOIN"
astRightJoin = "RIGHT JOIN"
astCrossJoin = "CROSS JOIN"
astNaturalJoin = "NATURAL JOIN"
astInnerJoin = "INNER JOIN"
)
func (node *JoinTableExpr) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s %s %s", node.Left, node.Join, node.Right)
if node.Cond != nil {
fmt.Fprintf(&buf, "%s", node.Cond)
}
return buf.String()
}
// JoinCond represents a join condition.
type JoinCond interface {
joinCond()
}
func (*OnJoinCond) joinCond() {}
func (*UsingJoinCond) joinCond() {}
// OnJoinCond represents an ON join condition.
type OnJoinCond struct {
Expr Expr
}
func (node *OnJoinCond) String() string {
return fmt.Sprintf(" ON %s", node.Expr)
}
// UsingJoinCond represents a USING join condition.
type UsingJoinCond struct {
Cols NameList
}
func (node *UsingJoinCond) String() string {
return fmt.Sprintf(" USING (%s)", node.Cols)
}
// Where represents a WHERE or HAVING clause.
type Where struct {
Type string
Expr Expr
}
// Where.Type
const (
astWhere = "WHERE"
astHaving = "HAVING"
)
// newWhere creates a WHERE or HAVING clause out of a Expr. If the expression
// is nil, it returns nil.
func newWhere(typ string, expr Expr) *Where {
if expr == nil {
return nil
}
return &Where{Type: typ, Expr: expr}
}
func (node *Where) String() string {
if node == nil {
return ""
}
return fmt.Sprintf(" %s %s", node.Type, node.Expr)
}
// GroupBy represents a GROUP BY clause.
type GroupBy []Expr
func (node GroupBy) String() string {
prefix := " GROUP BY "
var buf bytes.Buffer
for _, n := range node {
fmt.Fprintf(&buf, "%s%s", prefix, n)
prefix = ", "
}
return buf.String()
}
// OrderBy represents an ORDER By clause.
type OrderBy []*Order
func (node OrderBy) String() string {
prefix := " ORDER BY "
var buf bytes.Buffer
for _, n := range node {
fmt.Fprintf(&buf, "%s%s", prefix, n)
prefix = ", "
}
return buf.String()
}
// Order represents an ordering expression.
type Order struct {
Expr Expr
Direction string
}
// Order.Direction
const (
astAsc = " ASC"
astDesc = " DESC"
)
func (node *Order) String() string {
return fmt.Sprintf("%s%s", node.Expr, node.Direction)
}
// Limit represents a LIMIT clause.
type Limit struct {
Offset, Count Expr
}
func (node *Limit) String() string {
if node == nil {
return ""
}
var buf bytes.Buffer
if node.Count != nil {
fmt.Fprintf(&buf, " LIMIT %s", node.Count)
}
if node.Offset != nil {
fmt.Fprintf(&buf, " OFFSET %s", node.Offset)
}
return buf.String()
}