forked from doug-martin/goqu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapters.go
268 lines (261 loc) · 10.4 KB
/
adapters.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
package goqu
import (
"reflect"
"strings"
"time"
)
type (
//An adapter interface to be used by a Dataset to generate SQL for a specific dialect.
//See DefaultAdapter for a concrete implementation and examples.
Adapter interface {
//Returns true if the dialect supports ORDER BY expressions in DELETE statements
SupportsOrderByOnDelete() bool
//Returns true if the dialect supports ORDER BY expressions in UPDATE statements
SupportsOrderByOnUpdate() bool
//Returns true if the dialect supports LIMIT expressions in DELETE statements
SupportsLimitOnDelete() bool
//Returns true if the dialect supports LIMIT expressions in UPDATE statements
SupportsLimitOnUpdate() bool
//Returns true if the dialect supports RETURN expressions
SupportsReturn() bool
//Generates the sql for placeholders. Only invoked when not interpolating values.
//
//buf: The current SqlBuilder to write the sql to
//i: the value that should be added the the sqlbuilders args.
PlaceHolderSql(buf *SqlBuilder, i interface{}) error
//Generates the correct beginning sql for an UPDATE statement
//
//buf: The current SqlBuilder to write the sql to
UpdateBeginSql(buf *SqlBuilder) error
//Generates the correct beginning sql for an INSERT statement
//
//buf: The current SqlBuilder to write the sql to
InsertBeginSql(buf *SqlBuilder, o ConflictExpression) error
//Generates the correct beginning sql for a DELETE statement
//
//buf: The current SqlBuilder to write the sql to
DeleteBeginSql(buf *SqlBuilder) error
//Generates the correct beginning sql for a TRUNCATE statement
//
//buf: The current SqlBuilder to write the sql to
TruncateSql(buf *SqlBuilder, cols ColumnList, opts TruncateOptions) error
//Generates the correct sql for inserting default values in SQL
//
//buf: The current SqlBuilder to write the sql to
DefaultValuesSql(buf *SqlBuilder) error
//Generates the sql for update expressions
//
//buf: The current SqlBuilder to write the sql to
UpdateExpressionsSql(buf *SqlBuilder, updates ...UpdateExpression) error
//Generates the sql for the SELECT and ColumnList for a select statement
//
//buf: The current SqlBuilder to write the sql to
SelectSql(buf *SqlBuilder, cols ColumnList) error
//Generates the sql for the SELECT DISTINCT and ColumnList for a select statement
//
//buf: The current SqlBuilder to write the sql to
SelectDistinctSql(buf *SqlBuilder, cols ColumnList) error
//Generates the sql for a RETURNING clause
//
//buf: The current SqlBuilder to write the sql to
ReturningSql(buf *SqlBuilder, cols ColumnList) error
//Generates the sql for a FROM clause
//
//buf: The current SqlBuilder to write the sql to
FromSql(buf *SqlBuilder, from ColumnList) error
//Generates the sql for a list of columns.
//
//buf: The current SqlBuilder to write the sql to
SourcesSql(buf *SqlBuilder, from ColumnList) error
//Generates the sql for JoiningClauses clauses
//
//buf: The current SqlBuilder to write the sql to
JoinSql(buf *SqlBuilder, joins JoiningClauses) error
//Generates the sql for WHERE clause
//
//buf: The current SqlBuilder to write the sql to
WhereSql(buf *SqlBuilder, where ExpressionList) error
//Generates the sql for GROUP BY clause
//
//buf: The current SqlBuilder to write the sql to
GroupBySql(buf *SqlBuilder, groupBy ColumnList) error
//Generates the sql for HAVING clause
//
//buf: The current SqlBuilder to write the sql to
HavingSql(buf *SqlBuilder, having ExpressionList) error
//Generates the sql for COMPOUND expressions, such as UNION, and INTERSECT
//
//buf: The current SqlBuilder to write the sql to
CompoundsSql(buf *SqlBuilder, compounds []CompoundExpression) error
//Generates the sql for the WITH clauses for common table expressions (CTE)
//
//buf: The current SqlBuilder to write the sql to
CommonTablesSql(buf *SqlBuilder, ctes []CommonTableExpression) error
//Generates the sql for ORDER BY clause
//
//buf: The current SqlBuilder to write the sql to
OrderSql(buf *SqlBuilder, order ColumnList) error
//Generates the sql for LIMIT clause
//
//buf: The current SqlBuilder to write the sql to
LimitSql(buf *SqlBuilder, limit interface{}) error
//Generates the sql for OFFSET clause
//
//buf: The current SqlBuilder to write the sql to
OffsetSql(buf *SqlBuilder, offset uint) error
//Generates the sql for another Dataset being used as a sub select.
//
//buf: The current SqlBuilder to write the sql to
DatasetSql(buf *SqlBuilder, builder Dataset) error
//Correctly quotes an Identifier for use in SQL.
//
//buf: The current SqlBuilder to write the sql to
QuoteIdentifier(buf *SqlBuilder, ident IdentifierExpression) error
//Generates SQL value for nil
//
//buf: The current SqlBuilder to write the sql to
LiteralNil(buf *SqlBuilder) error
//Generates SQL value for a bool (e.g. TRUE, FALSE, 1, 0)
//
//buf: The current SqlBuilder to write the sql to
LiteralBool(buf *SqlBuilder, b bool) error
//Generates SQL value for a time.Time
//
//buf: The current SqlBuilder to write the sql to
LiteralTime(buf *SqlBuilder, t time.Time) error
//Generates SQL value for float64
//
//buf: The current SqlBuilder to write the sql to
LiteralFloat(buf *SqlBuilder, f float64) error
//Generates SQL value for an int64
//
//buf: The current SqlBuilder to write the sql to
LiteralInt(buf *SqlBuilder, i int64) error
//Generates SQL value for a string
//
//buf: The current SqlBuilder to write the sql to
LiteralString(buf *SqlBuilder, s string) error
//Generates SQL value for a Slice of Bytes
//
//buf: The current SqlBuilder to write the sql to
LiteralBytes(buf *SqlBuilder, bs []byte) error
//Generates SQL value for a Slice
//
//buf: The current SqlBuilder to write the sql to
SliceValueSql(buf *SqlBuilder, slice reflect.Value) error
//Generates SQL value for an AliasedExpression
//
//buf: The current SqlBuilder to write the sql to
AliasedExpressionSql(buf *SqlBuilder, aliased AliasedExpression) error
//Generates SQL value for a BooleanExpression
//
//buf: The current SqlBuilder to write the sql to
BooleanExpressionSql(buf *SqlBuilder, operator BooleanExpression) error
//Generates SQL value for a RangeExpression
//
//buf: The current SqlBuilder to write the sql to
RangeExpressionSql(buf *SqlBuilder, operator RangeExpression) error
//Generates SQL value for an OrderedExpression
//
//buf: The current SqlBuilder to write the sql to
OrderedExpressionSql(buf *SqlBuilder, order OrderedExpression) error
//Generates SQL value for an ExpressionList
//
//buf: The current SqlBuilder to write the sql to
ExpressionListSql(buf *SqlBuilder, expressionList ExpressionList) error
//Generates SQL value for a SqlFunction
//
//buf: The current SqlBuilder to write the sql to
SqlFunctionExpressionSql(buf *SqlBuilder, sqlFunc SqlFunctionExpression) error
//Generates SQL value for a CastExpression
//
//buf: The current SqlBuilder to write the sql to
CastExpressionSql(buf *SqlBuilder, casted CastExpression) error
//Generates SQL value for a CompoundExpression
//
//buf: The current SqlBuilder to write the sql to
CompoundExpressionSql(buf *SqlBuilder, compound CompoundExpression) error
//Generates SQL value for a CommonTableExpression
//
//buf: The current SqlBuilder to write the sql to
CommonTableExpressionSql(buf *SqlBuilder, commonTable CommonTableExpression) error
//Generates SQL value for a ColumnList
//
//buf: The current SqlBuilder to write the sql to
ColumnListSql(buf *SqlBuilder, columnList ColumnList) error
//Generates SQL value for an UpdateExpression
//
//buf: The current SqlBuilder to write the sql to
UpdateExpressionSql(buf *SqlBuilder, update UpdateExpression) error
Literal(buf *SqlBuilder, i interface{}) error
//Generates SQL value for a LiteralExpression
//
//buf: The current SqlBuilder to write the sql to
LiteralExpressionSql(buf *SqlBuilder, literal LiteralExpression) error
//Generates SQL value for an Ex Expression map
//
//buf: The current SqlBuilder to write the sql to
ExpressionMapSql(buf *SqlBuilder, ex Ex) error
//Generates SQL value for an ExOr Expression map
//
//buf: The current SqlBuilder to write the sql to
ExpressionOrMapSql(buf *SqlBuilder, ex ExOr) error
//Generates SQL value for the columns in an INSERT statement
//
//buf: The current SqlBuilder to write the sql to
InsertColumnsSql(buf *SqlBuilder, cols ColumnList) error
//Generates SQL value for the values in an INSERT statement
//
//buf: The current SqlBuilder to write the sql to
InsertValuesSql(buf *SqlBuilder, values [][]interface{}) error
//Returns true if the dialect supports INSERT IGNORE INTO syntax
SupportsInsertIgnoreSyntax() bool
//Returns true if the dialect supports ON CONFLICT (key) expressions
SupportsConflictTarget() bool
//Generates SQL value for the ON CONFLICT clause of an INSERT statement
//
//buf: The current SqlBuilder to write the sql to
OnConflictSql(buf *SqlBuilder, o ConflictExpression) error
//Returns true if the dialect supports a WHERE clause on upsert
SupportConflictUpdateWhere() bool
//Returns true if the dialect supports WITH common table expressions
SupportsWithCTE() bool
//Returns true if the dialect supports WITH RECURSIVE common table expressions
SupportsWithRecursiveCTE() bool
}
)
var ds_adapters = make(map[string]func(dataset *Dataset) Adapter)
//Registers an adapter.
//
// dialect: The dialect this adapter is for
// factory: a function that can be called to create a new Adapter for the dialect.
func RegisterAdapter(dialect string, factory func(ds *Dataset) Adapter) {
dialect = strings.ToLower(dialect)
ds_adapters[dialect] = factory
}
//Returns true if the dialect has an adapter registered
//
// dialect: The dialect to test
func HasAdapter(dialect string) bool {
dialect = strings.ToLower(dialect)
_, ok := ds_adapters[dialect]
return ok
}
//Clears the current adapter map, used internally for tests
func removeAdapter(dialect string) {
if HasAdapter(dialect) {
dialect = strings.ToLower(dialect)
delete(ds_adapters, dialect)
}
}
//Creates the appropriate adapter for the given dialect.
//
// dialect: the dialect to create an adapter for
// dataset: The dataset to be used by the adapter
func NewAdapter(dialect string, dataset *Dataset) Adapter {
dialect = strings.ToLower(dialect)
if adapterGen, ok := ds_adapters[dialect]; ok {
return adapterGen(dataset)
}
return NewDefaultAdapter(dataset)
}