-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_builder.go
More file actions
145 lines (138 loc) · 4.2 KB
/
search_builder.go
File metadata and controls
145 lines (138 loc) · 4.2 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cassandra
import (
"context"
"encoding/hex"
"reflect"
"strings"
"github.com/apache/cassandra-gocql-driver"
)
const (
desc = "desc"
asc = "asc"
)
type SearchBuilder struct {
DB *gocql.ClusterConfig
BuildQuery func(sm interface{}) (string, []interface{})
ModelType reflect.Type
Map func(ctx context.Context, model interface{}) (interface{}, error)
fieldsIndex map[string]int
}
func NewSearchQuery(db *gocql.ClusterConfig, modelType reflect.Type, buildQuery func(interface{}) (string, []interface{}), options ...func(context.Context, interface{}) (interface{}, error)) (*SearchBuilder, error) {
return NewSearchBuilder(db, modelType, buildQuery, options...)
}
func NewSearchBuilder(db *gocql.ClusterConfig, modelType reflect.Type, buildQuery func(interface{}) (string, []interface{}), 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{DB: db, fieldsIndex: fieldsIndex, BuildQuery: buildQuery, ModelType: modelType, Map: mp}
return builder, nil
}
func (b *SearchBuilder) Search(ctx context.Context, m interface{}, results interface{}, limit int64, refId string) (string, error) {
sql, params := b.BuildQuery(m)
ses, err := b.DB.CreateSession()
defer ses.Close()
if err != nil {
return "", err
}
nextPageToken, er2 := QueryWithMap(ses, b.fieldsIndex, results, sql, params, limit, refId, b.Map)
return nextPageToken, er2
}
func QueryWithMap(ses *gocql.Session, fieldsIndex map[string]int, results interface{}, sql string, values []interface{}, max int64, refId string, options ...func(context.Context, interface{}) (interface{}, error)) (string, error) {
var mp func(context.Context, interface{}) (interface{}, error)
if len(options) > 0 && options[0] != nil {
mp = options[0]
}
next, er0 := hex.DecodeString(refId)
if er0 != nil {
return "", er0
}
query := ses.Query(sql, values...).PageState(next).PageSize(int(max))
if query.Exec() != nil {
return "", query.Exec()
}
err := ScanIter(query.Iter(), results, fieldsIndex)
if err != nil {
return "", err
}
nextPageToken := hex.EncodeToString(query.Iter().PageState())
if mp != nil {
_, err := MapModels(context.Background(), results, mp)
return nextPageToken, err
}
return nextPageToken, nil
}
func GetSort(sortString string, modelType reflect.Type) string {
var sort = make([]string, 0)
sorts := strings.Split(sortString, ",")
for i := 0; i < len(sorts); i++ {
sortField := strings.TrimSpace(sorts[i])
fieldName := sortField
c := sortField[0:1]
if c == "-" || c == "+" {
fieldName = sortField[1:]
}
columnName := GetColumnNameForSearch(modelType, fieldName)
if len(columnName) > 0 {
sortType := GetSortType(c)
sort = append(sort, columnName+" "+sortType)
}
}
if len(sort) > 0 {
return strings.Join(sort, ",")
} else {
return ""
}
}
func BuildSort(sortString string, modelType reflect.Type) string {
sort := GetSort(sortString, modelType)
if len(sort) > 0 {
return ` order by ` + sort
} else {
return ""
}
}
func GetColumnNameForSearch(modelType reflect.Type, sortField string) string {
sortField = strings.TrimSpace(sortField)
i, _, column := GetFieldByJson(modelType, sortField)
if i > -1 {
return column
}
return ""
}
func GetSortType(sortType string) string {
if sortType == "-" {
return desc
} else {
return asc
}
}
func GetFieldByJson(modelType reflect.Type, jsonName string) (int, string, string) {
numField := modelType.NumField()
for i := 0; i < numField; i++ {
field := modelType.Field(i)
tag1, ok1 := field.Tag.Lookup("json")
if ok1 && strings.Split(tag1, ",")[0] == jsonName {
if tag2, ok2 := field.Tag.Lookup("gorm"); ok2 {
if has := strings.Contains(tag2, "column"); has {
str1 := strings.Split(tag2, ";")
num := len(str1)
for k := 0; k < num; k++ {
str2 := strings.Split(str1[k], ":")
for j := 0; j < len(str2); j++ {
if str2[j] == "column" {
return i, field.Name, str2[j+1]
}
}
}
}
}
return i, field.Name, ""
}
}
return -1, jsonName, jsonName
}