-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_builder.go
44 lines (41 loc) · 1.6 KB
/
search_builder.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
package sql
import (
"context"
"database/sql"
"database/sql/driver"
"reflect"
)
type SearchBuilder struct {
Database *sql.DB
BuildQuery func(sm interface{}) (string, []interface{})
ModelType reflect.Type
Map func(ctx context.Context, model interface{}) (interface{}, error)
fieldsIndex map[string]int
ToArray func(interface{}) interface {
driver.Valuer
sql.Scanner
}
}
func NewSearchBuilder(db *sql.DB, modelType reflect.Type, buildQuery func(interface{}) (string, []interface{}), options ...func(context.Context, interface{}) (interface{}, error)) (*SearchBuilder, error) {
return NewSearchBuilderWithArray(db, modelType, buildQuery, nil, options...)
}
func NewSearchBuilderWithArray(db *sql.DB, modelType reflect.Type, buildQuery func(interface{}) (string, []interface{}), toArray func(interface{}) interface {
driver.Valuer
sql.Scanner
}, options ...func(context.Context, interface{}) (interface{}, error)) (*SearchBuilder, error) {
var mp func(context.Context, interface{}) (interface{}, error)
if len(options) >= 1 {
mp = options[0]
}
fieldsIndex, err := GetColumnIndexes(modelType)
if err != nil {
return nil, err
}
builder := &SearchBuilder{Database: db, fieldsIndex: fieldsIndex, BuildQuery: buildQuery, ModelType: modelType, Map: mp, ToArray: toArray}
return builder, nil
}
func (b *SearchBuilder) Search(ctx context.Context, m interface{}, results interface{}, limit int64, offset int64) (int64, error) {
sql, params := b.BuildQuery(m)
total, er2 := BuildFromQuery(ctx, b.Database, b.fieldsIndex, results, sql, params, limit, offset, b.ToArray, b.Map)
return total, er2
}