forked from jinzhu/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
178 lines (155 loc) · 3.88 KB
/
search.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
package gorm
import "strconv"
type search struct {
db *DB
WhereConditions []map[string]interface{}
OrConditions []map[string]interface{}
NotConditions []map[string]interface{}
InitAttrs []interface{}
AssignAttrs []interface{}
HavingCondition map[string]interface{}
Orders []string
Joins string
Select string
Offset string
Limit string
Group string
TableName string
Unscope bool
Raw bool
}
func (s *search) clone() *search {
return &search{
WhereConditions: s.WhereConditions,
OrConditions: s.OrConditions,
NotConditions: s.NotConditions,
InitAttrs: s.InitAttrs,
AssignAttrs: s.AssignAttrs,
HavingCondition: s.HavingCondition,
Orders: s.Orders,
Select: s.Select,
Offset: s.Offset,
Limit: s.Limit,
Unscope: s.Unscope,
Group: s.Group,
Joins: s.Joins,
TableName: s.TableName,
Raw: s.Raw,
}
}
func (s *search) where(query interface{}, values ...interface{}) *search {
s.WhereConditions = append(s.WhereConditions, map[string]interface{}{"query": query, "args": values})
return s
}
func (s *search) not(query interface{}, values ...interface{}) *search {
s.NotConditions = append(s.NotConditions, map[string]interface{}{"query": query, "args": values})
return s
}
func (s *search) or(query interface{}, values ...interface{}) *search {
s.OrConditions = append(s.OrConditions, map[string]interface{}{"query": query, "args": values})
return s
}
func (s *search) attrs(attrs ...interface{}) *search {
s.InitAttrs = append(s.InitAttrs, toSearchableMap(attrs...))
return s
}
func (s *search) assign(attrs ...interface{}) *search {
s.AssignAttrs = append(s.AssignAttrs, toSearchableMap(attrs...))
return s
}
func (s *search) order(value string, reorder ...bool) *search {
if len(reorder) > 0 && reorder[0] {
s.Orders = []string{value}
} else {
s.Orders = append(s.Orders, value)
}
return s
}
func (s *search) selects(value interface{}) *search {
s.Select = s.getInterfaceAsSql(value)
return s
}
func (s *search) limit(value interface{}) *search {
s.Limit = s.getInterfaceAsSql(value)
return s
}
func (s *search) offset(value interface{}) *search {
s.Offset = s.getInterfaceAsSql(value)
return s
}
func (s *search) group(query string) *search {
s.Group = s.getInterfaceAsSql(query)
return s
}
func (s *search) having(query string, values ...interface{}) *search {
s.HavingCondition = map[string]interface{}{"query": query, "args": values}
return s
}
func (s *search) includes(value interface{}) *search {
return s
}
func (s *search) joins(query string) *search {
s.Joins = query
return s
}
func (s *search) raw(b bool) *search {
s.Raw = b
return s
}
func (s *search) unscoped() *search {
s.Unscope = true
return s
}
func (s *search) table(name string) *search {
s.TableName = name
return s
}
func (s *search) getInterfaceAsSql(value interface{}) (str string) {
var s_num int64
var u_num uint64
var isString, unsigned bool = false, false
switch value.(type) {
case string:
str = value.(string)
isString = true
case int:
s_num = int64(value.(int))
case int8:
s_num = int64(value.(int8))
case int16:
s_num = int64(value.(int16))
case int32:
s_num = int64(value.(int32))
case int64:
s_num = int64(value.(int64))
case uint:
u_num = uint64(value.(uint))
unsigned = true
case uint8:
u_num = uint64(value.(uint8))
unsigned = true
case uint16:
u_num = uint64(value.(uint16))
unsigned = true
case uint32:
u_num = uint64(value.(uint32))
unsigned = true
case uint64:
u_num = uint64(value.(uint64))
unsigned = true
default:
s.db.err(InvalidSql)
}
if !isString {
if unsigned {
str = strconv.FormatUint(u_num, 10)
} else {
if s_num < 0 {
str = ""
} else {
str = strconv.FormatInt(s_num, 10)
}
}
}
return
}