This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatement_ex.go
225 lines (201 loc) · 4.66 KB
/
statement_ex.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package xorm
import (
"fmt"
"reflect"
"github.com/coscms/xorm/core"
)
// == ORDER BY ==
type orderBy []*orderByParam
type orderByParam struct {
Field string //Column name
Sort string // ASC/DESC
}
// == Fields ==
type fields []string //for Omit
func newJoinTables(stmt *Statement) *joinTables {
return &joinTables{
params: []*joinParam{},
statement: stmt,
}
}
// == JOIN ==
type joinTables struct {
params []*joinParam
statement *Statement
}
func (j *joinTables) New(stmt *Statement) *joinParam {
join := NewJoinParam(stmt)
j.params = append(j.params, join)
return join
}
func (j *joinTables) Add(join *joinParam) {
j.params = append(j.params, join)
}
func (j *joinTables) Size() int {
return len(j.params)
}
func (j *joinTables) HasJoin() bool {
return j.Size() > 0
}
func (j *joinTables) String() string {
var joinStr string
if j.Size() > 0 {
var t string
for _, join := range j.params {
joinStr += t + join.String()
t = ` `
}
} else {
joinStr = j.fromRelation()
}
return joinStr
}
func (j *joinTables) fromRelation() string {
r := j.statement.relation
if r == nil || r.IsTable {
return ``
}
var (
joinStr string
t string
)
for i, table := range r.Extends {
rt := r.RelTables[i]
if rt == nil || !rt.IsValid() {
continue
}
s := rt.JoinType + ` JOIN ` + j.statement.Engine.Quote(rt.TableName)
alias, _ := r.ExAlias[table.Name]
if len(alias) > 0 {
s += ` AS ` + j.statement.Engine.Quote(alias)
}
s += ` ON ` + rt.Where
joinStr += t + s
t = ` `
}
return joinStr
}
func (j *joinTables) Args() (args []interface{}) {
for _, join := range j.params {
args = append(args, join.Args...)
}
return
}
func NewJoinParam(stmt *Statement) *joinParam {
return &joinParam{
Args: make([]interface{}, 0),
statement: stmt,
}
}
type joinParam struct {
Operator string //LEFT/RIGHT/INNER...
Table string
Alias string
ONStr string
Args []interface{}
SQLStr string
statement *Statement
}
func (j *joinParam) String() string {
if len(j.SQLStr) > 0 {
return j.SQLStr
}
joinStr := j.Operator + ` JOIN ` + j.statement.Engine.Quote(j.Table)
if len(j.Alias) == 0 && j.statement.relation != nil {
j.Alias, _ = j.statement.relation.ExAlias[j.Table]
}
if len(j.Alias) > 0 {
joinStr += ` AS ` + j.statement.Engine.Quote(j.Alias)
}
if len(j.ONStr) > 0 {
joinStr += ` ON ` + j.ONStr
}
j.SQLStr = joinStr
return joinStr
}
// == Extends Statement ==
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
func (statement *Statement) join(joinOP string, tablename interface{}, condition string, args ...interface{}) *Statement {
join := statement.joinTables.New(statement)
join.Operator = joinOP
join.Table = ``
join.Alias = ``
join.ONStr = condition
join.Args = args
switch t := tablename.(type) {
case []string:
if len(t) > 1 {
join.Table = statement.withPrefix(t[0])
join.Alias = t[1]
} else if len(t) == 1 {
join.Table = statement.withPrefix(t[0])
}
case []interface{}:
l := len(t)
var table string
if l > 0 {
f := t[0]
v := rValue(f)
t := v.Type()
if t.Kind() == reflect.String {
table = f.(string)
} else if t.Kind() == reflect.Struct {
r := statement.Engine.autoMapType(v)
table = r.Name
} else {
table = fmt.Sprintf("%v", f)
}
join.Table = statement.withPrefix(table)
}
if l > 1 {
join.Alias = fmt.Sprintf("%v", t[1])
}
case core.SQL:
join.SQLStr = joinOP + ` JOIN ` + string(t)
case string:
join.Table = statement.withPrefix(t)
default:
v := rValue(tablename)
typ := v.Type()
if typ.Kind() == reflect.Struct {
r := statement.Engine.autoMapType(v)
join.Table = r.Name
} else {
join.Table = statement.withPrefix(fmt.Sprintf("%v", tablename))
}
}
return statement
}
func (statement *Statement) withPrefix(tableName string) string {
if len(tableName) > 0 && tableName[0] == '~' {
return statement.Engine.TablePrefix + tableName[1:]
}
return tableName
}
func (statement *Statement) JoinStr() string {
if statement.joinGenerated {
return statement.joinStr
}
statement.joinStr = statement.joinTables.String()
statement.joinArgs = statement.joinTables.Args()
statement.joinGenerated = true
return statement.joinStr
}
func (statement *Statement) SetRelation(r *core.Relation) {
statement.relation = r
if r == nil || len(r.Extends) < 1 {
return
}
if !r.IsTable {
statement.RefTable = r.Extends[0]
if len(statement.TableAlias) == 0 {
name := statement.RefTable.Name
statement.TableAlias, _ = r.ExAlias[name]
}
statement.tableName = statement.RefTable.Name
} else {
if len(statement.TableAlias) == 0 {
statement.TableAlias = r.Table.Type.Name()
}
}
}