-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathunion.go
279 lines (242 loc) · 6.84 KB
/
union.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
// 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 (
"database/sql"
"fmt"
"strings"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
)
// SUnionQueryField represents a field of a union query
type SUnionQueryField struct {
union *SUnion
name string
alias string
}
// Expression implementation of SUnionQueryField for IQueryField
func (sqf *SUnionQueryField) Expression() string {
qChar := sqf.union.database().backend.QuoteChar()
return fmt.Sprintf("%s%s%s.%s%s%s", qChar, sqf.union.Alias(), qChar, qChar, sqf.name, qChar)
}
// Name implementation of SUnionQueryField for IQueryField
func (sqf *SUnionQueryField) Name() string {
if len(sqf.alias) > 0 {
return sqf.alias
}
return sqf.name
}
// Reference implementation of SUnionQueryField for IQueryField
func (sqf *SUnionQueryField) Reference() string {
qChar := sqf.union.database().backend.QuoteChar()
return fmt.Sprintf("%s%s%s.%s%s%s", qChar, sqf.union.Alias(), qChar, qChar, sqf.Name(), qChar)
}
// Label implementation of SUnionQueryField for IQueryField
func (sqf *SUnionQueryField) Label(label string) IQueryField {
if len(label) > 0 {
nsqf := *sqf
nsqf.alias = label
return &nsqf
} else {
return sqf
}
}
// Variables implementation of SUnionQueryField for IQueryField
func (sqf *SUnionQueryField) Variables() []interface{} {
return nil
}
// ConvertFromValue implementation of SUnionQueryField for IQueryField
func (sqf *SUnionQueryField) ConvertFromValue(val interface{}) interface{} {
for _, query := range sqf.union.queries {
field := query.Field(sqf.name)
if field != nil {
return field.ConvertFromValue(val)
}
}
return val
}
func (sqf *SUnionQueryField) database() *SDatabase {
return sqf.union.database()
}
// SUnion is the struct to store state of a Union query, which implementation the interface of IQuerySource
type SUnion struct {
alias string
queries []IQuery
fields []IQueryField
// orderBy []sQueryOrder
// limit int
// offset int
isAll bool
}
// Alias implementation of SUnion for IQuerySource
func (uq *SUnion) Alias() string {
return uq.alias
}
func (uq *SUnion) operator() string {
if uq.isAll {
return uq.database().backend.UnionAllString()
} else {
return uq.database().backend.UnionDistinctString()
}
}
// Expression implementation of SUnion for IQuerySource
func (uq *SUnion) Expression() string {
var buf strings.Builder
buf.WriteString("(")
for i := range uq.queries {
if i != 0 {
buf.WriteByte(' ')
buf.WriteString(uq.operator())
buf.WriteByte(' ')
}
subQ := uq.queries[i].SubQuery()
buf.WriteString(subQ.Query().String())
}
/*if uq.orderBy != nil && len(uq.orderBy) > 0 {
buf.WriteString(" ORDER BY ")
for i, f := range uq.orderBy {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(fmt.Sprintf("%s %s", f.field.Reference(), f.order))
}
}
if uq.limit > 0 {
buf.WriteString(fmt.Sprintf(" LIMIT %d", uq.limit))
}
if uq.offset > 0 {
buf.WriteString(fmt.Sprintf(" OFFSET %d", uq.offset))
}*/
buf.WriteByte(')')
return buf.String()
}
/*func (tq *SUnion) _orderBy(order QueryOrderType, fields []IQueryField) *SUnion {
if tq.orderBy == nil {
tq.orderBy = make([]SQueryOrder, 0)
}
for _, f := range fields {
tq.orderBy = append(tq.orderBy, SQueryOrder{field: f, order: order})
}
return tq
}
func (tq *SUnion) Asc(fields ...interface{}) *SUnion {
return tq._orderBy(SQL_ORDER_ASC, convertQueryField(tq, fields))
}
func (tq *SUnion) Desc(fields ...interface{}) *SUnion {
return tq._orderBy(SQL_ORDER_DESC, convertQueryField(tq, fields))
}
*/
// Limit adds limit to a union query
// func (uq *SUnion) Limit(limit int) *SUnion {
// uq.limit = limit
// return uq
// }
// Offset adds offset to a union query
// func (uq *SUnion) Offset(offset int) *SUnion {
// uq.offset = offset
// return uq
// }
// Fields implementation of SUnion for IQuerySource
func (uq *SUnion) Fields() []IQueryField {
return uq.fields
}
// Field implementation of SUnion for IQuerySource
func (uq *SUnion) Field(name string, alias ...string) IQueryField {
for i := range uq.fields {
if name == uq.fields[i].Name() {
if len(alias) > 0 {
uq.fields[i].Label(alias[0])
}
return uq.fields[i]
}
}
return nil
}
// Variables implementation of SUnion for IQuerySource
func (uq *SUnion) Variables() []interface{} {
ret := make([]interface{}, 0)
for i := range uq.queries {
ret = append(ret, uq.queries[i].Variables()...)
}
return ret
}
// Database implementation of SUnion for IQUerySource
func (uq *SUnion) database() *SDatabase {
for _, q := range uq.queries {
db := q.database()
if db != nil {
return db
}
}
return nil
}
// Union method returns union query of several queries.
// Require the fields of all queries should exactly match
// deprecated
func Union(query ...IQuery) *SUnion {
u, err := UnionWithError(query...)
if err != nil {
log.Fatalf("Fatal: %s", err.Error())
}
return u
}
// UnionWithError constructs union query of several Queries
// Require the fields of all queries should exactly match
func UnionWithError(query ...IQuery) (*SUnion, error) {
return unionWithError(false, query...)
}
func UnionAllWithError(query ...IQuery) (*SUnion, error) {
return unionWithError(true, query...)
}
func unionWithError(isAll bool, query ...IQuery) (*SUnion, error) {
if len(query) == 0 {
return nil, errors.Wrap(sql.ErrNoRows, "empty union query")
}
fieldNames := make([]string, 0)
for _, f := range query[0].QueryFields() {
fieldNames = append(fieldNames, f.Name())
}
var db *SDatabase
for i := 1; i < len(query); i++ {
if db == nil {
db = query[i].database()
} else if db != query[i].database() {
panic(ErrUnionAcrossDatabases)
}
qfields := query[i].QueryFields()
if len(fieldNames) != len(qfields) {
return nil, errors.Wrap(ErrUnionFieldsNotMatch, "number not match")
}
for i := range qfields {
if fieldNames[i] != qfields[i].Name() {
return nil, errors.Wrapf(ErrUnionFieldsNotMatch, "name %s:%s not match", fieldNames[i], qfields[i].Name())
}
}
}
fields := make([]IQueryField, len(fieldNames))
uq := &SUnion{
alias: getTableAliasName(),
queries: query,
fields: fields,
isAll: isAll,
}
for i := range fieldNames {
fields[i] = &SUnionQueryField{name: fieldNames[i], union: uq}
}
return uq, nil
}
// Query of SUnion returns a SQuery of a union query
func (uq *SUnion) Query(f ...IQueryField) *SQuery {
return DoQuery(uq, f...)
}