-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_cond.go
169 lines (151 loc) · 3.44 KB
/
query_cond.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
package orm
import (
"bytes"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
type CondQuery interface {
Table(table string) Query
TableAlias(table string, alias string) Query
Field(field string) Query
FieldAlias(field string, alias string) Query
Where(field string, operate string, value interface{}) Query
Limit(start int, offset int) Query
Page(page int, size int) Query
Order(field string) Query
Asc(field string) Query
Desc(field string) Query
Sql(query string, args interface{}) Query
}
func (q *query) Table(table string) Query {
q.table = "`" + table + "`"
return q
}
func (q *query) TableAlias(table string, alias string) Query {
q.table = "`" + table + "`"
if alias != "" {
q.table += " as " + alias
}
return q
}
func (q *query) Field(field string) Query {
return q.FieldAlias("`"+field+"`", "")
}
func (q *query) FieldAlias(field string, alias string) Query {
if alias != "" {
field += " as " + alias
}
if q.fields == nil {
q.fields = []string{field}
} else {
q.fields = append(q.fields, field)
}
return q
}
func (q *query) Where(field string, operate string, value interface{}) Query {
sql := "`" + field + "`" + operate + " ? "
if q.where == nil {
q.where = []string{sql}
} else {
q.where = append(q.where, sql)
}
if q.whereArgs == nil {
q.whereArgs = []interface{}{value}
} else {
q.whereArgs = append(q.whereArgs, value)
}
return q
}
func (q *query) Limit(start int, offset int) Query {
q.limit = []string{strconv.Itoa(start), strconv.Itoa(offset)}
return q
}
func (q *query) Page(page int, size int) Query {
start := 0
if page >= 1 {
start = (page - 1) * size
}
q.Limit(start, size)
return q
}
func (q *query) Order(field string) Query {
field = "`" + field + "`"
if q.order == nil {
q.order = []string{field}
} else {
q.order = append(q.order, field)
}
return q
}
func (q *query) Asc(field string) Query {
field = "`" + field + "`" + " asc"
if q.order == nil {
q.order = []string{field}
} else {
q.order = append(q.order, field)
}
return q
}
func (q *query) Desc(field string) Query {
field = "`" + field + "`" + " desc"
if q.order == nil {
q.order = []string{field}
} else {
q.order = append(q.order, field)
}
return q
}
func (q *query) Sql(query string, args interface{}) Query {
value := reflect.ValueOf(args)
switch value.Kind() {
case reflect.Slice:
return q.sql(query, args.([]interface{})...)
case reflect.Map:
query, args, err := MapToSlice(query, args)
if err != nil {
return q.error(err)
}
return q.sql(query, args)
case reflect.Struct:
query, args, err := StructToSlice(query, args)
if err != nil {
return q.error(err)
}
return q.sql(query, args)
default:
return q.error(errors.New("args can be slice, map, struct"))
}
}
func (q *query) getWhere() (string, []interface{}, error) {
var args []interface{}
buf := bytes.Buffer{}
if q.where != nil {
buf.WriteString(strings.Join(q.where, " AND "))
args = q.whereArgs
} else {
buf.WriteString("1")
}
if q.order != nil {
buf.WriteString(" ORDER BY ")
buf.WriteString(strings.Join(q.order, ","))
}
if q.limit != nil {
buf.WriteString(" LIMIT ")
buf.WriteString(strings.Join(q.limit, ","))
}
return buf.String(), args, nil
}
func (q *query) getField() (string, error) {
if q.fields != nil {
return strings.Join(q.fields, ","), nil
} else {
return "*", nil
}
}
func (q *query) sql(query string, args ...interface{}) *query {
q.sqlQuery = query
q.args = args
return q
}