/
types.go
223 lines (172 loc) · 5.24 KB
/
types.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
package goq
// Selection provides information about a selected expression.
type Selection struct {
Alias string
ColumnName string
TableName string
TableAlias string
StructName string
FieldName string
}
// DBContext abstructs query syntax differences among RDBs.
type DBContext interface {
Placeholder(typ string, prevArgs []interface{}) string
QuoteIdent(v string) string
}
// QueryApplier is the interface to append query parts.
// All query part structs implement this.
type QueryApplier interface {
Apply(q *Query, ctx DBContext)
}
// Selectable represents a selectable expression.
// QueryBuilder.Select accepts values that implement this interface.
type Selectable interface {
QueryApplier
Selection() Selection
}
// Aliased is an aliased expression.
// You cannot operate an aliased expression.
// For example 'A.Add(B.As("b"))' fails to compile.
type Aliased interface {
Selectable
Alias() string
}
// Expr is the interface of expressions that provide basic operators.
// All expression structs implement this interface.
type Expr interface {
Selectable
// Eq does '=' comparison (equal to).
Eq(v interface{}) PredExpr
// Neq does '<>' comparison (not equal to).
Neq(v interface{}) PredExpr
// Gt does '>' comparison (greater than).
Gt(v interface{}) PredExpr
// Gte does '>=' comparison (greater than or equal to).
Gte(v interface{}) PredExpr
// Lt does '<' comparison (less than).
Lt(v interface{}) PredExpr
// Lte does '<=' comparison (less than or equal to).
Lte(v interface{}) PredExpr
// Add does '+' operation (addition).
Add(v interface{}) AnonExpr
// Sbt does '-' operation (subtraction).
Sbt(v interface{}) AnonExpr
// Mlt does '*' operation (multiplication).
Mlt(v interface{}) AnonExpr
// Dvd does '/' operation (division).
Dvd(v interface{}) AnonExpr
// Concat concats a string by '||'.
Concat(s interface{}) AnonExpr
// Like does 'LIKE' comparison.
// For example, 'Title.Like("I am%")' becomes true if
// the 'Title' starts with 'I am'.
Like(s string) PredExpr
// Between does 'BETWEEN' comparison.
Between(start interface{}, end interface{}) PredExpr
// In constructs 'IN' comparison.
// You must pass a slice or query.
// Otherwise a query construction will result in an error.
In(valsOrQuery interface{}) PredExpr
// IsNull does 'IS NULL' comparison.
IsNull() PredExpr
// IsNotNull does 'IS NOT NULL' comparison.
IsNotNull() PredExpr
}
// AnonExpr represents an anonymous expression that does not have an alias.
type AnonExpr interface {
Expr
As(alias string) Aliased
}
// PredExpr represents a predicate expression.
// Some clauses like 'WHERE' or 'ON' accept predicate expressions only.
type PredExpr interface {
AnonExpr
ImplPredExpr()
}
// TableLike represents an expression that can be a table.
// Addition to database tables,
// aliased queries can also be used for 'FROM' or 'JOIN'.
// This means a query implements TableLike as well.
type TableLike interface {
ApplyTable(q *Query, ctx DBContext)
}
// QueryTable represents a query as a table.
type QueryTable interface {
Selectable
TableLike
}
// SchemaTable represents a database table.
type SchemaTable interface {
TableLike
// All constructs the column list expression that has all columns.
All() *ColumnList
// Except constructs the column list expression that has all columns except columns you specify.
Except(cols ...*Column) *ColumnList
}
// QueryRoot is the interface of query statements
// such as 'SELECT', 'CREATE', 'UPDATE', and so on.
type QueryRoot interface {
// Construct constructs a query.
Construct() (Query, error)
}
// QueryExpr is the interface of a query to select data.
type QueryExpr interface {
Expr
QueryRoot
// Select overrides the selections.
Select(exps ...Selectable) Clauses
// As gives an alias to the query.
As(alias string) QueryTable
// Selections lists selected data.
Selections() []Selection
// OrderBy adds 'ORDER BY' clause.
OrderBy(ords ...Orderer) QueryExpr
// Limit adds 'LIMIT' clause.
Limit(n int) QueryExpr
// Offset adds 'OFFSET' clause.
Offset(n int) QueryExpr
// WithLimits copy this QueryExpr shallowly and set 'LIMIT' and 'OFFSET'.
WithLimits(limit, offset int) QueryExpr
}
// SelectClause is a 'SELECT' clause.
type SelectClause interface {
QueryExpr
// From constructs a 'FROM' clause.
From(table TableLike, tables ...TableLike) Clauses
}
// Clauses has 'JOIN', 'WHERE', and 'GROUP BY' clauses.
// You can call 'WHERE' and 'JOIN' multiple times.
// All given expressions are appended to the query.
type Clauses interface {
QueryExpr
// Joins constructs a 'JOIN' clauses.
Joins(joins ...JoinDefiner) Clauses
// Where constructs a 'WHERE' clause.
Where(preds ...PredExpr) Clauses
// GroupBy constructs a 'GROUP BY' clasue.
GroupBy(exps ...Expr) GroupByClause
}
// GroupByClause is a 'GROUP BY' clause.
type GroupByClause interface {
QueryExpr
// Having constructs a 'HAVING' clause.
Having(preds ...PredExpr) GroupByClause
}
// Orderer is the interface of expressions for 'ORDER BY'.
type Orderer interface {
Ordering() Ordering
}
// Orders.
const (
ORDER_ASC = "ASC"
ORDER_DESC = "DESC"
)
type Order string
// Ordering is a combination of an expression and an order.
type Ordering struct {
expr Expr
order Order
}
func (ord Ordering) Ordering() Ordering {
return ord
}