-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_base.go
executable file
·99 lines (83 loc) · 2.51 KB
/
model_base.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
package model
import "fmt"
// Action CRUD actions
type Action int32
var (
// Create action when record is created
Create = Action(0)
// RetrieveOne action when a record is retrieved from db
RetrieveOne = Action(1)
// RetrieveMany action when record(s) are retrieved from db
RetrieveMany = Action(2)
// Update action when record is updated in db
Update = Action(3)
// Delete action when record is deleted in db
Delete = Action(4)
// FetchDDL action when fetching ddl info from db
FetchDDL = Action(5)
tables map[string]*TableInfo
)
func init() {
tables = make(map[string]*TableInfo)
tables["migration_schema"] = migration_schemaTableInfo
tables["sample"] = sampleTableInfo
}
// String describe the action
func (i Action) String() string {
switch i {
case Create:
return "Create"
case RetrieveOne:
return "RetrieveOne"
case RetrieveMany:
return "RetrieveMany"
case Update:
return "Update"
case Delete:
return "Delete"
case FetchDDL:
return "FetchDDL"
default:
return fmt.Sprintf("unknown action: %d", int(i))
}
}
// Model interface methods for database structs generated
type Model interface {
TableName() string
BeforeSave() error
Prepare()
Validate(action Action) error
TableInfo() *TableInfo
}
// TableInfo describes a table in the database
type TableInfo struct {
Name string `json:"name"`
Columns []*ColumnInfo `json:"columns"`
}
// ColumnInfo describes a column in the database table
type ColumnInfo struct {
Index int `json:"index"`
GoFieldName string `json:"go_field_name"`
GoFieldType string `json:"go_field_type"`
JSONFieldName string `json:"json_field_name"`
ProtobufFieldName string `json:"protobuf_field_name"`
ProtobufType string `json:"protobuf_field_type"`
ProtobufPos int `json:"protobuf_field_pos"`
Comment string `json:"comment"`
Notes string `json:"notes"`
Name string `json:"name"`
Nullable bool `json:"is_nullable"`
DatabaseTypeName string `json:"database_type_name"`
DatabaseTypePretty string `json:"database_type_pretty"`
IsPrimaryKey bool `json:"is_primary_key"`
IsAutoIncrement bool `json:"is_auto_increment"`
IsArray bool `json:"is_array"`
ColumnType string `json:"column_type"`
ColumnLength int64 `json:"column_length"`
DefaultValue string `json:"default_value"`
}
// GetTableInfo retrieve TableInfo for a table
func GetTableInfo(name string) (*TableInfo, bool) {
val, ok := tables[name]
return val, ok
}