/
types.go
257 lines (222 loc) · 4.71 KB
/
types.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
package internal
import (
"strings"
"github.com/sundayfun/xo/models"
)
// TemplateType represents a template type.
type TemplateType uint
// the order here will be the alter the output order per file.
const (
EnumTemplate TemplateType = iota
ProcTemplate
TypeTemplate
ForeignKeyTemplate
IndexTemplate
MapTemplate
QueryTypeTemplate
QueryTemplate
OptionalTemplate
TypeProtoTemplate
// always last
XOTemplate
)
// String returns the name for the associated template type.
func (tt TemplateType) String() string {
var s string
switch tt {
case XOTemplate:
s = "xo_db"
case EnumTemplate:
s = "enum"
case ProcTemplate:
s = "proc"
case TypeTemplate:
s = "type"
case ForeignKeyTemplate:
s = "foreignkey"
case IndexTemplate:
s = "index"
case MapTemplate:
s = "map"
case QueryTypeTemplate:
s = "querytype"
case QueryTemplate:
s = "query"
case OptionalTemplate:
s = "optional"
case TypeProtoTemplate:
s = "type"
default:
panic("unknown TemplateType")
}
return s
}
// RelType represents the different types of relational storage (table/view).
type RelType uint
const (
// Table reltype
Table RelType = iota
// View reltype
View
)
// EscType represents the different escape types.
type EscType uint
const (
SchemaEsc = iota
TableEsc
ColumnEsc
)
type LoadType uint
const (
LoadQueryFunc = iota
LoadMapFunc
)
// String provides the string representation of RelType.
func (rt RelType) String() string {
var s string
switch rt {
case Table:
s = "TABLE"
case View:
s = "VIEW"
default:
panic("unknown RelType")
}
return s
}
type MethodsConfig struct {
ListFields []string `yaml:"list_fields"`
ModelToPB map[string][]*TableConfig `yaml:"model_to_pb"`
}
type TableConfig struct {
Name string `yaml:"name"`
Sharding bool `yaml:"sharding"`
ShardingKey string `yaml:"sharding_key"`
Skips []string `yaml:"skips"`
}
// EnumValue holds data for a single enum value.
type EnumValue struct {
Name string
Val *models.EnumValue
Comment string
}
// Enum is a template item for a enum.
type Enum struct {
Name string
Schema string
Values []*EnumValue
Enum *models.Enum
Comment string
ReverseConstNames bool
}
// Proc is a template item for a stored procedure.
type Proc struct {
Name string
Schema string
ProcParams string
Params []*Field
Return *Field
Proc *models.Proc
Comment string
}
// Field contains field information.
type Field struct {
Name string
Type string
NilType string
Len int
Col *models.Column
Comment string
}
// Type is a template item for a type (ie, table/view/custom query).
type Type struct {
Name string
Schema string
RelType RelType
PrimaryKey *Field
PrimaryKeyFields []*Field
Fields []*Field
Indexes map[string]*Index
Table *models.Table
Comment string
HasDeletedField bool
// 是否支持分库分表
Sharding bool
ShardingKey *Field
// 同 sharding key 方便 goparamlist 使用
ShardingFields []*Field
}
func (t *Type) HasSharding() bool {
return t.Sharding
}
// ForeignKey is a template item for a foreign relationship on a table.
type ForeignKey struct {
Name string
Schema string
Type *Type
Field *Field
RefType *Type
RefField *Field
ForeignKey *models.ForeignKey
Comment string
}
// Index is a template item for a index into a table.
type Index struct {
FuncName string
MapFuncName string
MapField *Field
Schema string
Type *Type
Fields []*Field
Index *models.Index
Comment string
}
func (i *Index) HasSharding() bool {
return i.Type.HasSharding()
}
type MethodsOption struct {
Type *Type
Sub string
ListFields bool
ModelToPB bool
ModelToPBConfig *ModelToPBConfig
}
type ModelToPBConfig struct {
ImportService string
SkipFields map[string]struct{}
Sharding bool
ShardingKey string
}
type ProtoConfig []*MethodsOption
func (t ProtoConfig) Len() int {
return len(t)
}
func (t ProtoConfig) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
func (t ProtoConfig) Less(i, j int) bool {
return strings.Compare(t[i].Sub, t[j].Sub) < 0
}
// QueryParam is a query parameter for a custom query.
type QueryParam struct {
Name string
Type string
Interpolate bool
}
// Query is a template item for a custom query.
type Query struct {
Schema string
Name string
Query []string
QueryComments []string
QueryParams []*QueryParam
OnlyOne bool
Interpolate bool
Type *Type
Comment string
}
type Imports struct {
Package string
Imports []string
Schema string
}