-
Notifications
You must be signed in to change notification settings - Fork 6
/
table.go
77 lines (68 loc) · 3.04 KB
/
table.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
package db
import (
strings2 "github.com/goradd/goradd/pkg/strings"
"github.com/kenshaw/snaker"
)
type Table struct {
// DbKey is the key used to find the database in the global database cluster
DbKey string
// DbName is the name of the database table or object in the database.
DbName string
// LiteralName is the name of the object when describing it to the world. Use the "literalName" option in the comment to override the default. Should be lower case.
LiteralName string
// LiteralPlural is the plural name of the object. Use the "literalPlural" option in the comment to override the default. Should be lower case.
LiteralPlural string
// GoName is the name of the struct when referring to it in go code. Use the "goName" option in the comment to override the default.
GoName string
// GoPlural is the name of a collection of these objects when referring to them in go code. Use the "goPlural" option in the comment to override the default.
GoPlural string
// LcGoName is the same as GoName, but with first letter lower case.
LcGoName string
// Columns is a list of ColumnDescriptions, one for each column in the table.
Columns []*Column
// columnMap is an internal map of the columns by database name of the column
columnMap map[string]*Column
// Indexes are the indexes defined in the database. Unique indexes will result in LoadBy* functions.
Indexes []Index
// Options are key-value pairs of values that can be used to customize how code generation is performed
Options map[string]interface{}
// IsType is true if this is a type table
IsType bool
// Comment is the general comment included in the database
Comment string
// The following items are filled in by the importDescription process
// ManyManyReferences describe the many-to-many references pointing to this table
ManyManyReferences []*ManyManyReference
// ReverseReferences describes the many-to-one references pointing to this table
ReverseReferences []*ReverseReference
// SupportsForeignKeys determines whether the table uses a storage engine that
// supports foreign keys. Some examples: Postgres supports foreign keys across all tables,
// while some MySQL database engines do not support foreign keys, and the engine
// can be assigned on a table-by-table basis.
//
// If the table has foreign key support, the code generator will assume that the
// database will automatically handle updates and deletes of foreign key values.
// If not, the code generator will attempt to update the foreign keys as appropriate.
SupportsForeignKeys bool
}
func (t *Table) PrimaryKeyColumn() *Column {
if len(t.Columns) == 0 {
return nil
}
if !t.Columns[0].IsPk {
return nil
}
return t.Columns[0]
}
func (t *Table) PrimaryKeyGoType() string {
return t.PrimaryKeyColumn().ColumnType.GoType()
}
// GetColumn returns a Column given the database name of a column
func (t *Table) GetColumn(name string) *Column {
return t.columnMap[name]
}
func (t *Table) DefaultHtmlID() string {
defaultID := snaker.CamelToSnake(t.GoName)
defaultID = strings2.SnakeToKebab(defaultID)
return defaultID
}