-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
schema_options.go
142 lines (108 loc) · 2.98 KB
/
schema_options.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
package rel
// TableOption interface.
// Available options are: Comment, Options.
type TableOption interface {
applyTable(table *Table)
}
func applyTableOptions(table *Table, options []TableOption) {
for i := range options {
options[i].applyTable(table)
}
}
// ColumnOption interface.
// Available options are: Nil, Unsigned, Limit, Precision, Scale, Default, Comment, Options.
type ColumnOption interface {
applyColumn(column *Column)
}
func applyColumnOptions(column *Column, options []ColumnOption) {
for i := range options {
options[i].applyColumn(column)
}
}
// KeyOption interface.
// Available options are: Comment, Options.
type KeyOption interface {
applyKey(key *Key)
}
func applyKeyOptions(key *Key, options []KeyOption) {
for i := range options {
options[i].applyKey(key)
}
}
// Primary set column as primary.
type Primary bool
func (r Primary) applyColumn(column *Column) {
column.Primary = bool(r)
}
// Unique set column as unique.
type Unique bool
func (r Unique) applyColumn(column *Column) {
column.Unique = bool(r)
}
func (r Unique) applyIndex(index *Index) {
index.Unique = bool(r)
}
// Required disallows nil values in the column.
type Required bool
func (r Required) applyColumn(column *Column) {
column.Required = bool(r)
}
// Unsigned sets integer column to be unsigned.
type Unsigned bool
func (u Unsigned) applyColumn(column *Column) {
column.Unsigned = bool(u)
}
// Precision defines the precision for the decimal fields, representing the total number of digits in the number.
type Precision int
func (p Precision) applyColumn(column *Column) {
column.Precision = int(p)
}
// Scale Defines the scale for the decimal fields, representing the number of digits after the decimal point.
type Scale int
func (s Scale) applyColumn(column *Column) {
column.Scale = int(s)
}
type defaultValue struct {
value any
}
func (d defaultValue) applyColumn(column *Column) {
column.Default = d.value
}
// Default allows to set a default value on the column.).
func Default(def any) ColumnOption {
return defaultValue{value: def}
}
// OnDelete option for foreign key.
type OnDelete string
func (od OnDelete) applyKey(key *Key) {
key.Reference.OnDelete = string(od)
}
// OnUpdate option for foreign key.
type OnUpdate string
func (ou OnUpdate) applyKey(key *Key) {
key.Reference.OnUpdate = string(ou)
}
// Options options for table, column and index.
type Options string
func (o Options) applyTable(table *Table) {
table.Options = string(o)
}
func (o Options) applyColumn(column *Column) {
column.Options = string(o)
}
func (o Options) applyIndex(index *Index) {
index.Options = string(o)
}
func (o Options) applyKey(key *Key) {
key.Options = string(o)
}
// Optional option.
// when used with create table, will create table only if it's not exists.
// when used with drop table, will drop table only if it's exists.
type Optional bool
func (o Optional) applyTable(table *Table) {
table.Optional = bool(o)
}
func (o Optional) applyIndex(index *Index) {
index.Optional = bool(o)
}