-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataDescColumn.go
141 lines (130 loc) · 5.44 KB
/
dataDescColumn.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
package db
import (
"fmt"
"github.com/goradd/gengen/pkg/maps"
"github.com/goradd/goradd/pkg/datetime"
. "github.com/goradd/goradd/pkg/orm/query"
"strings"
)
// ColumnDescription describes a database column. Most of the information is either
// gleaned from the structure of the database, or is taken from a file that describes the relationships between
// different record types. Some of the information is filled in after analysis. Some of the information can be
// provided through information embedded in database comments.
type ColumnDescription struct {
// DbName is the name of the column in the database. This is blank if this is a "virtual" table for sql tables like an association or virtual attribute query.
DbName string
// GoName is the name of the column in go code
GoName string
// NativeType is the type of the column as described by the database itself.
NativeType string
// ColumnType is the goradd defined column type
ColumnType GoColumnType
// MaxCharLength is the maximum length of characters to allow in the column if a string type column.
// If the database has the ability to specify this, this will correspond to what is specified.
// In any case, we will generate code to prevent fields from getting bigger than this.
MaxCharLength uint64
// DefaultValue is the default value as specified by the database. We will initialize new ORM objects
// with this value. It will be case to the corresponding GO type.
DefaultValue interface{}
// MaxValue is the maximum value allowed for numeric values. This can be used by UI objects to tell the user what the limits are.
MaxValue interface{}
// MinValue is the minimum value allowed for numeric values. This can be used by UI objects to tell the user what the limits are.
MinValue interface{}
// IsId is true if this column represents a unique identifier generated by the database
IsId bool
// IsPk is true if this is the primary key column. PK's do not necessarily need to be ID columns, and if not, we will need to do our own work to generate unique PKs.
IsPk bool
// IsNullable is true if the column can be given a NULL value
IsNullable bool
// IsIndexed is true if the column's table has a single index on the column, which will generate a LoadArrayBy function.
IsIndexed bool
// IsUnique is true if the column's table has a single unique index on the column.
IsUnique bool
// IsTimestamp is true if the field is a timestamp. Timestamps represent a specific point in world time.
IsTimestamp bool
// IsAutoUpdateTimestamp is true if the database is updating the timestamp. Otherwise we will do it manually.
IsAutoUpdateTimestamp bool
// Comment is the contents of the comment associated with this field
Comment string
// Filled in by analyzer
// Options are the options extracted from the comments string
Options *maps.SliceMap
// ForeignKey is additional information describing a foreign key relationship
ForeignKey *ForeignKeyColumn
// ModelName is a cache for the internal model name of this column.
ModelName string
}
// DefaultConstantName returns the name of the default value constant that will be used to refer to the default value
func (cd *ColumnDescription) DefaultConstantName(tableName string) string {
title := tableName + cd.GoName + "Default"
return title
}
// DefaultValueAsValue returns the default value of the column as a GO value
func (cd *ColumnDescription) DefaultValueAsValue() string {
if cd.DefaultValue == nil {
v := cd.ColumnType.DefaultValue()
if v == "" {
return "nil"
} else {
return v
}
} else if cd.ColumnType == ColTypeDateTime {
if b, _ := cd.DefaultValue.(datetime.DateTime).MarshalText(); b == nil {
return cd.ColumnType.DefaultValue()
} else {
s := string(b[:])
if cd.IsTimestamp {
return fmt.Sprintf("datetime.NewTimestamp(%#v)", s)
} else {
return fmt.Sprintf("datetime.NewDateTime(%#v)", s)
}
}
} else {
return fmt.Sprintf("%#v", cd.DefaultValue)
}
}
// DefaultValueAsConstant returns the default value of the column as a GO constant
func (cd *ColumnDescription) DefaultValueAsConstant() string {
if cd.ColumnType == ColTypeDateTime {
if cd.DefaultValue == nil {
return "datetime.Zero" // pass this to datetime.NewDateTime()
} else {
d := cd.DefaultValue.(datetime.DateTime)
if b, _ := d.MarshalText(); b == nil {
return "datetime.Zero"
} else {
s := string(b[:])
return fmt.Sprintf("%#v", s)
}
}
} else if cd.DefaultValue == nil {
return cd.ColumnType.DefaultValue()
} else {
return fmt.Sprintf("%#v", cd.DefaultValue)
}
}
func (cd *ColumnDescription) JsonKey() string {
return cd.ModelName
}
// IsReference returns true if the column is a foreign key pointing to another table
func (cd *ColumnDescription) IsReference() bool {
return cd.ForeignKey != nil
}
// ReferenceFunction returns the function name that should be used to refer to the object
// that is referred to by a forward reference. It is extracted from the name of foreign key.
func (cd *ColumnDescription) ReferenceFunction(dd *DatabaseDescription) string {
if cd.IsReference() {
if cd.ForeignKey.IsType {
suf := UpperCaseIdentifier(dd.ForeignKeySuffix)
goName := strings.TrimSuffix(cd.GoName, suf)
return goName
} else {
return cd.ForeignKey.GoName
}
}
return ""
}
// ReferenceJsonKey returns the key that will be used for the referenced object in JSON.
func (cd *ColumnDescription) ReferenceJsonKey(dd *DatabaseDescription) string {
return LowerCaseIdentifier(strings.TrimSuffix(cd.DbName, dd.ForeignKeySuffix))
}