@@ -63,10 +63,18 @@ func reportError(err error, code int, reqIdx int, noFail bool, results []respons
63
63
// Processes a query, and returns a suitable responseItem
64
64
//
65
65
// This method is needed to execute properly the defers.
66
- func processWithResultSet (tx * sql.Tx , query string , values map [ string ] interface {} ) (* responseItem , error ) {
66
+ func processWithResultSet (tx * sql.Tx , query string , params requestParams ) (* responseItem , error ) {
67
67
resultSet := make ([]orderedmap.OrderedMap , 0 )
68
68
69
- rows , err := tx .Query (query , vals2nameds (values )... )
69
+ rows := (* sql .Rows )(nil )
70
+ err := (error )(nil )
71
+ if params .UnmarshalledDict == nil && params .UnmarshalledArray == nil {
72
+ rows , err = nil , errors .New ("processWithResultSet unreachable code" )
73
+ } else if params .UnmarshalledDict != nil {
74
+ rows , err = tx .Query (query , vals2nameds (params .UnmarshalledDict )... )
75
+ } else {
76
+ rows , err = tx .Query (query , params .UnmarshalledArray ... )
77
+ }
70
78
if err != nil {
71
79
return nil , err
72
80
}
@@ -99,8 +107,16 @@ func processWithResultSet(tx *sql.Tx, query string, values map[string]interface{
99
107
}
100
108
101
109
// Process a single statement, and returns a suitable responseItem
102
- func processForExec (tx * sql.Tx , statement string , values map [string ]interface {}) (* responseItem , error ) {
103
- res , err := tx .Exec (statement , vals2nameds (values )... )
110
+ func processForExec (tx * sql.Tx , statement string , params requestParams ) (* responseItem , error ) {
111
+ res := (sql .Result )(nil )
112
+ err := (error )(nil )
113
+ if params .UnmarshalledDict == nil && params .UnmarshalledArray == nil {
114
+ res , err = nil , errors .New ("processWithResultSet unreachable code" )
115
+ } else if params .UnmarshalledDict != nil {
116
+ res , err = tx .Exec (statement , vals2nameds (params .UnmarshalledDict )... )
117
+ } else {
118
+ res , err = tx .Exec (statement , params .UnmarshalledArray ... )
119
+ }
104
120
if err != nil {
105
121
return nil , err
106
122
}
@@ -115,16 +131,24 @@ func processForExec(tx *sql.Tx, statement string, values map[string]interface{})
115
131
116
132
// Process a batch statement, and returns a suitable responseItem.
117
133
// It prepares the statement, then executes it for each of the values' sets.
118
- func processForExecBatch (tx * sql.Tx , q string , valuesBatch []map [ string ] interface {} ) (* responseItem , error ) {
134
+ func processForExecBatch (tx * sql.Tx , q string , paramsBatch []requestParams ) (* responseItem , error ) {
119
135
ps , err := tx .Prepare (q )
120
136
if err != nil {
121
137
return nil , err
122
138
}
123
139
defer ps .Close ()
124
140
125
141
var rowsUpdatedBatch []int64
126
- for i := range valuesBatch {
127
- res , err := ps .Exec (vals2nameds (valuesBatch [i ])... )
142
+ for _ , params := range paramsBatch {
143
+ res := (sql .Result )(nil )
144
+ err := (error )(nil )
145
+ if params .UnmarshalledDict == nil && params .UnmarshalledArray == nil {
146
+ res , err = nil , errors .New ("processWithResultSet unreachable code" )
147
+ } else if params .UnmarshalledDict != nil {
148
+ res , err = tx .Exec (q , vals2nameds (params .UnmarshalledDict )... )
149
+ } else {
150
+ res , err = tx .Exec (q , params .UnmarshalledArray ... )
151
+ }
128
152
if err != nil {
129
153
return nil , err
130
154
}
@@ -215,7 +239,7 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
215
239
216
240
hasResultSet := txItem .Query != ""
217
241
218
- if len (txItem .Values ) != 0 && len (txItem .ValuesBatch ) != 0 {
242
+ if ! isEmptyRaw (txItem .Values ) && len (txItem .ValuesBatch ) != 0 {
219
243
reportError (errors .New ("cannot specify both values and valuesBatch" ), fiber .StatusBadRequest , i , txItem .NoFail , ret .Results )
220
244
continue
221
245
}
@@ -256,18 +280,18 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
256
280
257
281
if len (txItem .ValuesBatch ) > 0 {
258
282
// Process a batch statement (multiple values)
259
- var valuesBatch []map [ string ] interface {}
283
+ var paramsBatch []requestParams
260
284
for i2 := range txItem .ValuesBatch {
261
- values , err := raw2vals (txItem .ValuesBatch [i2 ])
285
+ params , err := raw2params (txItem .ValuesBatch [i2 ])
262
286
if err != nil {
263
287
reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
264
288
continue
265
289
}
266
290
267
- valuesBatch = append (valuesBatch , values )
291
+ paramsBatch = append (paramsBatch , * params )
268
292
}
269
293
270
- retE , err := processForExecBatch (tx , sqll , valuesBatch )
294
+ retE , err := processForExecBatch (tx , sqll , paramsBatch )
271
295
if err != nil {
272
296
reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
273
297
continue
@@ -276,7 +300,7 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
276
300
ret .Results [i ] = * retE
277
301
} else {
278
302
// At most one values set (be it query or statement)
279
- values , err := raw2vals (txItem .Values )
303
+ params , err := raw2params (txItem .Values )
280
304
if err != nil {
281
305
reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
282
306
continue
@@ -285,7 +309,7 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
285
309
if hasResultSet {
286
310
// Query
287
311
// Externalized in a func so that defer rows.Close() actually runs
288
- retWR , err := processWithResultSet (tx , sqll , values )
312
+ retWR , err := processWithResultSet (tx , sqll , * params )
289
313
if err != nil {
290
314
reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
291
315
continue
@@ -294,7 +318,7 @@ func handler(databaseId string) func(c *fiber.Ctx) error {
294
318
ret .Results [i ] = * retWR
295
319
} else {
296
320
// Statement
297
- retE , err := processForExec (tx , sqll , values )
321
+ retE , err := processForExec (tx , sqll , * params )
298
322
if err != nil {
299
323
reportError (err , fiber .StatusInternalServerError , i , txItem .NoFail , ret .Results )
300
324
continue
0 commit comments