/
union.go
78 lines (66 loc) · 1.31 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
package mystmt
func Union(f func(b UnionStatement)) *Result {
var st unionStmt
f(&st)
return newResult(build(st.make()))
}
type UnionStatement interface {
Select(f func(b SelectStatement))
AllSelect(f func(b SelectStatement))
DistinctSelect(f func(b SelectStatement))
OrderBy(col string) OrderBy
Limit(n int64)
}
type unionStmt struct {
b buffer
orderBy group
limit *int64
}
func (st *unionStmt) Select(f func(b SelectStatement)) {
var x selectStmt
f(&x)
if st.b.empty() {
st.b.push(paren(x.make()))
} else {
st.b.push("union", paren(x.make()))
}
}
func (st *unionStmt) AllSelect(f func(b SelectStatement)) {
var x selectStmt
f(&x)
if st.b.empty() {
st.b.push(paren(x.make()))
} else {
st.b.push("union all", paren(x.make()))
}
}
func (st *unionStmt) DistinctSelect(f func(b SelectStatement)) {
var x selectStmt
f(&x)
if st.b.empty() {
st.b.push(paren(x.make()))
} else {
st.b.push("union distinct", paren(x.make()))
}
}
func (st *unionStmt) OrderBy(col string) OrderBy {
p := orderBy{
col: col,
}
st.orderBy.push(&p)
return &p
}
func (st *unionStmt) Limit(n int64) {
st.limit = &n
}
func (st *unionStmt) make() *buffer {
var b buffer
b.push(&st.b)
if !st.orderBy.empty() {
b.push("order by", &st.orderBy)
}
if st.limit != nil {
b.push("limit", *st.limit)
}
return &b
}