@@ -124,10 +124,13 @@ func QueryMap(ctx context.Context, db *sql.DB, transform func(s string) string,
124
124
}
125
125
return res , nil
126
126
}
127
- func QueryWithMap (ctx context.Context , db * sql.DB , fieldsIndex map [string ]int , results interface {}, sql string , toArray func (interface {}) interface {
127
+ func QueryWithMap (ctx context.Context , db * sql.DB , fieldsIndex map [string ]int , results interface {}, sql string , values ... interface {}) error {
128
+ return QueryWithMapAndArray (ctx , db , fieldsIndex , results , nil , sql , values ... )
129
+ }
130
+ func QueryWithMapAndArray (ctx context.Context , db * sql.DB , fieldsIndex map [string ]int , results interface {}, toArray func (interface {}) interface {
128
131
driver.Valuer
129
132
sql.Scanner
130
- },values ... interface {}) error {
133
+ }, sql string , values ... interface {}) error {
131
134
rows , er1 := db .QueryContext (ctx , sql , values ... )
132
135
if er1 != nil {
133
136
return er1
@@ -140,7 +143,7 @@ func QueryWithMap(ctx context.Context, db *sql.DB, fieldsIndex map[string]int, r
140
143
return er1
141
144
}
142
145
}
143
- tb , er3 := Scans (rows , modelType , fieldsIndex , toArray )
146
+ tb , er3 := Scan (rows , modelType , fieldsIndex , toArray )
144
147
if er3 != nil {
145
148
return er3
146
149
}
@@ -157,26 +160,38 @@ func QueryWithMap(ctx context.Context, db *sql.DB, fieldsIndex map[string]int, r
157
160
}
158
161
return nil
159
162
}
160
- func Query (ctx context.Context , db * sql.DB , results interface {}, sql string , values []interface {}, toArray func (interface {}) interface {
163
+ func Query (ctx context.Context , db * sql.DB , results interface {}, toArray func (interface {}) interface {
164
+ driver.Valuer
165
+ sql.Scanner
166
+ }, sql string , values []interface {}, options ... map [string ]int ) error {
167
+ return Query (ctx , db , results , nil , sql , values , options ... )
168
+ }
169
+ func QueryWithArray (ctx context.Context , db * sql.DB , results interface {}, toArray func (interface {}) interface {
161
170
driver.Valuer
162
171
sql.Scanner
163
- }, options ... map [string ]int ) error {
172
+ }, sql string , values [] interface {}, options ... map [string ]int ) error {
164
173
var fieldsIndex map [string ]int
165
174
if len (options ) > 0 && options [0 ] != nil {
166
175
fieldsIndex = options [0 ]
167
176
}
168
- return QueryWithMap (ctx , db , fieldsIndex , results , sql , toArray , values ... )
177
+ return QueryWithMapAndArray (ctx , db , fieldsIndex , results , toArray , sql , values ... )
169
178
}
170
- func Select (ctx context.Context , db * sql.DB , results interface {}, sql string , toArray func (interface {}) interface {
179
+ func Select (ctx context.Context , db * sql.DB , results interface {}, sql string , values ... interface {}) error {
180
+ return SelectWithArray (ctx , db , results , nil , sql , values ... )
181
+ }
182
+ func SelectWithArray (ctx context.Context , db * sql.DB , results interface {}, toArray func (interface {}) interface {
171
183
driver.Valuer
172
184
sql.Scanner
173
- }, values ... interface {}) error {
174
- return QueryWithMap (ctx , db , nil , results , sql , toArray , values ... )
185
+ }, sql string , values ... interface {}) error {
186
+ return QueryWithMapAndArray (ctx , db , nil , results , toArray , sql , values ... )
175
187
}
176
- func QueryTx (ctx context.Context , tx * sql.Tx , fieldsIndex map [string ]int , results interface {}, sql string , toArray func (interface {}) interface {
188
+ func QueryTx (ctx context.Context , tx * sql.Tx , fieldsIndex map [string ]int , results interface {}, sql string , values ... interface {}) error {
189
+ return QueryTxWithArray (ctx , tx , fieldsIndex , results , nil , sql , values ... )
190
+ }
191
+ func QueryTxWithArray (ctx context.Context , tx * sql.Tx , fieldsIndex map [string ]int , results interface {}, toArray func (interface {}) interface {
177
192
driver.Valuer
178
193
sql.Scanner
179
- }, values ... interface {}) error {
194
+ }, sql string , values ... interface {}) error {
180
195
rows , er1 := tx .QueryContext (ctx , sql , values ... )
181
196
if er1 != nil {
182
197
return er1
@@ -191,7 +206,7 @@ func QueryTx(ctx context.Context, tx *sql.Tx, fieldsIndex map[string]int, result
191
206
}
192
207
}
193
208
194
- tb , er3 := Scans (rows , modelType , fieldsIndex , toArray )
209
+ tb , er3 := Scan (rows , modelType , fieldsIndex , toArray )
195
210
if er3 != nil {
196
211
return er3
197
212
}
@@ -226,7 +241,7 @@ func QueryByStatement(ctx context.Context, stm *sql.Stmt, fieldsIndex map[string
226
241
}
227
242
}
228
243
229
- tb , er3 := Scans (rows , modelType , fieldsIndex , toArray )
244
+ tb , er3 := Scan (rows , modelType , fieldsIndex , toArray )
230
245
if er3 != nil {
231
246
return er3
232
247
}
@@ -243,10 +258,10 @@ func QueryByStatement(ctx context.Context, stm *sql.Stmt, fieldsIndex map[string
243
258
}
244
259
return nil
245
260
}
246
- func QueryAndCount (ctx context.Context , db * sql.DB , fieldsIndex map [string ]int , results interface {}, count * int64 , sql string , toArray func (interface {}) interface {
261
+ func QueryAndCount (ctx context.Context , db * sql.DB , fieldsIndex map [string ]int , results interface {}, toArray func (interface {}) interface {
247
262
driver.Valuer
248
263
sql.Scanner
249
- }, values ... interface {}) error {
264
+ }, count * int64 , sql string , values ... interface {}) error {
250
265
rows , er1 := db .QueryContext (ctx , sql , values ... )
251
266
if er1 != nil {
252
267
return er1
@@ -261,7 +276,7 @@ func QueryAndCount(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
261
276
}
262
277
}
263
278
264
- tb , c , er3 := ScansAndCount (rows , modelType , fieldsIndex , toArray )
279
+ tb , c , er3 := ScanAndCount (rows , modelType , fieldsIndex , toArray )
265
280
* count = c
266
281
if er3 != nil {
267
282
return er3
@@ -279,10 +294,13 @@ func QueryAndCount(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
279
294
}
280
295
return nil
281
296
}
282
- func QueryRow (ctx context.Context , db * sql.DB , modelType reflect.Type , fieldsIndex map [string ]int , sql string , toArray func (interface {}) interface {
297
+ func QueryRow (ctx context.Context , db * sql.DB , modelType reflect.Type , fieldsIndex map [string ]int , sql string , values ... interface {}) (interface {}, error ) {
298
+ return QueryRowWithArray (ctx , db , modelType , fieldsIndex , nil , sql , values ... )
299
+ }
300
+ func QueryRowWithArray (ctx context.Context , db * sql.DB , modelType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
283
301
driver.Valuer
284
302
sql.Scanner
285
- },values ... interface {}) (interface {}, error ) {
303
+ }, sql string , values ... interface {}) (interface {}, error ) {
286
304
strSQL := "limit 1"
287
305
driver := GetDriver (db )
288
306
if driver == DriverOracle {
@@ -313,10 +331,13 @@ func QueryRow(ctx context.Context, db *sql.DB, modelType reflect.Type, fieldsInd
313
331
}
314
332
return tb , nil
315
333
}
316
- func QueryRowTx (ctx context.Context , tx * sql.Tx , modelType reflect.Type , fieldsIndex map [string ]int , sql string , toArray func (interface {}) interface {
334
+ func QueryRowTx (ctx context.Context , tx * sql.Tx , modelType reflect.Type , fieldsIndex map [string ]int , sql string , values ... interface {}) (interface {}, error ) {
335
+ return QueryRowTxWithArray (ctx , tx , modelType , fieldsIndex , nil , sql , values ... )
336
+ }
337
+ func QueryRowTxWithArray (ctx context.Context , tx * sql.Tx , modelType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
317
338
driver.Valuer
318
339
sql.Scanner
319
- }, values ... interface {}) (interface {}, error ) {
340
+ }, sql string , values ... interface {}) (interface {}, error ) {
320
341
rows , er1 := tx .QueryContext (ctx , sql , values ... )
321
342
if er1 != nil {
322
343
return nil , er1
@@ -477,28 +498,48 @@ func GetColumns(cols []string, err error) ([]string, error) {
477
498
}
478
499
return c2 , nil
479
500
}
480
- func Scans (rows * sql.Rows , modelType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
501
+ func Scan (rows * sql.Rows , modelType reflect.Type , fieldsIndex map [string ]int , options ... func (interface {}) interface {
481
502
driver.Valuer
482
503
sql.Scanner
483
504
}) (t []interface {}, err error ) {
505
+ var toArray func (interface {}) interface {
506
+ driver.Valuer
507
+ sql.Scanner
508
+ }
509
+ if len (options ) > 0 {
510
+ toArray = options [0 ]
511
+ }
484
512
columns , er0 := GetColumns (rows .Columns ())
485
513
if er0 != nil {
486
514
return nil , er0
487
515
}
488
516
for rows .Next () {
489
517
initModel := reflect .New (modelType ).Interface ()
490
- r , swapValues := StructScan (initModel , columns , fieldsIndex , - 1 , toArray )
518
+ r , swapValues := StructScan (initModel , columns , fieldsIndex , toArray )
491
519
if err = rows .Scan (r ... ); err == nil {
492
520
SwapValuesToBool (initModel , & swapValues )
493
521
t = append (t , initModel )
494
522
}
495
523
}
496
524
return
497
525
}
498
- func StructScan (s interface {}, columns []string , fieldsIndex map [string ]int , indexIgnore int , toArray func (interface {}) interface {
526
+ func StructScan (s interface {}, columns []string , fieldsIndex map [string ]int , options ... func (interface {}) interface {
499
527
driver.Valuer
500
528
sql.Scanner
501
529
}) (r []interface {}, swapValues map [int ]interface {}) {
530
+ var toArray func (interface {}) interface {
531
+ driver.Valuer
532
+ sql.Scanner
533
+ }
534
+ if len (options ) > 0 {
535
+ toArray = options [0 ]
536
+ }
537
+ return StructScanAndIgnore (s , columns , fieldsIndex , toArray , - 1 )
538
+ }
539
+ func StructScanAndIgnore (s interface {}, columns []string , fieldsIndex map [string ]int , toArray func (interface {}) interface {
540
+ driver.Valuer
541
+ sql.Scanner
542
+ }, indexIgnore int ) (r []interface {}, swapValues map [int ]interface {}) {
502
543
if s != nil {
503
544
modelType := reflect .TypeOf (s ).Elem ()
504
545
swapValues = make (map [int ]interface {}, 0 )
@@ -593,7 +634,7 @@ func SwapValuesToBool(s interface{}, swap *map[int]interface{}) {
593
634
}
594
635
}
595
636
}
596
- func ScansAndCount (rows * sql.Rows , modelType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
637
+ func ScanAndCount (rows * sql.Rows , modelType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
597
638
driver.Valuer
598
639
sql.Scanner
599
640
}) ([]interface {}, int64 , error ) {
@@ -613,7 +654,7 @@ func ScansAndCount(rows *sql.Rows, modelType reflect.Type, fieldsIndex map[strin
613
654
initModel := reflect .New (modelType ).Interface ()
614
655
var c []interface {}
615
656
c = append (c , & count )
616
- r , swapValues := StructScan (initModel , columns , fieldsIndex , 0 , toArray )
657
+ r , swapValues := StructScanAndIgnore (initModel , columns , fieldsIndex , toArray , 0 )
617
658
c = append (c , r ... )
618
659
if err := rows .Scan (c ... ); err == nil {
619
660
SwapValuesToBool (initModel , & swapValues )
@@ -623,22 +664,7 @@ func ScansAndCount(rows *sql.Rows, modelType reflect.Type, fieldsIndex map[strin
623
664
return t , count , nil
624
665
}
625
666
626
- func ScanByModelType (rows * sql.Rows , modelType reflect.Type , toArray func (interface {}) interface {
627
- driver.Valuer
628
- sql.Scanner
629
- }) (t []interface {}, err error ) {
630
- for rows .Next () {
631
- gTb := reflect .New (modelType ).Interface ()
632
- r , swapValues := StructScan (gTb , nil , nil , - 1 , toArray )
633
- if err = rows .Scan (r ... ); err == nil {
634
- SwapValuesToBool (gTb , & swapValues )
635
- t = append (t , gTb )
636
- }
637
- }
638
- return
639
- }
640
-
641
- func Scan (rows * sql.Rows , structType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
667
+ func ScanRowsWithArray (rows * sql.Rows , structType reflect.Type , fieldsIndex map [string ]int , toArray func (interface {}) interface {
642
668
driver.Valuer
643
669
sql.Scanner
644
670
}) (t interface {}, err error ) {
@@ -656,7 +682,7 @@ func Scan(rows *sql.Rows, structType reflect.Type, fieldsIndex map[string]int, t
656
682
}
657
683
for rows .Next () {
658
684
gTb := reflect .New (structType ).Interface ()
659
- r , swapValues := StructScan (gTb , columns , fieldsIndex , - 1 , toArray )
685
+ r , swapValues := StructScanAndIgnore (gTb , columns , fieldsIndex , toArray , - 1 )
660
686
if err = rows .Scan (r ... ); err == nil {
661
687
SwapValuesToBool (gTb , & swapValues )
662
688
t = gTb
@@ -667,12 +693,12 @@ func Scan(rows *sql.Rows, structType reflect.Type, fieldsIndex map[string]int, t
667
693
}
668
694
669
695
//Row
670
- func ScanRow (row * sql.Row , structType reflect.Type , toArray func (interface {}) interface {
696
+ func ScanRowWithArray (row * sql.Row , structType reflect.Type , toArray func (interface {}) interface {
671
697
driver.Valuer
672
698
sql.Scanner
673
699
}) (t interface {}, err error ) {
674
700
t = reflect .New (structType ).Interface ()
675
- r , swapValues := StructScan (t , nil , nil , - 1 , toArray )
701
+ r , swapValues := StructScan (t , nil , nil , toArray )
676
702
err = row .Scan (r ... )
677
703
SwapValuesToBool (t , & swapValues )
678
704
return
0 commit comments