-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathquerydefs.go
258 lines (229 loc) · 6.6 KB
/
querydefs.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
// Copyright 2019 Yunion
//
// 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.
package sqlchemy
import (
"bytes"
"fmt"
"sort"
)
// IQuery is an interface that reprsents a SQL query, e.g.
// SELECT ... FROM ... WHERE ...
type IQuery interface {
// String returns the queryString
String(fields ...IQueryField) string
// QueryFields returns fields in the select clause
QueryFields() []IQueryField
// Variables returns variables in statement
Variables() []interface{}
// SubQuery convert this SQL to a subquery
SubQuery() *SSubQuery
// Field reference to a field by name
Field(name string) IQueryField
// Database returns the database for this query
database() *SDatabase
}
// IQuerySource is an interface that represents a data source of a SQL query. the source can be a table or a subquery
// e.g. SELECT ... FROM (SELECT * FROM tbl) AS A
type IQuerySource interface {
// Expression string in select ... from (expresson here)
Expression() string
// Alias is the alias in select ... from (express) as alias
Alias() string
// variables in statement
Variables() []interface{}
// Field reference to a field by name, optionally giving an alias name
Field(id string, alias ...string) IQueryField
// Fields return all the fields that this source provides
Fields() []IQueryField
// Database returns the database of this IQuerySource
database() *SDatabase
}
// IQueryField is an interface that represents a select field in a SQL query
type IQueryField interface {
// the string after select
Expression() string
// the name of thie field
Name() string
// the reference string in where clause
Reference() string
// give this field an alias name
Label(label string) IQueryField
// return variables
Variables() []interface{}
// ConvertFromValue returns the SQL representation of a value for this
ConvertFromValue(val interface{}) interface{}
// Database returns the database of this IQuerySource
database() *SDatabase
}
// DoQuery returns a SQuery instance that query specified fields from a query source
func DoQuery(from IQuerySource, f ...IQueryField) *SQuery {
if from.database() == nil {
panic("DoQuery IQuerySource with empty database")
}
// if len(f) == 0 {
// f = from.Fields()
// }
tq := SQuery{fields: f, from: from, db: from.database()}
return &tq
}
func queryString(tq *SQuery, tmpFields ...IQueryField) string {
if len(tq.rawSql) > 0 {
return tq.rawSql
}
qChar := tq.database().backend.QuoteChar()
var buf bytes.Buffer
buf.WriteString("SELECT ")
if tq.distinct {
buf.WriteString("DISTINCT ")
}
fields := tmpFields
if len(fields) == 0 {
if len(tq.fields) > 0 {
fields = append(fields, tq.fields...)
} else {
fields = append(fields, tq.from.Fields()...)
for i := range fields {
tq.from.Field(fields[i].Name())
}
}
}
{
// add reference query fields
queryFields := make(map[string]IQueryField)
for i, f := range fields {
queryFields[f.Name()] = fields[i]
}
refFields := make([]IQueryField, 0)
for _, f := range tq.refFieldMap {
if _, ok := queryFields[f.Name()]; !ok {
queryFields[f.Name()] = f
refFields = append(refFields, f)
}
}
sort.Sort(queryFieldList(refFields))
fields = append(fields, refFields...)
}
groupFields := make(map[string]IQueryField)
if len(tq.groupBy) > 0 {
for i := range tq.groupBy {
f := tq.groupBy[i]
groupFields[f.Name()] = f
}
}
for i := range fields {
if i > 0 {
buf.WriteString(", ")
}
f := fields[i]
if len(tq.groupBy) > 0 {
if gf, ok := groupFields[f.Name()]; ok && f.Reference() == gf.Reference() {
// in group by, normal
buf.WriteString(f.Expression())
} else {
// not in group by, check if the field is a group aggregate function
if gf, ok := f.(IFunctionQueryField); ok && gf.IsAggregate() {
// is a aggregate function field
buf.WriteString(f.Expression())
} else {
f = MAX(f.Name(), f)
buf.WriteString(f.Expression())
}
}
} else {
// normal
buf.WriteString(f.Expression())
}
if f.Name() != "" {
buf.WriteString(fmt.Sprintf(" AS %s%s%s", qChar, f.Name(), qChar))
}
}
buf.WriteString(" FROM ")
buf.WriteString(fmt.Sprintf("%s AS %s%s%s", tq.from.Expression(), qChar, tq.from.Alias(), qChar))
for _, join := range tq.joins {
buf.WriteByte(' ')
buf.WriteString(string(join.jointype))
buf.WriteByte(' ')
buf.WriteString(fmt.Sprintf("%s AS %s%s%s", join.from.Expression(), qChar, join.from.Alias(), qChar))
whereCls := join.condition.WhereClause()
if len(whereCls) > 0 {
buf.WriteString(" ON ")
buf.WriteString(whereCls)
}
}
if tq.where != nil {
whereCls := tq.where.WhereClause()
if len(whereCls) > 0 {
buf.WriteString(" WHERE ")
buf.WriteString(whereCls)
}
}
if tq.groupBy != nil && len(tq.groupBy) > 0 {
buf.WriteString(" GROUP BY ")
groupByFields := make(map[string]IQueryField)
for i := range tq.groupBy {
f := tq.groupBy[i]
if _, ok := groupByFields[f.Reference()]; ok {
continue
}
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(f.Reference())
groupByFields[f.Reference()] = f
}
// DAMENG SQL Compatibility, all order by fields should be in group by
for i := range tq.orderBy {
f := tq.orderBy[i]
if _, ok := groupByFields[f.field.Reference()]; ok {
continue
}
if ff, ok := f.field.(IFunctionQueryField); ok && ff.IsAggregate() {
continue
}
buf.WriteString(", ")
buf.WriteString(f.field.Reference())
groupByFields[f.field.Reference()] = f.field
}
}
/*if tq.having != nil {
buf.WriteString(" HAVING ")
buf.WriteString(tq.having.WhereClause())
}*/
if tq.orderBy != nil && len(tq.orderBy) > 0 {
buf.WriteString(" ORDER BY ")
for i := range tq.orderBy {
f := tq.orderBy[i]
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprintf("%s %s", f.field.Reference(), f.order))
}
}
if tq.limit > 0 {
buf.WriteString(fmt.Sprintf(" LIMIT %d", tq.limit))
}
if tq.offset > 0 {
buf.WriteString(fmt.Sprintf(" OFFSET %d", tq.offset))
}
return buf.String()
}
func getFieldBackend(fields ...IQueryField) IBackend {
for _, f := range fields {
db := f.database()
if db != nil {
return db.backend
}
}
return defaultBackend
}