-
Notifications
You must be signed in to change notification settings - Fork 92
/
select.go
206 lines (178 loc) · 4.49 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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package ast
import (
"strings"
)
import (
"github.com/pkg/errors"
)
var (
_ Statement = (*SelectStatement)(nil)
_ Node = (*SelectStatement)(nil)
_ Restorer = (*SelectStatement)(nil)
)
const (
_ SelectLock = 1 << iota
SelectLockForUpdate
SelectLockInShardMode
)
var _selectLockNames = [...]string{
SelectLockForUpdate: "FOR UPDATE",
SelectLockInShardMode: "LOCK IN SHARE MODE",
}
type SelectLock uint8
func (sl SelectLock) String() string {
return _selectLockNames[sl]
}
type SelectStatement struct {
Select SelectNode
From FromNode
Where ExpressionNode
GroupBy *GroupByNode
Having ExpressionNode
OrderBy OrderByNode
Limit *LimitNode
Lock SelectLock
Distinct bool
Hint *HintNode
}
func (ss *SelectStatement) Accept(visitor Visitor) (interface{}, error) {
return visitor.VisitSelectStatement(ss)
}
func (ss *SelectStatement) Restore(flag RestoreFlag, sb *strings.Builder, args *[]int) error {
sb.WriteString("SELECT ")
if ss.Hint != nil {
if err := ss.Hint.Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
sb.WriteString(" ")
}
if ss.Distinct {
sb.WriteString(Distinct)
sb.WriteString(" ")
}
if err := ss.Select[0].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
for i := 1; i < len(ss.Select); i++ {
sb.WriteByte(',')
if err := ss.Select[i].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
if len(ss.From) > 0 {
sb.WriteString(" FROM ")
if err := ss.From[0].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
for i := 1; i < len(ss.From); i++ {
sb.WriteString(", ")
if err := ss.From[i].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
}
if ss.Where != nil {
sb.WriteString(" WHERE ")
if err := ss.Where.Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
if ss.GroupBy != nil {
sb.WriteString(" GROUP BY ")
if err := ss.GroupBy.Items[0].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
for i := 1; i < len(ss.GroupBy.Items); i++ {
sb.WriteByte(',')
if err := ss.GroupBy.Items[i].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
if ss.GroupBy.RollUp {
sb.WriteString(" WITH ROLLUP ")
}
}
if ss.Having != nil {
sb.WriteString(" HAVING ")
if err := ss.Having.Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
if len(ss.OrderBy) > 0 {
sb.WriteString(" ORDER BY ")
if err := ss.OrderBy[0].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
for i := 1; i < len(ss.OrderBy); i++ {
sb.WriteString(", ")
if err := ss.OrderBy[i].Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
}
if ss.Limit != nil {
sb.WriteString(" LIMIT ")
if err := ss.Limit.Restore(flag, sb, args); err != nil {
return errors.WithStack(err)
}
}
if ss.Lock != 0 {
sb.WriteByte(' ')
sb.WriteString(ss.Lock.String())
}
return nil
}
func (ss *SelectStatement) HasJoin() bool {
switch len(ss.From) {
case 0:
return false
case 1:
if len(ss.From[0].Joins) > 0 {
return true
}
switch it := ss.From[0].Source.(type) {
case *SelectStatement:
return it.HasJoin()
case *UnionSelectStatement:
if it.First.HasJoin() {
return true
}
for _, next := range it.UnionStatementItems {
if next.Stmt.HasJoin() {
return true
}
}
}
return false
default:
return true
}
}
func (ss *SelectStatement) HasSubQuery() bool {
for _, it := range ss.From {
switch it.Source.(type) {
case *SelectStatement, *UnionSelectStatement:
return true
}
}
return false
}
func (ss *SelectStatement) Mode() SQLType {
return SQLTypeSelect
}