-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
152 lines (133 loc) · 3.03 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
package torm
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
)
type KV map[string]interface{}
type selectBuilder struct {
h handler
fields []string
}
func newSelect(h handler, f ...string) *selectBuilder {
return &selectBuilder{
h: h,
fields: f,
}
}
func (s *selectBuilder) Where(clause string, kv KV) *querySelectBuilder {
return &querySelectBuilder{
h: s.h,
fields: s.fields,
clause: clause,
kv: kv,
}
}
func (s *selectBuilder) Query(ctx context.Context, res interface{}) error {
q := &querySelectBuilder{
h: s.h,
fields: s.fields,
}
return q.Query(ctx, res)
}
type querySelectBuilder struct {
h handler
fields []string
clause string
kv KV
}
func (q *querySelectBuilder) ToSQL(res interface{}) (*SQL, error) {
if reflect.TypeOf(res).Kind() != reflect.Ptr {
return nil, fmt.Errorf("Query must be specified Ptr type")
}
var meta *tableMeta
switch reflect.TypeOf(res).Elem().Kind() {
case reflect.Slice:
s, ok := interface{}(reflect.New(reflect.TypeOf(res).Elem().Elem()).Interface()).(Schema)
if !ok {
return nil, fmt.Errorf("res is expected to pass schema type or slice of schema")
}
meta = metas[s.TableName()]
default:
s, ok := res.(Schema)
if !ok {
return nil, fmt.Errorf("res is expected to pass schema type or slice of schema")
}
meta = metas[s.TableName()]
}
selectColumns := []string{"*"}
if len(q.fields) > 0 {
if q.fields[0] != "*" {
selectColumns = q.fields
}
} else {
selectColumns = meta.Fields
}
quoted := make([]string, 0, len(selectColumns))
for _, col := range selectColumns {
qcol := ""
if col == "*" {
qcol = col
} else {
qcol = fmt.Sprintf("`%s`", col)
}
quoted = append(quoted, qcol)
}
syntax := []string{fmt.Sprintf("SELECT %s FROM `%s`", strings.Join(quoted, ","), meta.TableName)}
args := []interface{}{}
if q.clause != "" {
syntax = append(syntax, fmt.Sprintf("WHERE %s", q.clause))
query, params, err := sqlx.Named(strings.Join(syntax, " "), q.kv)
if err != nil {
return nil, err
}
syntax = []string{query}
args = params
}
asSliceForIn := false
for _, arg := range args {
if reflect.TypeOf(arg).Kind() == reflect.Slice {
asSliceForIn = true
break
}
}
var query string
var params []interface{}
if asSliceForIn {
var err error
query, params, err = sqlx.In(syntax[0], args...)
if err != nil {
return nil, err
}
query = q.h.Rebind(query)
} else {
query = syntax[0]
params = args
}
log.Infof("SQL: %s values: %#v", query, params)
return &SQL{
Query: query,
Args: params,
}, nil
}
func (q *querySelectBuilder) Query(ctx context.Context, res interface{}) error {
sql, err := q.ToSQL(res)
if err != nil {
return err
}
var resIsSlice bool
switch reflect.TypeOf(res).Elem().Kind() {
case reflect.Slice:
resIsSlice = true
default:
resIsSlice = false
}
if resIsSlice {
return q.h.SelectContext(ctx, res, sql.Query, sql.Args...)
} else {
return q.h.GetContext(ctx, res, sql.Query, sql.Args...)
}
}