forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.go
149 lines (126 loc) · 3.04 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
package sqlchemy
import (
"fmt"
"strings"
"yunion.io/x/log"
)
type SUnionQueryField struct {
name string
}
func (sqf *SUnionQueryField) Expression() string {
return sqf.name
}
func (sqf *SUnionQueryField) Name() string {
return sqf.name
}
func (sqf *SUnionQueryField) Reference() string {
return sqf.name
}
func (sqf *SUnionQueryField) Label(label string) IQueryField {
return sqf
}
type SUnionQuery struct {
queries []IQuery
fields []IQueryField
orderBy []SQueryOrder
limit int
offset int
}
func (uq *SUnionQuery) String() string {
var buf strings.Builder
for i := range uq.queries {
if i != 0 {
buf.WriteString(" UNION ")
}
buf.WriteByte('(')
buf.WriteString(uq.queries[i].String())
buf.WriteByte(')')
}
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))
}
return buf.String()
}
func (tq *SUnionQuery) _orderBy(order QueryOrderType, fields []IQueryField) *SUnionQuery {
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 *SUnionQuery) Asc(fields ...interface{}) *SUnionQuery {
return tq._orderBy(SQL_ORDER_ASC, convertQueryField(tq, fields))
}
func (tq *SUnionQuery) Desc(fields ...interface{}) *SUnionQuery {
return tq._orderBy(SQL_ORDER_DESC, convertQueryField(tq, fields))
}
func (uq *SUnionQuery) Limit(limit int) *SUnionQuery {
uq.limit = limit
return uq
}
func (uq *SUnionQuery) Offset(offset int) *SUnionQuery {
uq.offset = offset
return uq
}
func (uq *SUnionQuery) QueryFields() []IQueryField {
return uq.fields
}
func (uq *SUnionQuery) Field(name string) IQueryField {
for i := range uq.fields {
if name == uq.fields[i].Name() {
return uq.fields[i]
}
}
return nil
}
func (uq *SUnionQuery) Variables() []interface{} {
ret := make([]interface{}, 0)
for i := range uq.queries {
ret = append(ret, uq.queries[i].Variables()...)
}
return ret
}
func (uq *SUnionQuery) SubQuery() *SSubQuery {
sq := SSubQuery{query: uq, alias: getTableAliasName()}
return &sq
}
func Union(query ...IQuery) *SUnionQuery {
fieldNames := make([]string, 0)
for _, f := range query[0].QueryFields() {
fieldNames = append(fieldNames, f.Name())
}
for i := 1; i < len(query); i += 1 {
qfields := query[i].QueryFields()
if len(fieldNames) != len(qfields) {
log.Fatalf("cannot union, number of fields not match!")
}
for i := range qfields {
if fieldNames[i] != qfields[i].Name() {
log.Fatalf("cannot union, name of fields not match!")
}
}
}
fields := make([]IQueryField, len(fieldNames))
for i := range fieldNames {
fields[i] = &SUnionQueryField{name: fieldNames[i]}
}
uq := &SUnionQuery{
queries: query,
fields: fields,
}
return uq
}