forked from fuxiaohei/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
176 lines (158 loc) · 3.5 KB
/
query.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gojx
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// Query provides query engine for selection by condition.
type Query struct {
s *Storage
eq map[string]interface{}
order string
limit [2]int
}
// Eq add eq condition in index field.
func (q *Query) Eq(k string, v interface{}) *Query {
q.eq[k] = v
return q
}
// Limit set returning slice size.
func (q *Query) Limit(i int) *Query {
q.limit = [2]int{0, i}
return q
}
// Page set returning slice size as pagination.
func (q *Query) Pager(page, size int) *Query {
if page < 1 {
page = 1
}
if size < 1 {
size = 1
}
q.limit = [2]int{(page - 1) * size, page * size}
return q
}
// Order set result slice order by pk int.
func (q *Query) Order(order string) *Query {
q.order = strings.ToUpper(order)
return q
}
// ToSlice query storage by conditions and assign results to slice.
// Slice need a *[]*Struct ( a reference of slice of struct pointer.
func (q *Query) ToSlice(v interface{}) (e error) {
sliceRt, sliceRv := reflect.TypeOf(v), reflect.ValueOf(v)
// check type, need *[]*Value
if sliceRt.Kind() != reflect.Ptr || sliceRt.Elem().Kind() != reflect.Slice || sliceRt.Elem().Elem().Kind() != reflect.Ptr || sliceRt.Elem().Elem().Elem().Kind() != reflect.Struct {
e = ErrorToSliceNeedSlice
return
}
// get table
sliceElemRt := sliceRt.Elem().Elem().Elem()
table := q.s.table[getReflectTypeName(sliceElemRt)]
// parse eq condition
sliceResult := q.parseEq(table)
// parse order
sliceResult = q.parseOrder(sliceResult)
// parse limit condition
sliceResult = q.parseLimit(sliceResult)
if len(sliceResult) < 1 {
return
}
// build slice
q.buildSlice(sliceResult, table, sliceElemRt, sliceRv)
return
}
// assign result data to slice.
func (q *Query) buildSlice(sliceResult []int, t *Table, rt reflect.Type, sliceRv reflect.Value) (e error) {
if len(sliceResult) < 1 {
return
}
for _, pk := range sliceResult {
rv := reflect.New(rt)
if pk < 1 {
continue
}
_, iftValue, e := t.Get(pk)
if e != nil {
return e
}
if iftValue == nil {
continue
}
e = q.s.saver.ToStruct(iftValue, rv.Interface())
if e != nil {
return e
}
sliceRv.Elem().Set(reflect.Append(sliceRv.Elem(), rv))
}
return
}
// parse eq condition.
// return pk slice []int.
func (q *Query) parseEq(t *Table) []int {
res := []interface{}{}
for k, v := range q.eq {
idx := t.valueIndex[k]
if idx == nil {
continue
}
tmp := idx.Get(fmt.Sprintf("%v", v))
if len(tmp) > 0 {
if len(res) < 1 {
res = tmp
continue
}
tmp2 := []interface{}{}
for _, j := range tmp {
if _, b := isInInterfaceSlice(res, j); b {
tmp2 = append(tmp2, j)
}
}
if len(tmp2) < 1 {
return []int{}
}
res = tmp2
} else {
return []int{}
}
}
result := make([]int, len(res))
for i, v := range res {
pk, _ := strconv.Atoi(fmt.Sprint(v))
result[i] = pk
}
return result
}
// parse limit condition and pagination.
func (q *Query) parseLimit(result []int) []int {
if q.limit[1] > 0 {
length := len(result)
if length <= q.limit[0] {
return nil
}
if length <= q.limit[1] {
q.limit[1] = length
}
return result[q.limit[0]:q.limit[1]]
}
return result
}
func (q *Query) parseOrder(result []int) []int {
if q.order == "ASC" {
sortIntSliceASC(result)
}
if q.order == "DESC" {
sortIntSliceDESC(result)
}
return result
}
// create new Query with *Storage.
func NewQuery(s *Storage) *Query {
q := new(Query)
q.s = s
q.order = "ASC"
q.eq = make(map[string]interface{})
q.limit = [2]int{-1, -1}
return q
}