-
Notifications
You must be signed in to change notification settings - Fork 213
/
update_dataset.go
246 lines (208 loc) · 7.88 KB
/
update_dataset.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
package goqu
import (
"github.com/doug-martin/goqu/v9/exec"
"github.com/doug-martin/goqu/v9/exp"
"github.com/doug-martin/goqu/v9/internal/errors"
"github.com/doug-martin/goqu/v9/internal/sb"
)
type UpdateDataset struct {
dialect SQLDialect
clauses exp.UpdateClauses
isPrepared prepared
queryFactory exec.QueryFactory
err error
}
var ErrUnsupportedUpdateTableType = errors.New("unsupported table type, a string or identifier expression is required")
// used internally by database to create a database with a specific adapter
func newUpdateDataset(d string, queryFactory exec.QueryFactory) *UpdateDataset {
return &UpdateDataset{
clauses: exp.NewUpdateClauses(),
dialect: GetDialect(d),
queryFactory: queryFactory,
}
}
func Update(table interface{}) *UpdateDataset {
return newUpdateDataset("default", nil).Table(table)
}
// Set the parameter interpolation behavior. See examples
//
// prepared: If true the dataset WILL NOT interpolate the parameters.
func (ud *UpdateDataset) Prepared(prepared bool) *UpdateDataset {
ret := ud.copy(ud.clauses)
ret.isPrepared = preparedFromBool(prepared)
return ret
}
func (ud *UpdateDataset) IsPrepared() bool {
return ud.isPrepared.Bool()
}
// Sets the adapter used to serialize values and create the SQL statement
func (ud *UpdateDataset) WithDialect(dl string) *UpdateDataset {
ds := ud.copy(ud.GetClauses())
ds.dialect = GetDialect(dl)
return ds
}
// Returns the current adapter on the dataset
func (ud *UpdateDataset) Dialect() SQLDialect {
return ud.dialect
}
// Returns the current adapter on the dataset
func (ud *UpdateDataset) SetDialect(dialect SQLDialect) *UpdateDataset {
cd := ud.copy(ud.GetClauses())
cd.dialect = dialect
return cd
}
func (ud *UpdateDataset) Expression() exp.Expression {
return ud
}
// Clones the dataset
func (ud *UpdateDataset) Clone() exp.Expression {
return ud.copy(ud.clauses)
}
// Returns the current clauses on the dataset.
func (ud *UpdateDataset) GetClauses() exp.UpdateClauses {
return ud.clauses
}
// used internally to copy the dataset
func (ud *UpdateDataset) copy(clauses exp.UpdateClauses) *UpdateDataset {
return &UpdateDataset{
dialect: ud.dialect,
clauses: clauses,
isPrepared: ud.isPrepared,
queryFactory: ud.queryFactory,
err: ud.err,
}
}
// Creates a WITH clause for a common table expression (CTE).
//
// The name will be available to use in the UPDATE from in the associated query; and can optionally
// contain a list of column names "name(col1, col2, col3)".
//
// The name will refer to the results of the specified subquery.
func (ud *UpdateDataset) With(name string, subquery exp.Expression) *UpdateDataset {
return ud.copy(ud.clauses.CommonTablesAppend(exp.NewCommonTableExpression(false, name, subquery)))
}
// Creates a WITH RECURSIVE clause for a common table expression (CTE)
//
// The name will be available to use in the UPDATE from in the associated query; and must
// contain a list of column names "name(col1, col2, col3)" for a recursive clause.
//
// The name will refer to the results of the specified subquery. The subquery for
// a recursive query will always end with a UNION or UNION ALL with a clause that
// refers to the CTE by name.
func (ud *UpdateDataset) WithRecursive(name string, subquery exp.Expression) *UpdateDataset {
return ud.copy(ud.clauses.CommonTablesAppend(exp.NewCommonTableExpression(true, name, subquery)))
}
// Sets the table to update.
func (ud *UpdateDataset) Table(table interface{}) *UpdateDataset {
switch t := table.(type) {
case exp.Expression:
return ud.copy(ud.clauses.SetTable(t))
case string:
return ud.copy(ud.clauses.SetTable(exp.ParseIdentifier(t)))
default:
panic(ErrUnsupportedUpdateTableType)
}
}
// Sets the values to use in the SET clause. See examples.
func (ud *UpdateDataset) Set(values interface{}) *UpdateDataset {
return ud.copy(ud.clauses.SetSetValues(values))
}
// Allows specifying other tables to reference in your update (If your dialect supports it). See examples.
func (ud *UpdateDataset) From(tables ...interface{}) *UpdateDataset {
return ud.copy(ud.clauses.SetFrom(exp.NewColumnListExpression(tables...)))
}
// Adds a WHERE clause. See examples.
func (ud *UpdateDataset) Where(expressions ...exp.Expression) *UpdateDataset {
return ud.copy(ud.clauses.WhereAppend(expressions...))
}
// Removes the WHERE clause. See examples.
func (ud *UpdateDataset) ClearWhere() *UpdateDataset {
return ud.copy(ud.clauses.ClearWhere())
}
// Adds a ORDER clause. If the ORDER is currently set it replaces it. See examples.
func (ud *UpdateDataset) Order(order ...exp.OrderedExpression) *UpdateDataset {
return ud.copy(ud.clauses.SetOrder(order...))
}
// Adds a more columns to the current ORDER BY clause. If no order has be previously specified it is the same as
// calling Order. See examples.
func (ud *UpdateDataset) OrderAppend(order ...exp.OrderedExpression) *UpdateDataset {
return ud.copy(ud.clauses.OrderAppend(order...))
}
// Adds a more columns to the beginning of the current ORDER BY clause. If no order has be previously specified it is the same as
// calling Order. See examples.
func (ud *UpdateDataset) OrderPrepend(order ...exp.OrderedExpression) *UpdateDataset {
return ud.copy(ud.clauses.OrderPrepend(order...))
}
// Removes the ORDER BY clause. See examples.
func (ud *UpdateDataset) ClearOrder() *UpdateDataset {
return ud.copy(ud.clauses.ClearOrder())
}
// Adds a LIMIT clause. If the LIMIT is currently set it replaces it. See examples.
func (ud *UpdateDataset) Limit(limit uint) *UpdateDataset {
if limit > 0 {
return ud.copy(ud.clauses.SetLimit(limit))
}
return ud.copy(ud.clauses.ClearLimit())
}
// Adds a LIMIT ALL clause. If the LIMIT is currently set it replaces it. See examples.
func (ud *UpdateDataset) LimitAll() *UpdateDataset {
return ud.copy(ud.clauses.SetLimit(L("ALL")))
}
// Removes the LIMIT clause.
func (ud *UpdateDataset) ClearLimit() *UpdateDataset {
return ud.copy(ud.clauses.ClearLimit())
}
// Adds a RETURNING clause to the dataset if the adapter supports it. See examples.
func (ud *UpdateDataset) Returning(returning ...interface{}) *UpdateDataset {
return ud.copy(ud.clauses.SetReturning(exp.NewColumnListExpression(returning...)))
}
// Get any error that has been set or nil if no error has been set.
func (ud *UpdateDataset) Error() error {
return ud.err
}
// Set an error on the dataset if one has not already been set. This error will be returned by a future call to Error
// or as part of ToSQL. This can be used by end users to record errors while building up queries without having to
// track those separately.
func (ud *UpdateDataset) SetError(err error) *UpdateDataset {
if ud.err == nil {
ud.err = err
}
return ud
}
// Generates an UPDATE sql statement, if Prepared has been called with true then the parameters will not be interpolated.
// See examples.
//
// Errors:
// - There is an error generating the SQL
func (ud *UpdateDataset) ToSQL() (sql string, params []interface{}, err error) {
return ud.updateSQLBuilder().ToSQL()
}
// Appends this Dataset's UPDATE statement to the SQLBuilder
// This is used internally when using updates in CTEs
func (ud *UpdateDataset) AppendSQL(b sb.SQLBuilder) {
if ud.err != nil {
b.SetError(ud.err)
return
}
ud.dialect.ToUpdateSQL(b, ud.GetClauses())
}
func (ud *UpdateDataset) GetAs() exp.IdentifierExpression {
return nil
}
func (ud *UpdateDataset) ReturnsColumns() bool {
return ud.clauses.HasReturning()
}
// Generates the UPDATE sql, and returns an exec.QueryExecutor with the sql set to the UPDATE statement
//
// db.Update("test").Set(Record{"name":"Bob", update: time.Now()}).Executor()
func (ud *UpdateDataset) Executor() exec.QueryExecutor {
return ud.queryFactory.FromSQLBuilder(ud.updateSQLBuilder())
}
func (ud *UpdateDataset) updateSQLBuilder() sb.SQLBuilder {
buf := sb.NewSQLBuilder(ud.isPrepared.Bool())
if ud.err != nil {
return buf.SetError(ud.err)
}
ud.dialect.ToUpdateSQL(buf, ud.clauses)
return buf
}