-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
189 lines (183 loc) · 4.81 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
177
178
179
180
181
182
183
184
185
186
187
188
189
package sql
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
)
const (
DefaultPagingFormat = " limit %s offset %s "
OraclePagingFormat = " offset %s rows fetch next %s rows only "
desc = "desc"
asc = "asc"
)
func BuildFromQuery(ctx context.Context, db *sql.DB, fieldsIndex map[string]int, models interface{}, query string, params []interface{}, limit int64, offset int64, toArray func(interface{}) interface {
driver.Valuer
sql.Scanner
}, options ...func(context.Context, interface{}) (interface{}, error)) (int64, error) {
var mp func(context.Context, interface{}) (interface{}, error)
if len(options) > 0 && options[0] != nil {
mp = options[0]
}
var total int64
driver := GetDriver(db)
if limit <= 0 {
er1 := QueryWithArray(ctx, db, fieldsIndex, models, toArray, query, params...)
if er1 != nil {
return -1, er1
}
objectValues := reflect.Indirect(reflect.ValueOf(models))
if objectValues.Kind() == reflect.Slice {
i := objectValues.Len()
total = int64(i)
}
er2 := BuildSearchResult(ctx, models, mp)
return total, er2
} else {
if driver == DriverOracle {
queryPaging := BuildPagingQueryByDriver(query, limit, offset, driver)
er1 := QueryAndCount(ctx, db, fieldsIndex, models, toArray, &total, queryPaging, params...)
if er1 != nil {
return -1, er1
}
er2 := BuildSearchResult(ctx, models, mp)
return total, er2
} else {
queryPaging := BuildPagingQuery(query, limit, offset, driver)
queryCount := BuildCountQuery(query)
er1 := QueryWithArray(ctx, db, fieldsIndex, models, toArray, queryPaging, params...)
if er1 != nil {
return -1, er1
}
total, er2 := Count(ctx, db, queryCount, params...)
if er2 != nil {
total = 0
}
er3 := BuildSearchResult(ctx, models, mp)
return total, er3
}
}
}
func BuildPagingQueryByDriver(sql string, limit int64, offset int64, driver string) string {
s2 := BuildPagingQuery(sql, limit, offset, driver)
if driver != DriverOracle {
return s2
} else {
l := len(" distinct ")
i := strings.Index(sql, " distinct ")
if i < 0 {
i = strings.Index(sql, " DISTINCT ")
}
if i < 0 {
l = len("select") + 1
i = strings.Index(s2, "select")
}
if i < 0 {
i = strings.Index(s2, "SELECT")
}
if i >= 0 {
return s2[0:l] + " count(*) over() as total, " + s2[l:]
}
return s2
}
}
func BuildPagingQuery(sql string, limit int64, offset int64, opts ...string) string {
if offset < 0 {
offset = 0
}
if limit > 0 {
var pagingQuery string
if len(opts) > 0 && opts[0] == DriverOracle {
pagingQuery = fmt.Sprintf(OraclePagingFormat, strconv.FormatInt(offset, 10), strconv.FormatInt(limit, 10))
} else {
pagingQuery = fmt.Sprintf(DefaultPagingFormat, strconv.FormatInt(limit, 10), strconv.FormatInt(offset, 10))
}
sql += pagingQuery
}
return sql
}
func BuildCountQuery(sql string) string {
i := strings.Index(sql, "select ")
if i < 0 {
return sql
}
j := strings.Index(sql, " from ")
if j < 0 {
return sql
}
k := strings.Index(sql, " order by ")
h := strings.Index(sql, " distinct ")
if h > 0 {
sql3 := `select count(*) as total from (` + sql[i:] + `) as main`
return sql3
}
if k > 0 {
sql3 := `select count(*) as total ` + sql[j:k]
return sql3
} else {
sql3 := `select count(*) as total ` + sql[j:]
return sql3
}
}
func BuildSearchResult(ctx context.Context, models interface{}, mp func(context.Context, interface{}) (interface{}, error)) error {
if mp == nil {
return nil
}
_, err := MapModels(ctx, models, mp)
return err
}
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 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 BuildSort(sortString string, modelType reflect.Type) string {
var sort = GetSort(sortString, modelType)
if len(sort) > 0 {
return ` order by ` + sort
} else {
return ""
}
}
func ExtractArray(values []interface{}, field interface{}) []interface{} {
s := reflect.Indirect(reflect.ValueOf(field))
for i := 0; i < s.Len(); i++ {
values = append(values, s.Index(i).Interface())
}
return values
}