-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
53 lines (46 loc) · 1.15 KB
/
schema.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
package greasel
// Table represents a db table, either a physical table or a table derived from a query
type Table struct {
// Table name
source string
// Reflection
fields map[string]*Field
intFields map[string]*IntField
}
func NewTable(name string) *Table {
if name == "" {
panic("table name required")
}
return &Table{
source: name,
fields: make(map[string]*Field),
intFields: make(map[string]*IntField),
}
}
// Fields returns all fields on this table
func (t *Table) Fields() []*Field {
// fixme, this is non-deterministic -> ordered field map?
lst := make([]*Field, 0, len(t.fields))
for _, f := range t.fields {
lst = append(lst, f)
}
return lst
}
// Field returns field by name
func (t *Table) Field(name string) *Field {
return t.fields[name]
}
// Field represents a table field of any type
type Field struct {
parent *Table
name string
}
// Create field and register it with parent table
func NewField(parent *Table, name string) *Field {
if _, exists := parent.fields[name]; exists {
panic("field with this name already exists in table")
}
field := &Field{parent: parent, name: name}
parent.fields[name] = field
return field
}