@@ -5,59 +5,66 @@ import (
5
5
"database/sql"
6
6
"fmt"
7
7
"sort"
8
+ "strconv"
8
9
"strings"
9
-
10
- "github.com/lann/builder"
11
10
)
12
11
13
- type updateData struct {
14
- PlaceholderFormat PlaceholderFormat
15
- RunWith BaseRunner
16
- Prefixes exprs
17
- Table string
18
- SetClauses []setClause
19
- WhereParts []Sqlizer
20
- OrderBys []string
21
- Limit string
22
- Offset string
23
- Suffixes exprs
24
- }
25
-
26
12
type setClause struct {
27
13
column string
28
14
value interface {}
29
15
}
30
16
31
- func (d * updateData ) Exec () (sql.Result , error ) {
32
- if d .RunWith == nil {
17
+ // Builder
18
+
19
+ // UpdateBuilder builds SQL UPDATE statements.
20
+ type UpdateBuilder struct {
21
+ StatementBuilderType
22
+
23
+ prefixes exprs
24
+ table string
25
+ setClauses []setClause
26
+ whereParts []Sqlizer
27
+ orderBys []string
28
+ limit uint64
29
+ offset uint64
30
+ suffixes exprs
31
+ }
32
+
33
+ func NewUpdateBuilder (b StatementBuilderType ) * UpdateBuilder {
34
+ return & UpdateBuilder {StatementBuilderType : b }
35
+ }
36
+
37
+ func (b * UpdateBuilder ) Exec () (sql.Result , error ) {
38
+ if b .runWith == nil {
33
39
return nil , RunnerNotSet
34
40
}
35
- return ExecWith (d .RunWith , d )
41
+
42
+ return ExecWith (b .runWith , b )
36
43
}
37
44
38
- func (d * updateData ) ToSql () (sqlStr string , args []interface {}, err error ) {
39
- if len (d . Table ) == 0 {
45
+ func (b * UpdateBuilder ) ToSql () (sqlStr string , args []interface {}, err error ) {
46
+ if len (b . table ) == 0 {
40
47
err = fmt .Errorf ("update statements must specify a table" )
41
48
return
42
49
}
43
- if len (d . SetClauses ) == 0 {
50
+ if len (b . setClauses ) == 0 {
44
51
err = fmt .Errorf ("update statements must have at least one Set clause" )
45
52
return
46
53
}
47
54
48
55
sql := & bytes.Buffer {}
49
56
50
- if len (d . Prefixes ) > 0 {
51
- args , _ = d . Prefixes .AppendToSql (sql , " " , args )
57
+ if len (b . prefixes ) > 0 {
58
+ args , _ = b . prefixes .AppendToSql (sql , " " , args )
52
59
sql .WriteString (" " )
53
60
}
54
61
55
62
sql .WriteString ("UPDATE " )
56
- sql .WriteString (d . Table )
63
+ sql .WriteString (b . table )
57
64
58
65
sql .WriteString (" SET " )
59
- setSqls := make ([]string , len (d . SetClauses ))
60
- for i , setClause := range d . SetClauses {
66
+ setSqls := make ([]string , len (b . setClauses ))
67
+ for i , setClause := range b . setClauses {
61
68
var valSql string
62
69
e , isExpr := setClause .value .(expr )
63
70
if isExpr {
@@ -71,94 +78,78 @@ func (d *updateData) ToSql() (sqlStr string, args []interface{}, err error) {
71
78
}
72
79
sql .WriteString (strings .Join (setSqls , ", " ))
73
80
74
- if len (d . WhereParts ) > 0 {
81
+ if len (b . whereParts ) > 0 {
75
82
sql .WriteString (" WHERE " )
76
- args , err = appendToSql (d . WhereParts , sql , " AND " , args )
83
+ args , err = appendToSql (b . whereParts , sql , " AND " , args )
77
84
if err != nil {
78
85
return
79
86
}
80
87
}
81
88
82
- if len (d . OrderBys ) > 0 {
89
+ if len (b . orderBys ) > 0 {
83
90
sql .WriteString (" ORDER BY " )
84
- sql .WriteString (strings .Join (d . OrderBys , ", " ))
91
+ sql .WriteString (strings .Join (b . orderBys , ", " ))
85
92
}
86
93
87
- if len (d .Limit ) > 0 {
94
+ // TODO: limit == 0 and offswt == 0 are valid. Need to go dbr way and implement offsetValid and limitValid
95
+ if b .limit > 0 {
88
96
sql .WriteString (" LIMIT " )
89
- sql .WriteString (d . Limit )
97
+ sql .WriteString (strconv . FormatUint ( b . limit , 10 ) )
90
98
}
91
99
92
- if len ( d . Offset ) > 0 {
100
+ if b . offset > 0 {
93
101
sql .WriteString (" OFFSET " )
94
- sql .WriteString (d . Offset )
102
+ sql .WriteString (strconv . FormatUint ( b . offset , 10 ) )
95
103
}
96
104
97
- if len (d . Suffixes ) > 0 {
105
+ if len (b . suffixes ) > 0 {
98
106
sql .WriteString (" " )
99
- args , _ = d . Suffixes .AppendToSql (sql , " " , args )
107
+ args , _ = b . suffixes .AppendToSql (sql , " " , args )
100
108
}
101
109
102
- sqlStr , err = d . PlaceholderFormat .ReplacePlaceholders (sql .String ())
110
+ sqlStr , err = b . placeholderFormat .ReplacePlaceholders (sql .String ())
103
111
return
104
112
}
105
113
106
-
107
- // Builder
108
-
109
- // UpdateBuilder builds SQL UPDATE statements.
110
- type UpdateBuilder builder.Builder
111
-
112
- func init () {
113
- builder .Register (UpdateBuilder {}, updateData {})
114
- }
115
-
116
114
// Format methods
117
115
118
116
// PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the
119
117
// query.
120
- func (b UpdateBuilder ) PlaceholderFormat (f PlaceholderFormat ) UpdateBuilder {
121
- return builder .Set (b , "PlaceholderFormat" , f ).(UpdateBuilder )
118
+ func (b * UpdateBuilder ) PlaceholderFormat (f PlaceholderFormat ) * UpdateBuilder {
119
+ b .placeholderFormat = f
120
+ return b
122
121
}
123
122
124
123
// Runner methods
125
124
126
125
// RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
127
- func (b UpdateBuilder ) RunWith (runner BaseRunner ) UpdateBuilder {
128
- return setRunWith (b , runner ).(UpdateBuilder )
129
- }
130
-
131
- // Exec builds and Execs the query with the Runner set by RunWith.
132
- func (b UpdateBuilder ) Exec () (sql.Result , error ) {
133
- data := builder .GetStruct (b ).(updateData )
134
- return data .Exec ()
126
+ func (b * UpdateBuilder ) RunWith (runner BaseRunner ) * UpdateBuilder {
127
+ b .runWith = runner
128
+ return b
135
129
}
136
130
137
131
// SQL methods
138
132
139
- // ToSql builds the query into a SQL string and bound args.
140
- func (b UpdateBuilder ) ToSql () (string , []interface {}, error ) {
141
- data := builder .GetStruct (b ).(updateData )
142
- return data .ToSql ()
143
- }
144
-
145
133
// Prefix adds an expression to the beginning of the query
146
- func (b UpdateBuilder ) Prefix (sql string , args ... interface {}) UpdateBuilder {
147
- return builder .Append (b , "Prefixes" , Expr (sql , args ... )).(UpdateBuilder )
134
+ func (b * UpdateBuilder ) Prefix (sql string , args ... interface {}) * UpdateBuilder {
135
+ b .prefixes = append (b .prefixes , Expr (sql , args ... ))
136
+ return b
148
137
}
149
138
150
- // Table sets the table to be updated.
151
- func (b UpdateBuilder ) Table (table string ) UpdateBuilder {
152
- return builder .Set (b , "Table" , table ).(UpdateBuilder )
139
+ // Table sets the table to be updateb.
140
+ func (b * UpdateBuilder ) Table (table string ) * UpdateBuilder {
141
+ b .table = table
142
+ return b
153
143
}
154
144
155
145
// Set adds SET clauses to the query.
156
- func (b UpdateBuilder ) Set (column string , value interface {}) UpdateBuilder {
157
- return builder .Append (b , "SetClauses" , setClause {column : column , value : value }).(UpdateBuilder )
146
+ func (b * UpdateBuilder ) Set (column string , value interface {}) * UpdateBuilder {
147
+ b .setClauses = append (b .setClauses , setClause {column : column , value : value })
148
+ return b
158
149
}
159
150
160
151
// SetMap is a convenience method which calls .Set for each key/value pair in clauses.
161
- func (b UpdateBuilder ) SetMap (clauses map [string ]interface {}) UpdateBuilder {
152
+ func (b * UpdateBuilder ) SetMap (clauses map [string ]interface {}) * UpdateBuilder {
162
153
keys := make ([]string , len (clauses ))
163
154
i := 0
164
155
for key := range clauses {
@@ -176,26 +167,32 @@ func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder {
176
167
// Where adds WHERE expressions to the query.
177
168
//
178
169
// See SelectBuilder.Where for more information.
179
- func (b UpdateBuilder ) Where (pred interface {}, args ... interface {}) UpdateBuilder {
180
- return builder .Append (b , "WhereParts" , newWherePart (pred , args ... )).(UpdateBuilder )
170
+ func (b * UpdateBuilder ) Where (pred interface {}, args ... interface {}) * UpdateBuilder {
171
+ b .whereParts = append (b .whereParts , newWherePart (pred , args ... ))
172
+ return b
181
173
}
182
174
183
175
// OrderBy adds ORDER BY expressions to the query.
184
- func (b UpdateBuilder ) OrderBy (orderBys ... string ) UpdateBuilder {
185
- return builder .Extend (b , "OrderBys" , orderBys ).(UpdateBuilder )
176
+ func (b * UpdateBuilder ) OrderBy (orderBys ... string ) * UpdateBuilder {
177
+ b .orderBys = append (b .orderBys , orderBys ... )
178
+ return b
186
179
}
187
180
188
181
// Limit sets a LIMIT clause on the query.
189
- func (b UpdateBuilder ) Limit (limit uint64 ) UpdateBuilder {
190
- return builder .Set (b , "Limit" , fmt .Sprintf ("%d" , limit )).(UpdateBuilder )
182
+ func (b * UpdateBuilder ) Limit (limit uint64 ) * UpdateBuilder {
183
+ b .limit = limit
184
+ return b
191
185
}
192
186
193
187
// Offset sets a OFFSET clause on the query.
194
- func (b UpdateBuilder ) Offset (offset uint64 ) UpdateBuilder {
195
- return builder .Set (b , "Offset" , fmt .Sprintf ("%d" , offset )).(UpdateBuilder )
188
+ func (b * UpdateBuilder ) Offset (offset uint64 ) * UpdateBuilder {
189
+ b .offset = offset
190
+ return b
196
191
}
197
192
198
193
// Suffix adds an expression to the end of the query
199
- func (b UpdateBuilder ) Suffix (sql string , args ... interface {}) UpdateBuilder {
200
- return builder .Append (b , "Suffixes" , Expr (sql , args ... )).(UpdateBuilder )
194
+ func (b * UpdateBuilder ) Suffix (sql string , args ... interface {}) * UpdateBuilder {
195
+ b .suffixes = append (b .suffixes , Expr (sql , args ... ))
196
+
197
+ return b
201
198
}
0 commit comments