-
Notifications
You must be signed in to change notification settings - Fork 5
/
configurators.go
185 lines (158 loc) · 4.68 KB
/
configurators.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
package orm
import (
"database/sql"
"github.com/gertd/go-pluralize"
)
type EntityConfigurator struct {
connection string
table string
this Entity
relations map[string]interface{}
resolveRelations []func()
columnConstraints []*FieldConfigurator
}
func newEntityConfigurator() *EntityConfigurator {
return &EntityConfigurator{}
}
func (ec *EntityConfigurator) Table(name string) *EntityConfigurator {
ec.table = name
return ec
}
func (ec *EntityConfigurator) Connection(name string) *EntityConfigurator {
ec.connection = name
return ec
}
func (ec *EntityConfigurator) HasMany(property Entity, config HasManyConfig) *EntityConfigurator {
if ec.relations == nil {
ec.relations = map[string]interface{}{}
}
ec.resolveRelations = append(ec.resolveRelations, func() {
if config.PropertyForeignKey != "" && config.PropertyTable != "" {
ec.relations[config.PropertyTable] = config
return
}
configurator := newEntityConfigurator()
property.ConfigureEntity(configurator)
if config.PropertyTable == "" {
config.PropertyTable = configurator.table
}
if config.PropertyForeignKey == "" {
config.PropertyForeignKey = pluralize.NewClient().Singular(ec.table) + "_id"
}
ec.relations[configurator.table] = config
return
})
return ec
}
func (ec *EntityConfigurator) HasOne(property Entity, config HasOneConfig) *EntityConfigurator {
if ec.relations == nil {
ec.relations = map[string]interface{}{}
}
ec.resolveRelations = append(ec.resolveRelations, func() {
if config.PropertyForeignKey != "" && config.PropertyTable != "" {
ec.relations[config.PropertyTable] = config
return
}
configurator := newEntityConfigurator()
property.ConfigureEntity(configurator)
if config.PropertyTable == "" {
config.PropertyTable = configurator.table
}
if config.PropertyForeignKey == "" {
config.PropertyForeignKey = pluralize.NewClient().Singular(ec.table) + "_id"
}
ec.relations[configurator.table] = config
return
})
return ec
}
func (ec *EntityConfigurator) BelongsTo(owner Entity, config BelongsToConfig) *EntityConfigurator {
if ec.relations == nil {
ec.relations = map[string]interface{}{}
}
ec.resolveRelations = append(ec.resolveRelations, func() {
if config.ForeignColumnName != "" && config.LocalForeignKey != "" && config.OwnerTable != "" {
ec.relations[config.OwnerTable] = config
return
}
ownerConfigurator := newEntityConfigurator()
owner.ConfigureEntity(ownerConfigurator)
if config.OwnerTable == "" {
config.OwnerTable = ownerConfigurator.table
}
if config.LocalForeignKey == "" {
config.LocalForeignKey = pluralize.NewClient().Singular(ownerConfigurator.table) + "_id"
}
if config.ForeignColumnName == "" {
config.ForeignColumnName = "id"
}
ec.relations[ownerConfigurator.table] = config
})
return ec
}
func (ec *EntityConfigurator) BelongsToMany(owner Entity, config BelongsToManyConfig) *EntityConfigurator {
if ec.relations == nil {
ec.relations = map[string]interface{}{}
}
ec.resolveRelations = append(ec.resolveRelations, func() {
ownerConfigurator := newEntityConfigurator()
owner.ConfigureEntity(ownerConfigurator)
if config.OwnerLookupColumn == "" {
var pkName string
for _, field := range genericFieldsOf(owner) {
if field.IsPK {
pkName = field.Name
}
}
config.OwnerLookupColumn = pkName
}
if config.OwnerTable == "" {
config.OwnerTable = ownerConfigurator.table
}
if config.IntermediateTable == "" {
panic("cannot infer intermediate table yet")
}
if config.IntermediatePropertyID == "" {
config.IntermediatePropertyID = pluralize.NewClient().Singular(ownerConfigurator.table) + "_id"
}
if config.IntermediateOwnerID == "" {
config.IntermediateOwnerID = pluralize.NewClient().Singular(ec.table) + "_id"
}
ec.relations[ownerConfigurator.table] = config
})
return ec
}
type FieldConfigurator struct {
fieldName string
nullable sql.NullBool
primaryKey bool
column string
isCreatedAt bool
isUpdatedAt bool
isDeletedAt bool
}
func (ec *EntityConfigurator) Field(name string) *FieldConfigurator {
cc := &FieldConfigurator{fieldName: name}
ec.columnConstraints = append(ec.columnConstraints, cc)
return cc
}
func (fc *FieldConfigurator) IsPrimaryKey() *FieldConfigurator {
fc.primaryKey = true
return fc
}
func (fc *FieldConfigurator) IsCreatedAt() *FieldConfigurator {
fc.isCreatedAt = true
return fc
}
func (fc *FieldConfigurator) IsUpdatedAt() *FieldConfigurator {
fc.isUpdatedAt = true
return fc
}
func (fc *FieldConfigurator) IsDeletedAt() *FieldConfigurator {
fc.isDeletedAt = true
return fc
}
func (fc *FieldConfigurator) ColumnName(name string) *FieldConfigurator {
fc.column = name
return fc
}