This repository has been archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
343 lines (304 loc) · 8.42 KB
/
metadata.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
/**
* Date: 9/02/14
* Time: 7:03 PM
*/
package opal
import (
"database/sql"
"fmt"
"log"
"reflect"
"strings"
)
type ModelMetadata struct {
Model
this reflect.Type
table Table
// The metadata columns
columns []Column
columnsByIndex map[int]*Column
columnsByFieldName map[string]*Column
keysByIndex map[int]*Column
keysByFieldName map[string]*Column
// TODO maps are not guaranteed to keep insert order
// Sort an int array
// Also combine two maps where necessary here for
// easier insertion of parameters
// Prepared query store
preparedStatements map[string]*sql.Stmt
Service ModelDAO
}
// Hold special data about a domain object model
func NewMetadata(pModel Model, pType reflect.Type) *ModelMetadata {
o := new(ModelMetadata)
o.this = pType
o.Model = pModel
o.columnsByIndex = make(map[int]*Column)
o.columnsByFieldName = make(map[string]*Column)
o.keysByIndex = make(map[int]*Column)
o.keysByFieldName = make(map[string]*Column)
o.preparedStatements = make(map[string]*sql.Stmt)
return o
}
func (o *ModelMetadata) addStmt(pDB *sql.DB, pKey string, pValue Sql) {
sql := pValue.String()
log.Printf("Opal.ModelMetadata.addStmt: %s", sql)
stmt, err := pDB.Prepare(sql)
if err != nil {
panic(err) // TODO wtf?
}
o.preparedStatements[pKey] = stmt
//log.Printf("Opal.ModelMetadata.addStmt: %#v", o.preparedStatements[pKey])
}
// Get the columns metadata
func (o ModelMetadata) Table() Table {
return o.table
}
// Get the columns metadata
func (o ModelMetadata) Columns() []Column {
return o.columns
}
// Get the column metadata by its natural index
func (o ModelMetadata) ColumnByIndex(index int) Column {
return o.columns[index]
}
// Get the column metadata by the domains field name
func (o ModelMetadata) Column(pField string) Column {
return *o.columnsByFieldName[pField]
}
// Get the column metadata by the domains field index
func (o ModelMetadata) ColumnByFieldIndex(pIndex int) Column {
return *o.columnsByIndex[pIndex]
}
// Get the type of the entity domain parent
func (o ModelMetadata) Type() reflect.Type {
return o.this
}
// TODO
func (o *ModelMetadata) AddTable(pTable Table, pKeyFieldNames ...string) {
o.table = pTable
o.table.Key = pKeyFieldNames
}
type GenerationType int
const (
AUTO GenerationType = iota
INCREMENT
TABLE
IDENTITY
SEQUENCE
)
type KeyColumn struct {
Auto bool
Type GenerationType
}
// TODO
func (o *ModelMetadata) AddKey(pField string, pIndex int, pColumn Column, pKind reflect.Kind) {
// k := Key{Auto: true, Type: INCREMENT}
// keyTag := ExtractOpalTags(o.this.FieldByName("Key").Tag)
// if keyTag.Get("Auto") != "" {
// k.Nilable = pColumn.Nilable
// }
tag := ExtractOpalTags(o.this.Field(pIndex).Tag)
// Set the default values if not specified
c := Column{Length: 255, Insertable: true, Updatable: true, Nilable: true}
if tag.Get("Nilable") != "" {
c.Nilable = pColumn.Nilable
}
if tag.Get("Insertable") != "" {
c.Insertable = pColumn.Insertable
}
if tag.Get("Updatable") != "" {
c.Updatable = pColumn.Updatable
}
if tag.Get("Length") != "" {
c.Length = pColumn.Length
}
c.Identifier = pField
c.Name = pColumn.Name
if tag.Get("Name") == "" {
c.Name = c.Identifier
}
c.Unique = pColumn.Unique
c.Precision = pColumn.Precision
c.Scale = pColumn.Scale
c.AutoIncrement = pColumn.AutoIncrement
c.Kind = pKind
o.columns = append(o.columns, c)
o.keysByFieldName[pField] = &o.columns[len(o.columns)-1]
o.keysByIndex[pIndex] = o.keysByFieldName[pField]
}
// TODO
func (o *ModelMetadata) AddColumn(pField string, pIndex int, pColumn Column, pKind reflect.Kind) {
tag := ExtractOpalTags(o.this.Field(pIndex).Tag)
// Set the default values if not specified
c := Column{Insertable: true, Length: 255, Nilable: true, Updatable: true}
if tag.Get("Nilable") != "" {
c.Nilable = pColumn.Nilable
}
if tag.Get("Insertable") != "" {
c.Insertable = pColumn.Insertable
}
if tag.Get("Updatable") != "" {
c.Updatable = pColumn.Updatable
}
if tag.Get("Length") != "" {
c.Length = pColumn.Length
}
c.Identifier = pField
c.Name = pColumn.Name
if tag.Get("Name") == "" {
c.Name = c.Identifier
}
c.Unique = pColumn.Unique
c.Precision = pColumn.Precision
c.Scale = pColumn.Scale
c.Kind = pKind
o.columns = append(o.columns, c)
o.columnsByFieldName[pField] = &o.columns[len(o.columns)-1]
o.columnsByIndex[pIndex] = o.columnsByFieldName[pField]
}
// TODO
func (o *ModelMetadata) ReplaceTableIdentifiers(sql string, fDialect DialectEncoder) string {
return strings.Replace(sql, o.this.Name(), fDialect(o.table.Name), -1)
}
// TODO
func (o *ModelMetadata) ReplaceColumnIdentifiers(sql string, fDialect DialectEncoder) string {
for _, column := range o.columns {
sql = strings.Replace(sql, column.Name, fDialect(column.Name), -1)
}
return sql
}
// TODO
func (o *ModelMetadata) ColumnsList(pBuilder *SqlBuilder, fDialect DialectEncoder) *SqlBuilder {
for _, column := range o.columns {
pBuilder.Add(column.Name).Add(", ")
}
return pBuilder.Truncate(2)
}
// Adds columns onto a sql builder in the form
// :Name, :Name,... or ?, ?... // TODO
func (o *ModelMetadata) ColumnsBindList(pBuilder *SqlBuilder, fDialect DialectEncoder) *SqlBuilder {
for i := 0; i < len(o.columns); i++ {
pBuilder.Add("?, ")
}
return pBuilder.Truncate(2)
}
// sqlite seems to ignore incorrect set pk bindings when updating
// TODO need to ignore primary keys columns
func (o *ModelMetadata) NonKeyListEqualsNonKeyBindList(pBuilder *SqlBuilder, fDialect DialectEncoder) *SqlBuilder {
for _, column := range o.columnsByFieldName {
pBuilder.Add(column.Name).Add(" = ?, ")
} // TODO the assumption is there is always a column
return pBuilder.Truncate(2)
}
// TODO
func (o *ModelMetadata) ColumnsListEqualsColumnsBindList(pBuilder *SqlBuilder, fDialect DialectEncoder) *SqlBuilder {
for _, column := range o.columns {
pBuilder.Add(column.Name).Add(" = ? AND ")
}
return pBuilder.Truncate(5)
}
// TODO
func (o *ModelMetadata) KeyListEqualsKeyBindList(pBuilder *SqlBuilder, fDialect DialectEncoder) *SqlBuilder {
for _, key := range o.keysByFieldName {
pBuilder.Add(key.Name).Add(" = ? AND ")
}
return pBuilder.Truncate(5)
}
// TODO
func (o *ModelMetadata) ColumnListWithConstraints(pBuilder *SqlBuilder, fDialect DialectEncoder) *SqlBuilder {
if len(o.keysByFieldName) == 1 {
for _, key := range o.keysByIndex {
if key.Kind == reflect.Int64 {
pBuilder.Add(key.Name)
pBuilder.Add(" INTEGER NOT NULL PRIMARY KEY")
if key.AutoIncrement {
pBuilder.Add(" AUTOINCREMENT")
}
pBuilder.Add(", ")
} else {
key.BuildKeySchema(pBuilder).Add(", ")
} // TODO a better way
// TODO handle other types and compound keys and different dialects
}
} else {
for _, key := range o.keysByFieldName {
key.BuildColumnSchema(pBuilder).Add(", ")
}
}
for _, column := range o.columnsByFieldName {
column.BuildColumnSchema(pBuilder).Add(", ")
}
return pBuilder.Truncate(2)
}
// TODO
type Column struct {
Identifier string
Name string
AutoIncrement bool
Unique bool
Nilable bool
Insertable bool
Updatable bool
Length uint
Precision uint
Scale uint
Kind reflect.Kind
}
// TODO
func (o Column) BuildColumnSchema(pBuilder *SqlBuilder) *SqlBuilder {
pBuilder.Add(o.Name).Add(o.ToSqlType())
o.unique(pBuilder)
o.nilable(pBuilder)
return pBuilder
}
// TODO
func (o Column) BuildKeySchema(pBuilder *SqlBuilder) *SqlBuilder {
pBuilder.Add(o.Name).Add(o.ToSqlType()).Add(" NOT NULL PRIMARY KEY")
return pBuilder
}
func (o Column) unique(pBuilder *SqlBuilder) {
if o.Unique {
pBuilder.Add(" UNIQUE")
}
}
func (o Column) nilable(pBuilder *SqlBuilder) {
if !o.Nilable {
pBuilder.Add(" NOT NULL")
}
}
// TODO
func (o Column) ToSqlType() string {
switch o.Kind {
case reflect.Ptr:
return " crash" // TODO sort out types
case reflect.Bool:
return " BOOLEAN"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return " INTEGER"
case reflect.Float64, reflect.Float32:
return " FLOAT"
case reflect.Slice:
return " BLOB"
case OpalTime:
return " DATETIME"
}
// switch val.Name() {
// case "Int64":
// return "INTEGER"
// case "Float64":
// return "REAL"
// case "Bool":
// return "INTEGER"
// case "RawBytes": // TODO support properly
// return "BLOB"
// case "Time":
// return "DATETIME"
// }
return fmt.Sprintf(" VARCHAR(%d)", o.Length)
}
// TODO
type Table struct {
Name string
Key []string
}