This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from orus-io/yago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures_yago_test.go
613 lines (538 loc) · 18.3 KB
/
fixtures_yago_test.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package yago_test
// generated with yago. Better NOT to edit!
import (
"database/sql"
"fmt"
"reflect"
"github.com/slicebit/qb"
"github.com/m4rw3r/uuid"
"github.com/orus-io/yago"
)
const (
// SimpleStructID is the ID field name
SimpleStructID = "ID"
// SimpleStructIDColumnName is the ID field associated column name
SimpleStructIDColumnName = "id"
// SimpleStructName is the Name field name
SimpleStructName = "Name"
// SimpleStructNameColumnName is the Name field associated column name
SimpleStructNameColumnName = "name"
)
const (
// SimpleStructTableName is the SimpleStruct associated table name
SimpleStructTableName = "simple_struct"
)
var simpleStructTable = qb.Table(
SimpleStructTableName,
qb.Column(SimpleStructIDColumnName, qb.BigInt()).PrimaryKey().AutoIncrement().NotNull(),
qb.Column(SimpleStructNameColumnName, qb.Varchar()).NotNull(),
qb.UniqueKey(
SimpleStructNameColumnName,
),
)
var simpleStructType = reflect.TypeOf(SimpleStruct{})
// StructType returns the reflect.Type of the struct
// It is used for indexing mappers (and only that I guess, so
// it could be replaced with a unique identifier).
func (SimpleStruct) StructType() reflect.Type {
return simpleStructType
}
// SimpleStructModel provides direct access to helpers for SimpleStruct
// queries
type SimpleStructModel struct {
mapper *SimpleStructMapper
ID yago.ScalarField
Name yago.ScalarField
}
// NewSimpleStructModel returns a new SimpleStructModel
func NewSimpleStructModel(meta *yago.Metadata) SimpleStructModel {
mapper := NewSimpleStructMapper()
meta.AddMapper(mapper)
return SimpleStructModel{
mapper: mapper,
ID: yago.NewScalarField(mapper.Table().C(SimpleStructIDColumnName)),
Name: yago.NewScalarField(mapper.Table().C(SimpleStructNameColumnName)),
}
}
// GetMapper returns the associated SimpleStructMapper instance
func (m SimpleStructModel) GetMapper() yago.Mapper {
return m.mapper
}
// NewSimpleStructMapper initialize a NewSimpleStructMapper
func NewSimpleStructMapper() *SimpleStructMapper {
m := &SimpleStructMapper{}
return m
}
// SimpleStructMapper is the SimpleStruct mapper
type SimpleStructMapper struct{}
// GetMapper returns itself
func (mapper *SimpleStructMapper) GetMapper() yago.Mapper {
return mapper
}
// Name returns the mapper name
func (*SimpleStructMapper) Name() string {
return "yago_test/SimpleStruct"
}
// Table returns the mapper table
func (*SimpleStructMapper) Table() *qb.TableElem {
return &simpleStructTable
}
// StructType returns the reflect.Type of the mapped structure
func (SimpleStructMapper) StructType() reflect.Type {
return simpleStructType
}
// SQLValues returns values as a map
// The primary key is included only if having non-default values
func (mapper SimpleStructMapper) SQLValues(instance yago.MappedStruct, fields ...string) map[string]interface{} {
s, ok := instance.(*SimpleStruct)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &SimpleStruct{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
allValues := len(fields) == 0
m := make(map[string]interface{})
if s.ID != 0 {
m[SimpleStructIDColumnName] = s.ID
}
if allValues || yago.StringListContains(fields, SimpleStructName) {
m[SimpleStructNameColumnName] = s.Name
}
return m
}
// FieldList returns the list of fields for a select
func (mapper SimpleStructMapper) FieldList() []qb.Clause {
return []qb.Clause{
simpleStructTable.C(SimpleStructIDColumnName),
simpleStructTable.C(SimpleStructNameColumnName),
}
}
// ScanPKey scans the primary key only
func (mapper SimpleStructMapper) ScanPKey(rows *sql.Rows, instance yago.MappedStruct) error {
s, ok := instance.(*SimpleStruct)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &SimpleStruct{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
return rows.Scan(
&s.ID,
)
}
// Scan a struct
func (mapper SimpleStructMapper) Scan(rows *sql.Rows, instance yago.MappedStruct) error {
s, ok := instance.(*SimpleStruct)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &SimpleStruct{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
if err := rows.Scan(
&s.ID,
&s.Name,
); err != nil {
return err
}
return nil
}
// AutoIncrementPKey return true if a column of the pkey is autoincremented
func (SimpleStructMapper) AutoIncrementPKey() bool {
return true
}
// LoadAutoIncrementPKeyValue set the pkey autoincremented column value
func (SimpleStructMapper) LoadAutoIncrementPKeyValue(instance yago.MappedStruct, value int64) {
s := instance.(*SimpleStruct)
s.ID = value
}
// PKey returns the instance primary key values
func (mapper SimpleStructMapper) PKey(instance yago.MappedStruct) (values []interface{}) {
str := instance.(*SimpleStruct)
values = append(values, str.ID)
return
}
// PKeyClause returns a clause that matches the instance primary key
func (mapper SimpleStructMapper) PKeyClause(values []interface{}) qb.Clause {
return simpleStructTable.C(SimpleStructIDColumnName).Eq(values[0])
}
const (
// BaseStructID is the ID field name
BaseStructID = "ID"
// BaseStructIDColumnName is the ID field associated column name
BaseStructIDColumnName = "id"
// BaseStructCreatedAt is the CreatedAt field name
BaseStructCreatedAt = "CreatedAt"
// BaseStructCreatedAtColumnName is the CreatedAt field associated column name
BaseStructCreatedAtColumnName = "created_at"
// BaseStructUpdatedAt is the UpdatedAt field name
BaseStructUpdatedAt = "UpdatedAt"
// BaseStructUpdatedAtColumnName is the UpdatedAt field associated column name
BaseStructUpdatedAtColumnName = "updated_at"
)
const (
// PersonStructActive is the Active field name
PersonStructActive = "Active"
// PersonStructActiveColumnName is the Active field associated column name
PersonStructActiveColumnName = "active"
// PersonStructFirstName is the FirstName field name
PersonStructFirstName = "FirstName"
// PersonStructFirstNameColumnName is the FirstName field associated column name
PersonStructFirstNameColumnName = "first_name"
// PersonStructLastName is the LastName field name
PersonStructLastName = "LastName"
// PersonStructLastNameColumnName is the LastName field associated column name
PersonStructLastNameColumnName = "last_name"
// PersonStructGender is the Gender field name
PersonStructGender = "Gender"
// PersonStructGenderColumnName is the Gender field associated column name
PersonStructGenderColumnName = "gender"
)
const (
// PersonStructTableName is the PersonStruct associated table name
PersonStructTableName = "person_struct"
)
var personStructTable = qb.Table(
PersonStructTableName,
qb.Column(PersonStructActiveColumnName, qb.Boolean()).NotNull(),
qb.Column(PersonStructFirstNameColumnName, qb.Varchar()).NotNull(),
qb.Column(PersonStructLastNameColumnName, qb.Varchar()).Null(),
qb.Column(PersonStructGenderColumnName, qb.Varchar()).NotNull(),
qb.Column(BaseStructIDColumnName, qb.UUID()).PrimaryKey().NotNull(),
qb.Column(BaseStructCreatedAtColumnName, qb.Timestamp()).NotNull(),
qb.Column(BaseStructUpdatedAtColumnName, qb.Timestamp()).NotNull(),
qb.UniqueKey(
PersonStructFirstNameColumnName,
),
)
var personStructType = reflect.TypeOf(PersonStruct{})
// StructType returns the reflect.Type of the struct
// It is used for indexing mappers (and only that I guess, so
// it could be replaced with a unique identifier).
func (PersonStruct) StructType() reflect.Type {
return personStructType
}
// PersonStructModel provides direct access to helpers for PersonStruct
// queries
type PersonStructModel struct {
mapper *PersonStructMapper
Active yago.ScalarField
FirstName yago.ScalarField
LastName yago.ScalarField
Gender yago.MarshaledScalarField
ID yago.ScalarField
CreatedAt yago.ScalarField
UpdatedAt yago.ScalarField
}
// NewPersonStructModel returns a new PersonStructModel
func NewPersonStructModel(meta *yago.Metadata) PersonStructModel {
mapper := NewPersonStructMapper()
meta.AddMapper(mapper)
return PersonStructModel{
mapper: mapper,
Active: yago.NewScalarField(mapper.Table().C(PersonStructActiveColumnName)),
FirstName: yago.NewScalarField(mapper.Table().C(PersonStructFirstNameColumnName)),
LastName: yago.NewScalarField(mapper.Table().C(PersonStructLastNameColumnName)),
Gender: yago.NewMarshaledScalarField(mapper.Table().C(PersonStructGenderColumnName)),
ID: yago.NewScalarField(mapper.Table().C(BaseStructIDColumnName)),
CreatedAt: yago.NewScalarField(mapper.Table().C(BaseStructCreatedAtColumnName)),
UpdatedAt: yago.NewScalarField(mapper.Table().C(BaseStructUpdatedAtColumnName)),
}
}
// GetMapper returns the associated PersonStructMapper instance
func (m PersonStructModel) GetMapper() yago.Mapper {
return m.mapper
}
// NewPersonStructMapper initialize a NewPersonStructMapper
func NewPersonStructMapper() *PersonStructMapper {
m := &PersonStructMapper{}
return m
}
// PersonStructMapper is the PersonStruct mapper
type PersonStructMapper struct{}
// GetMapper returns itself
func (mapper *PersonStructMapper) GetMapper() yago.Mapper {
return mapper
}
// Name returns the mapper name
func (*PersonStructMapper) Name() string {
return "yago_test/PersonStruct"
}
// Table returns the mapper table
func (*PersonStructMapper) Table() *qb.TableElem {
return &personStructTable
}
// StructType returns the reflect.Type of the mapped structure
func (PersonStructMapper) StructType() reflect.Type {
return personStructType
}
// SQLValues returns values as a map
// The primary key is included only if having non-default values
func (mapper PersonStructMapper) SQLValues(instance yago.MappedStruct, fields ...string) map[string]interface{} {
s, ok := instance.(*PersonStruct)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &PersonStruct{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
allValues := len(fields) == 0
m := make(map[string]interface{})
if s.ID != (uuid.UUID{}) {
m[BaseStructIDColumnName] = s.ID
}
if allValues || yago.StringListContains(fields, PersonStructActive) {
m[PersonStructActiveColumnName] = s.Active
}
if allValues || yago.StringListContains(fields, PersonStructFirstName) {
m[PersonStructFirstNameColumnName] = s.FirstName
}
if allValues || yago.StringListContains(fields, PersonStructLastName) {
m[PersonStructLastNameColumnName] = s.LastName
}
if allValues || yago.StringListContains(fields, PersonStructGender) {
if b, err := s.Gender.MarshalText(); err == nil {
m[PersonStructGenderColumnName] = b
} else {
panic(err)
}
}
if allValues || yago.StringListContains(fields, BaseStructCreatedAt) {
m[BaseStructCreatedAtColumnName] = s.CreatedAt
}
if allValues || yago.StringListContains(fields, BaseStructUpdatedAt) {
m[BaseStructUpdatedAtColumnName] = s.UpdatedAt
}
return m
}
// FieldList returns the list of fields for a select
func (mapper PersonStructMapper) FieldList() []qb.Clause {
return []qb.Clause{
personStructTable.C(PersonStructActiveColumnName),
personStructTable.C(PersonStructFirstNameColumnName),
personStructTable.C(PersonStructLastNameColumnName),
personStructTable.C(PersonStructGenderColumnName),
personStructTable.C(BaseStructIDColumnName),
personStructTable.C(BaseStructCreatedAtColumnName),
personStructTable.C(BaseStructUpdatedAtColumnName),
}
}
// ScanPKey scans the primary key only
func (mapper PersonStructMapper) ScanPKey(rows *sql.Rows, instance yago.MappedStruct) error {
s, ok := instance.(*PersonStruct)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &PersonStruct{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
return rows.Scan(
&s.ID,
)
}
// Scan a struct
func (mapper PersonStructMapper) Scan(rows *sql.Rows, instance yago.MappedStruct) error {
s, ok := instance.(*PersonStruct)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &PersonStruct{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
var GenderText []byte
if err := rows.Scan(
&s.Active,
&s.FirstName,
&s.LastName,
&GenderText,
&s.ID,
&s.CreatedAt,
&s.UpdatedAt,
); err != nil {
return err
}
if err := s.Gender.UnmarshalText(GenderText); err != nil {
return err
}
return nil
}
// AutoIncrementPKey return true if a column of the pkey is autoincremented
func (PersonStructMapper) AutoIncrementPKey() bool {
return false
}
// LoadAutoIncrementPKeyValue set the pkey autoincremented column value
func (PersonStructMapper) LoadAutoIncrementPKeyValue(instance yago.MappedStruct, value int64) {
panic("PersonStruct has no auto increment column in its pkey")
}
// PKey returns the instance primary key values
func (mapper PersonStructMapper) PKey(instance yago.MappedStruct) (values []interface{}) {
str := instance.(*PersonStruct)
values = append(values, str.ID)
return
}
// PKeyClause returns a clause that matches the instance primary key
func (mapper PersonStructMapper) PKeyClause(values []interface{}) qb.Clause {
return personStructTable.C(BaseStructIDColumnName).Eq(values[0])
}
const (
// AutoIncBaseID is the ID field name
AutoIncBaseID = "ID"
// AutoIncBaseIDColumnName is the ID field associated column name
AutoIncBaseIDColumnName = "id"
)
const (
// AutoIncChildName is the Name field name
AutoIncChildName = "Name"
// AutoIncChildNameColumnName is the Name field associated column name
AutoIncChildNameColumnName = "name"
// AutoIncChildPerson is the Person field name
AutoIncChildPerson = "Person"
// AutoIncChildPersonColumnName is the Person field associated column name
AutoIncChildPersonColumnName = "person"
)
const (
// AutoIncChildTableName is the AutoIncChild associated table name
AutoIncChildTableName = "auto_inc_child"
)
var autoIncChildTable = qb.Table(
AutoIncChildTableName,
qb.Column(AutoIncChildNameColumnName, qb.Varchar()).NotNull(),
qb.Column(AutoIncChildPersonColumnName, qb.UUID()).NotNull(),
qb.Column(AutoIncBaseIDColumnName, qb.BigInt()).PrimaryKey().AutoIncrement().NotNull(),
qb.ForeignKey(AutoIncChildPersonColumnName).References(PersonStructTableName, BaseStructIDColumnName).OnUpdate("CASCADE").OnDelete("SET NULL"),
)
var autoIncChildType = reflect.TypeOf(AutoIncChild{})
// StructType returns the reflect.Type of the struct
// It is used for indexing mappers (and only that I guess, so
// it could be replaced with a unique identifier).
func (AutoIncChild) StructType() reflect.Type {
return autoIncChildType
}
// AutoIncChildModel provides direct access to helpers for AutoIncChild
// queries
type AutoIncChildModel struct {
mapper *AutoIncChildMapper
Name yago.ScalarField
Person yago.ScalarField
ID yago.ScalarField
}
// NewAutoIncChildModel returns a new AutoIncChildModel
func NewAutoIncChildModel(meta *yago.Metadata) AutoIncChildModel {
mapper := NewAutoIncChildMapper()
meta.AddMapper(mapper)
return AutoIncChildModel{
mapper: mapper,
Name: yago.NewScalarField(mapper.Table().C(AutoIncChildNameColumnName)),
Person: yago.NewScalarField(mapper.Table().C(AutoIncChildPersonColumnName)),
ID: yago.NewScalarField(mapper.Table().C(AutoIncBaseIDColumnName)),
}
}
// GetMapper returns the associated AutoIncChildMapper instance
func (m AutoIncChildModel) GetMapper() yago.Mapper {
return m.mapper
}
// NewAutoIncChildMapper initialize a NewAutoIncChildMapper
func NewAutoIncChildMapper() *AutoIncChildMapper {
m := &AutoIncChildMapper{}
return m
}
// AutoIncChildMapper is the AutoIncChild mapper
type AutoIncChildMapper struct{}
// GetMapper returns itself
func (mapper *AutoIncChildMapper) GetMapper() yago.Mapper {
return mapper
}
// Name returns the mapper name
func (*AutoIncChildMapper) Name() string {
return "yago_test/AutoIncChild"
}
// Table returns the mapper table
func (*AutoIncChildMapper) Table() *qb.TableElem {
return &autoIncChildTable
}
// StructType returns the reflect.Type of the mapped structure
func (AutoIncChildMapper) StructType() reflect.Type {
return autoIncChildType
}
// SQLValues returns values as a map
// The primary key is included only if having non-default values
func (mapper AutoIncChildMapper) SQLValues(instance yago.MappedStruct, fields ...string) map[string]interface{} {
s, ok := instance.(*AutoIncChild)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &AutoIncChild{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
allValues := len(fields) == 0
m := make(map[string]interface{})
if s.ID != 0 {
m[AutoIncBaseIDColumnName] = s.ID
}
if allValues || yago.StringListContains(fields, AutoIncChildName) {
m[AutoIncChildNameColumnName] = s.Name
}
if allValues || yago.StringListContains(fields, AutoIncChildPerson) {
m[AutoIncChildPersonColumnName] = s.Person
}
return m
}
// FieldList returns the list of fields for a select
func (mapper AutoIncChildMapper) FieldList() []qb.Clause {
return []qb.Clause{
autoIncChildTable.C(AutoIncChildNameColumnName),
autoIncChildTable.C(AutoIncChildPersonColumnName),
autoIncChildTable.C(AutoIncBaseIDColumnName),
}
}
// ScanPKey scans the primary key only
func (mapper AutoIncChildMapper) ScanPKey(rows *sql.Rows, instance yago.MappedStruct) error {
s, ok := instance.(*AutoIncChild)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &AutoIncChild{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
return rows.Scan(
&s.ID,
)
}
// Scan a struct
func (mapper AutoIncChildMapper) Scan(rows *sql.Rows, instance yago.MappedStruct) error {
s, ok := instance.(*AutoIncChild)
if !ok {
panic(fmt.Sprintf(
"Wrong struct type passed to the mapper. Expected &AutoIncChild{}, got %s",
reflect.TypeOf(instance).Name(),
))
}
if err := rows.Scan(
&s.Name,
&s.Person,
&s.ID,
); err != nil {
return err
}
return nil
}
// AutoIncrementPKey return true if a column of the pkey is autoincremented
func (AutoIncChildMapper) AutoIncrementPKey() bool {
return true
}
// LoadAutoIncrementPKeyValue set the pkey autoincremented column value
func (AutoIncChildMapper) LoadAutoIncrementPKeyValue(instance yago.MappedStruct, value int64) {
s := instance.(*AutoIncChild)
s.ID = value
}
// PKey returns the instance primary key values
func (mapper AutoIncChildMapper) PKey(instance yago.MappedStruct) (values []interface{}) {
str := instance.(*AutoIncChild)
values = append(values, str.ID)
return
}
// PKeyClause returns a clause that matches the instance primary key
func (mapper AutoIncChildMapper) PKeyClause(values []interface{}) qb.Clause {
return autoIncChildTable.C(AutoIncBaseIDColumnName).Eq(values[0])
}