forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
181 lines (146 loc) · 4.8 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package rel
// TableDefinition interface.
type TableDefinition interface {
internalTableDefinition()
}
// Table definition.
type Table struct {
Op SchemaOp
Name string
Rename string
Definitions []TableDefinition
Optional bool
Options string
}
// Column defines a column with name and type.
func (t *Table) Column(name string, typ ColumnType, options ...ColumnOption) {
t.Definitions = append(t.Definitions, createColumn(name, typ, options))
}
// ID defines a column with name and ID type.
// the resulting database type will depends on database.
func (t *Table) ID(name string, options ...ColumnOption) {
t.Column(name, ID, options...)
}
// Bool defines a column with name and Bool type.
func (t *Table) Bool(name string, options ...ColumnOption) {
t.Column(name, Bool, options...)
}
// Int defines a column with name and Int type.
func (t *Table) Int(name string, options ...ColumnOption) {
t.Column(name, Int, options...)
}
// BigInt defines a column with name and BigInt type.
func (t *Table) BigInt(name string, options ...ColumnOption) {
t.Column(name, BigInt, options...)
}
// Float defines a column with name and Float type.
func (t *Table) Float(name string, options ...ColumnOption) {
t.Column(name, Float, options...)
}
// Decimal defines a column with name and Decimal type.
func (t *Table) Decimal(name string, options ...ColumnOption) {
t.Column(name, Decimal, options...)
}
// String defines a column with name and String type.
func (t *Table) String(name string, options ...ColumnOption) {
t.Column(name, String, options...)
}
// Text defines a column with name and Text type.
func (t *Table) Text(name string, options ...ColumnOption) {
t.Column(name, Text, options...)
}
// Date defines a column with name and Date type.
func (t *Table) Date(name string, options ...ColumnOption) {
t.Column(name, Date, options...)
}
// DateTime defines a column with name and DateTime type.
func (t *Table) DateTime(name string, options ...ColumnOption) {
t.Column(name, DateTime, options...)
}
// Time defines a column with name and Time type.
func (t *Table) Time(name string, options ...ColumnOption) {
t.Column(name, Time, options...)
}
// Timestamp defines a column with name and Timestamp type.
func (t *Table) Timestamp(name string, options ...ColumnOption) {
t.Column(name, Timestamp, options...)
}
// PrimaryKey defines a primary key for table.
func (t *Table) PrimaryKey(column string, options ...KeyOption) {
t.PrimaryKeys([]string{column}, options...)
}
// PrimaryKeys defines composite primary keys for table.
func (t *Table) PrimaryKeys(columns []string, options ...KeyOption) {
t.Definitions = append(t.Definitions, createPrimaryKeys(columns, options))
}
// ForeignKey defines foreign key index.
func (t *Table) ForeignKey(column string, refTable string, refColumn string, options ...KeyOption) {
t.Definitions = append(t.Definitions, createForeignKey(column, refTable, refColumn, options))
}
// Unique defines an unique key for columns.
func (t *Table) Unique(columns []string, options ...KeyOption) {
t.Definitions = append(t.Definitions, createKeys(columns, UniqueKey, options))
}
// Fragment defines anything using sql fragment.
func (t *Table) Fragment(fragment string) {
t.Definitions = append(t.Definitions, Raw(fragment))
}
func (t Table) description() string {
return t.Op.String() + " table " + t.Name
}
func (t Table) internalMigration() {}
// AlterTable Migrator.
type AlterTable struct {
Table
}
// RenameColumn to a new name.
func (at *AlterTable) RenameColumn(name string, newName string, options ...ColumnOption) {
at.Definitions = append(at.Definitions, renameColumn(name, newName, options))
}
// DropColumn from this table.
func (at *AlterTable) DropColumn(name string, options ...ColumnOption) {
at.Definitions = append(at.Definitions, dropColumn(name, options))
}
func createTable(name string, options []TableOption) Table {
table := Table{
Op: SchemaCreate,
Name: name,
}
applyTableOptions(&table, options)
return table
}
func createTableIfNotExists(name string, options []TableOption) Table {
table := createTable(name, options)
table.Optional = true
return table
}
func alterTable(name string, options []TableOption) AlterTable {
table := Table{
Op: SchemaAlter,
Name: name,
}
applyTableOptions(&table, options)
return AlterTable{Table: table}
}
func renameTable(name string, newName string, options []TableOption) Table {
table := Table{
Op: SchemaRename,
Name: name,
Rename: newName,
}
applyTableOptions(&table, options)
return table
}
func dropTable(name string, options []TableOption) Table {
table := Table{
Op: SchemaDrop,
Name: name,
}
applyTableOptions(&table, options)
return table
}
func dropTableIfExists(name string, options []TableOption) Table {
table := dropTable(name, options)
table.Optional = true
return table
}