Skip to content

Commit c4701b7

Browse files
committed
push loader query sql_util files
1 parent 21f8839 commit c4701b7

File tree

3 files changed

+83
-53
lines changed

3 files changed

+83
-53
lines changed

loader.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ type Loader struct {
2020
mapJsonColumnKeys map[string]string
2121
fieldsIndex map[string]int
2222
table string
23-
toArray func(interface{}) interface {
23+
toArray func(interface{}) interface {
2424
driver.Valuer
2525
sql.Scanner
2626
}
2727
}
28-
func NewLoader(db *sql.DB, tableName string, modelType reflect.Type, toArray func(interface{}) interface {
28+
29+
func NewLoader(db *sql.DB, tableName string, modelType reflect.Type, options ...func(context.Context, interface{}) (interface{}, error)) (*Loader, error) {
30+
return NewLoaderWithArray(db, tableName, modelType, nil, options...)
31+
}
32+
func NewLoaderWithArray(db *sql.DB, tableName string, modelType reflect.Type, toArray func(interface{}) interface {
2933
driver.Valuer
3034
sql.Scanner
3135
}, options ...func(context.Context, interface{}) (interface{}, error)) (*Loader, error) {
@@ -38,7 +42,7 @@ func NewLoader(db *sql.DB, tableName string, modelType reflect.Type, toArray fun
3842
func NewSqlLoader(db *sql.DB, tableName string, modelType reflect.Type, mp func(context.Context, interface{}) (interface{}, error), toArray func(interface{}) interface {
3943
driver.Valuer
4044
sql.Scanner
41-
},options...func(i int) string) (*Loader, error) {
45+
}, options ...func(i int) string) (*Loader, error) {
4246
var buildParam func(i int) string
4347
if len(options) > 0 && options[0] != nil {
4448
buildParam = options[0]
@@ -63,7 +67,7 @@ func (s *Loader) Keys() []string {
6367
func (s *Loader) All(ctx context.Context) (interface{}, error) {
6468
query := BuildSelectAllQuery(s.table)
6569
result := reflect.New(s.modelsType).Interface()
66-
err := QueryWithMap(ctx, s.Database, s.fieldsIndex, result, query, s.toArray)
70+
err := QueryWithMapAndArray(ctx, s.Database, s.fieldsIndex, result, s.toArray, query)
6771
if err == nil {
6872
if s.Map != nil {
6973
return MapModels(ctx, result, s.Map)
@@ -75,7 +79,7 @@ func (s *Loader) All(ctx context.Context) (interface{}, error) {
7579

7680
func (s *Loader) Load(ctx context.Context, ids interface{}) (interface{}, error) {
7781
queryFindById, values := BuildFindById(s.Database, s.table, ids, s.mapJsonColumnKeys, s.keys, s.BuildParam)
78-
r, err := QueryRow(ctx, s.Database, s.modelType, s.fieldsIndex, queryFindById, s.toArray, values...)
82+
r, err := QueryRowWithArray(ctx, s.Database, s.modelType, s.fieldsIndex, s.toArray, queryFindById, values...)
7983
if s.Map != nil {
8084
_, er2 := s.Map(ctx, &r)
8185
if er2 != nil {
@@ -120,7 +124,7 @@ func (s *Loader) Exist(ctx context.Context, id interface{}) (bool, error) {
120124
func (s *Loader) LoadAndDecode(ctx context.Context, id interface{}, result interface{}) (bool, error) {
121125
var values []interface{}
122126
sql, values := BuildFindById(s.Database, s.table, id, s.mapJsonColumnKeys, s.keys, s.BuildParam)
123-
rowData, err1 := QueryRow(ctx, s.Database, s.modelType, s.fieldsIndex, sql, s.toArray, values...)
127+
rowData, err1 := QueryRowWithArray(ctx, s.Database, s.modelType, s.fieldsIndex, s.toArray, sql, values...)
124128
if err1 != nil || rowData == nil {
125129
return false, err1
126130
}

query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func BuildFromQuery(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
2828
var total int64
2929
driver := GetDriver(db)
3030
if limit <= 0 {
31-
er1 := Select(ctx, db, models, query, toArray, params...)
31+
er1 := QueryWithMapAndArray(ctx, db, fieldsIndex, models, toArray, query, params...)
3232
if er1 != nil {
3333
return -1, er1
3434
}
@@ -42,7 +42,7 @@ func BuildFromQuery(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
4242
} else {
4343
if driver == DriverOracle {
4444
queryPaging := BuildPagingQueryByDriver(query, limit, offset, driver)
45-
er1 := QueryAndCount(ctx, db, fieldsIndex, models, &total, queryPaging, toArray, params...)
45+
er1 := QueryAndCount(ctx, db, fieldsIndex, models, toArray, &total, queryPaging, params...)
4646
if er1 != nil {
4747
return -1, er1
4848
}
@@ -51,7 +51,7 @@ func BuildFromQuery(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
5151
} else {
5252
queryPaging := BuildPagingQuery(query, limit, offset, driver)
5353
queryCount, paramsCount := BuildCountQuery(query, params)
54-
er1 := Select(ctx, db, models, queryPaging, toArray, params...)
54+
er1 := QueryWithMapAndArray(ctx, db, fieldsIndex, models, toArray, queryPaging, params...)
5555
if er1 != nil {
5656
return -1, er1
5757
}

sql_util.go

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,13 @@ func QueryMap(ctx context.Context, db *sql.DB, transform func(s string) string,
124124
}
125125
return res, nil
126126
}
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 {
128131
driver.Valuer
129132
sql.Scanner
130-
},values ...interface{}) error {
133+
}, sql string, values ...interface{}) error {
131134
rows, er1 := db.QueryContext(ctx, sql, values...)
132135
if er1 != nil {
133136
return er1
@@ -140,7 +143,7 @@ func QueryWithMap(ctx context.Context, db *sql.DB, fieldsIndex map[string]int, r
140143
return er1
141144
}
142145
}
143-
tb, er3 := Scans(rows, modelType, fieldsIndex, toArray)
146+
tb, er3 := Scan(rows, modelType, fieldsIndex, toArray)
144147
if er3 != nil {
145148
return er3
146149
}
@@ -157,26 +160,38 @@ func QueryWithMap(ctx context.Context, db *sql.DB, fieldsIndex map[string]int, r
157160
}
158161
return nil
159162
}
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 {
161170
driver.Valuer
162171
sql.Scanner
163-
}, options...map[string]int) error {
172+
}, sql string, values []interface{}, options...map[string]int) error {
164173
var fieldsIndex map[string]int
165174
if len(options) > 0 && options[0] != nil {
166175
fieldsIndex = options[0]
167176
}
168-
return QueryWithMap(ctx, db, fieldsIndex, results, sql, toArray, values...)
177+
return QueryWithMapAndArray(ctx, db, fieldsIndex, results, toArray, sql, values...)
169178
}
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 {
171183
driver.Valuer
172184
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...)
175187
}
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 {
177192
driver.Valuer
178193
sql.Scanner
179-
}, values ...interface{}) error {
194+
}, sql string, values ...interface{}) error {
180195
rows, er1 := tx.QueryContext(ctx, sql, values...)
181196
if er1 != nil {
182197
return er1
@@ -191,7 +206,7 @@ func QueryTx(ctx context.Context, tx *sql.Tx, fieldsIndex map[string]int, result
191206
}
192207
}
193208

194-
tb, er3 := Scans(rows, modelType, fieldsIndex, toArray)
209+
tb, er3 := Scan(rows, modelType, fieldsIndex, toArray)
195210
if er3 != nil {
196211
return er3
197212
}
@@ -226,7 +241,7 @@ func QueryByStatement(ctx context.Context, stm *sql.Stmt, fieldsIndex map[string
226241
}
227242
}
228243

229-
tb, er3 := Scans(rows, modelType, fieldsIndex, toArray)
244+
tb, er3 := Scan(rows, modelType, fieldsIndex, toArray)
230245
if er3 != nil {
231246
return er3
232247
}
@@ -243,10 +258,10 @@ func QueryByStatement(ctx context.Context, stm *sql.Stmt, fieldsIndex map[string
243258
}
244259
return nil
245260
}
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 {
247262
driver.Valuer
248263
sql.Scanner
249-
}, values ...interface{}) error {
264+
}, count *int64, sql string, values ...interface{}) error {
250265
rows, er1 := db.QueryContext(ctx, sql, values...)
251266
if er1 != nil {
252267
return er1
@@ -261,7 +276,7 @@ func QueryAndCount(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
261276
}
262277
}
263278

264-
tb, c, er3 := ScansAndCount(rows, modelType, fieldsIndex, toArray)
279+
tb, c, er3 := ScanAndCount(rows, modelType, fieldsIndex, toArray)
265280
*count = c
266281
if er3 != nil {
267282
return er3
@@ -279,10 +294,13 @@ func QueryAndCount(ctx context.Context, db *sql.DB, fieldsIndex map[string]int,
279294
}
280295
return nil
281296
}
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 {
283301
driver.Valuer
284302
sql.Scanner
285-
},values ...interface{}) (interface{}, error) {
303+
}, sql string, values ...interface{}) (interface{}, error) {
286304
strSQL := "limit 1"
287305
driver := GetDriver(db)
288306
if driver == DriverOracle {
@@ -313,10 +331,13 @@ func QueryRow(ctx context.Context, db *sql.DB, modelType reflect.Type, fieldsInd
313331
}
314332
return tb, nil
315333
}
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 {
317338
driver.Valuer
318339
sql.Scanner
319-
}, values ...interface{}) (interface{}, error) {
340+
}, sql string, values ...interface{}) (interface{}, error) {
320341
rows, er1 := tx.QueryContext(ctx, sql, values...)
321342
if er1 != nil {
322343
return nil, er1
@@ -477,28 +498,48 @@ func GetColumns(cols []string, err error) ([]string, error) {
477498
}
478499
return c2, nil
479500
}
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 {
481502
driver.Valuer
482503
sql.Scanner
483504
}) (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+
}
484512
columns, er0 := GetColumns(rows.Columns())
485513
if er0 != nil {
486514
return nil, er0
487515
}
488516
for rows.Next() {
489517
initModel := reflect.New(modelType).Interface()
490-
r, swapValues := StructScan(initModel, columns, fieldsIndex, -1, toArray)
518+
r, swapValues := StructScan(initModel, columns, fieldsIndex, toArray)
491519
if err = rows.Scan(r...); err == nil {
492520
SwapValuesToBool(initModel, &swapValues)
493521
t = append(t, initModel)
494522
}
495523
}
496524
return
497525
}
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 {
499527
driver.Valuer
500528
sql.Scanner
501529
}) (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{}) {
502543
if s != nil {
503544
modelType := reflect.TypeOf(s).Elem()
504545
swapValues = make(map[int]interface{}, 0)
@@ -593,7 +634,7 @@ func SwapValuesToBool(s interface{}, swap *map[int]interface{}) {
593634
}
594635
}
595636
}
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 {
597638
driver.Valuer
598639
sql.Scanner
599640
}) ([]interface{}, int64, error) {
@@ -613,7 +654,7 @@ func ScansAndCount(rows *sql.Rows, modelType reflect.Type, fieldsIndex map[strin
613654
initModel := reflect.New(modelType).Interface()
614655
var c []interface{}
615656
c = append(c, &count)
616-
r, swapValues := StructScan(initModel, columns, fieldsIndex, 0,toArray)
657+
r, swapValues := StructScanAndIgnore(initModel, columns, fieldsIndex, toArray, 0)
617658
c = append(c, r...)
618659
if err := rows.Scan(c...); err == nil {
619660
SwapValuesToBool(initModel, &swapValues)
@@ -623,22 +664,7 @@ func ScansAndCount(rows *sql.Rows, modelType reflect.Type, fieldsIndex map[strin
623664
return t, count, nil
624665
}
625666

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 {
642668
driver.Valuer
643669
sql.Scanner
644670
}) (t interface{}, err error) {
@@ -656,7 +682,7 @@ func Scan(rows *sql.Rows, structType reflect.Type, fieldsIndex map[string]int, t
656682
}
657683
for rows.Next() {
658684
gTb := reflect.New(structType).Interface()
659-
r, swapValues := StructScan(gTb, columns, fieldsIndex, -1, toArray)
685+
r, swapValues := StructScanAndIgnore(gTb, columns, fieldsIndex, toArray, -1)
660686
if err = rows.Scan(r...); err == nil {
661687
SwapValuesToBool(gTb, &swapValues)
662688
t = gTb
@@ -667,12 +693,12 @@ func Scan(rows *sql.Rows, structType reflect.Type, fieldsIndex map[string]int, t
667693
}
668694

669695
//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 {
671697
driver.Valuer
672698
sql.Scanner
673699
}) (t interface{}, err error) {
674700
t = reflect.New(structType).Interface()
675-
r, swapValues := StructScan(t, nil, nil, -1, toArray)
701+
r, swapValues := StructScan(t, nil, nil, toArray)
676702
err = row.Scan(r...)
677703
SwapValuesToBool(t, &swapValues)
678704
return

0 commit comments

Comments
 (0)