forked from go-goe/goe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.go
More file actions
307 lines (249 loc) · 6.2 KB
/
Copy pathattribute.go
File metadata and controls
307 lines (249 loc) · 6.2 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package goe
import (
"reflect"
"github.com/go-goe/goe/enum"
"github.com/go-goe/goe/model"
"github.com/go-goe/goe/utils"
)
type oneToOne struct {
attributeStrings
}
func (o *oneToOne) getDb() *DB {
return o.db
}
func (o *oneToOne) isPrimaryKey() bool {
return false
}
func (o *oneToOne) getTableId() int {
return o.tableId
}
func (o *oneToOne) table() string {
return o.tableName
}
func (o *oneToOne) getAttributeName() string {
return o.attributeName
}
func createOneToOne(b body, typeOf reflect.Type) any {
mto := new(oneToOne)
targetPks := primaryKeys(typeOf)
count := 0
for i := range targetPks {
if targetPks[i].Name == b.prefixName {
count++
}
}
if count == 0 {
return nil
}
mto.attributeStrings = createAttributeStrings(
b.mapp.db,
b.driver.KeywordHandler(utils.TableNamePattern(b.typeOf.Name())),
b.fieldName,
b.mapp.tableId,
b.fieldId,
b.driver,
)
return mto
}
type manyToOne struct {
attributeStrings
}
func (m *manyToOne) getDb() *DB {
return m.db
}
func (m *manyToOne) isPrimaryKey() bool {
return false
}
func (m *manyToOne) getTableId() int {
return m.tableId
}
func (m *manyToOne) table() string {
return m.tableName
}
func (m *manyToOne) getAttributeName() string {
return m.attributeName
}
func createManyToOne(b body, typeOf reflect.Type) any {
mto := new(manyToOne)
targetPks := primaryKeys(typeOf)
count := 0
for i := range targetPks {
if targetPks[i].Name == b.prefixName {
count++
}
}
if count == 0 {
return nil
}
mto.attributeStrings = createAttributeStrings(
b.mapp.db,
b.driver.KeywordHandler(utils.TableNamePattern(b.typeOf.Name())),
b.fieldName,
b.mapp.tableId,
b.fieldId,
b.driver,
)
return mto
}
type attributeStrings struct {
db *DB
tableId int
tableName string
attributeName string
fieldId int
}
func createAttributeStrings(db *DB, table string, attributeName string, tableId, fieldId int, Driver Driver) attributeStrings {
return attributeStrings{
db: db,
tableName: table,
tableId: tableId,
fieldId: fieldId,
attributeName: Driver.KeywordHandler(utils.ColumnNamePattern(attributeName)),
}
}
type pk struct {
autoIncrement bool
attributeStrings
}
func (p *pk) getDb() *DB {
return p.db
}
func (p *pk) isPrimaryKey() bool {
return true
}
func (p *pk) getTableId() int {
return p.tableId
}
func (p *pk) table() string {
return p.tableName
}
func (p *pk) getAttributeName() string {
return p.attributeName
}
func createPk(db *DB, table string, attributeName string, autoIncrement bool, tableId, fieldId int, Driver Driver) *pk {
table = Driver.KeywordHandler(utils.TableNamePattern(table))
return &pk{
attributeStrings: createAttributeStrings(db, table, attributeName, tableId, fieldId, Driver),
autoIncrement: autoIncrement}
}
type att struct {
attributeStrings
}
func (a *att) getDb() *DB {
return a.db
}
func (a *att) isPrimaryKey() bool {
return false
}
func (a *att) getTableId() int {
return a.tableId
}
func (a *att) table() string {
return a.tableName
}
func (a *att) getAttributeName() string {
return a.attributeName
}
func createAtt(db *DB, attributeName string, table string, tableId, fieldId int, d Driver) *att {
return &att{
attributeStrings: createAttributeStrings(db, table, attributeName, tableId, fieldId, d)}
}
func (p *pk) buildAttributeSelect(b *builder) {
b.query.Attributes = append(b.query.Attributes, model.Attribute{
Table: p.tableName,
Name: p.attributeName,
})
}
func (a *att) buildAttributeSelect(b *builder) {
b.query.Attributes = append(b.query.Attributes, model.Attribute{
Table: a.tableName,
Name: a.attributeName,
})
}
func (m *manyToOne) buildAttributeSelect(b *builder) {
b.query.Attributes = append(b.query.Attributes, model.Attribute{
Table: m.tableName,
Name: m.attributeName,
})
}
func (o *oneToOne) buildAttributeSelect(b *builder) {
b.query.Attributes = append(b.query.Attributes, model.Attribute{
Table: o.tableName,
Name: o.attributeName,
})
}
func (p *pk) buildAttributeInsert(b *builder) {
if !p.autoIncrement {
b.inserts = append(b.inserts, p)
b.query.Attributes = append(b.query.Attributes, model.Attribute{Name: p.getAttributeName()})
return
}
b.query.ReturningId = &model.Attribute{Name: p.getAttributeName()}
b.pkFieldId = p.fieldId
}
func (p *pk) writeAttributeInsert(b *builder) {
b.fieldIds = append(b.fieldIds, p.fieldId)
}
func (a *att) buildAttributeInsert(b *builder) {
b.inserts = append(b.inserts, a)
b.query.Attributes = append(b.query.Attributes, model.Attribute{Name: a.getAttributeName()})
}
func (a *att) writeAttributeInsert(b *builder) {
b.fieldIds = append(b.fieldIds, a.fieldId)
}
func (m *manyToOne) buildAttributeInsert(b *builder) {
b.inserts = append(b.inserts, m)
b.query.Attributes = append(b.query.Attributes, model.Attribute{Name: m.getAttributeName()})
}
func (m *manyToOne) writeAttributeInsert(b *builder) {
b.fieldIds = append(b.fieldIds, m.fieldId)
}
func (o *oneToOne) buildAttributeInsert(b *builder) {
b.inserts = append(b.inserts, o)
b.query.Attributes = append(b.query.Attributes, model.Attribute{Name: o.getAttributeName()})
}
func (o *oneToOne) writeAttributeInsert(b *builder) {
b.fieldIds = append(b.fieldIds, o.fieldId)
}
func (p *pk) getFieldId() int {
return p.fieldId
}
func (a *att) getFieldId() int {
return a.fieldId
}
func (m *manyToOne) getFieldId() int {
return m.fieldId
}
func (o *oneToOne) getFieldId() int {
return o.fieldId
}
type aggregateResult struct {
attributeName string
table string
aggregateType enum.AggregateType
db *DB
}
func (a *aggregateResult) buildAttributeSelect(b *builder) {
b.query.Attributes = append(b.query.Attributes, model.Attribute{
Table: a.table,
Name: a.attributeName,
AggregateType: a.aggregateType})
}
func (a *aggregateResult) getDb() *DB {
return a.db
}
type functionResult struct {
attributeName string
table string
functionType enum.FunctionType
db *DB
}
func (f *functionResult) buildAttributeSelect(b *builder) {
b.query.Attributes = append(b.query.Attributes, model.Attribute{
Table: f.table,
Name: f.attributeName,
FunctionType: f.functionType})
}
func (f *functionResult) getDb() *DB {
return f.db
}