-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
167 lines (142 loc) · 2.68 KB
/
table.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
package orm
type TableReference interface {
tableAlias() string
}
var _ TableReference = Table{}
var _ TableReference = Join{}
var _ TableReference = Subquery{}
var _ TableReference = &Union{}
type Table struct {
entity any
alias string
}
func (t Table) tableAlias() string {
return t.alias
}
func TableOf(entity any) Table {
return Table{
entity: entity,
}
}
func (t Table) C(name string) Column {
return Column{
name: name,
table: t,
}
}
func (t Table) As(alias string) Table {
return Table{
entity: t.entity,
alias: alias,
}
}
func (t Table) Join(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: t,
right: target,
typ: "JOIN",
}
}
func (t Table) LeftJoin(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: t,
right: target,
typ: "LEFT JOIN",
}
}
func (t Table) RightJoin(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: t,
right: target,
typ: "RIGHT JOIN",
}
}
type JoinBuilder struct {
left TableReference
right TableReference
typ string
}
func (j *JoinBuilder) On(ps ...Predicate) Join {
pres := &predicates{ps: ps}
return Join{
typ: j.typ,
on: pres,
left: j.left,
right: j.right,
}
}
func (j *JoinBuilder) Using(cs ...string) Join {
return Join{
typ: j.typ,
using: cs,
left: j.left,
right: j.right,
}
}
type Join struct {
typ string
on *predicates
left TableReference
right TableReference
using []string
}
func (j Join) tableAlias() string {
return ""
}
func (j Join) Join(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: j,
right: target,
typ: "JOIN",
}
}
func (j Join) LeftJoin(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: j,
right: target,
typ: "LEFT JOIN",
}
}
func (j Join) RightJoin(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: j,
right: target,
typ: "RIGHT JOIN",
}
}
type Subquery struct {
// 使用 QueryBuilder 仅仅是为了让 Subquery 可以是非泛型的。
s QueryBuilder
table TableReference
// select 查询的列
columns []Selectable
alias string
}
func (s Subquery) expr() {}
func (s Subquery) tableAlias() string {
return s.alias
}
func (s Subquery) C(name string) Column {
return Column{table: s, name: name}
}
func (s Subquery) Join(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: s,
right: target,
typ: "JOIN",
}
}
func (s Subquery) LeftJoin(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: s,
right: target,
typ: "LEFT JOIN",
}
}
func (s Subquery) RightJoin(target TableReference) *JoinBuilder {
return &JoinBuilder{
left: s,
right: target,
typ: "RIGHT JOIN",
}
}