-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
key.go
66 lines (55 loc) · 1.26 KB
/
key.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
package rel
// KeyType definition.
type KeyType string
const (
// PrimaryKey KeyType.
PrimaryKey KeyType = "PRIMARY KEY"
// ForeignKey KeyType.
ForeignKey KeyType = "FOREIGN KEY"
// UniqueKey KeyType.
UniqueKey = "UNIQUE"
)
// ForeignKeyReference definition.
type ForeignKeyReference struct {
Table string
Columns []string
OnDelete string
OnUpdate string
}
// Key definition.
type Key struct {
Op SchemaOp
Name string
Type KeyType
Columns []string
Rename string
Reference ForeignKeyReference
Options string
}
func (Key) internalTableDefinition() {}
func createKeys(columns []string, typ KeyType, options []KeyOption) Key {
key := Key{
Op: SchemaCreate,
Columns: columns,
Type: typ,
}
applyKeyOptions(&key, options)
return key
}
func createPrimaryKeys(columns []string, options []KeyOption) Key {
return createKeys(columns, PrimaryKey, options)
}
func createForeignKey(column string, refTable string, refColumn string, options []KeyOption) Key {
key := Key{
Op: SchemaCreate,
Type: ForeignKey,
Columns: []string{column},
Reference: ForeignKeyReference{
Table: refTable,
Columns: []string{refColumn},
},
}
applyKeyOptions(&key, options)
return key
}
// TODO: Rename and Drop, PR welcomed.