This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
173 lines (136 loc) · 4.05 KB
/
types.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
package schema
import (
"fmt"
"strings"
)
const InitActionName = "init"
// ColumnType is the type of column
type ColumnType string
const (
ColNull ColumnType = "null"
ColText ColumnType = "text"
ColInt ColumnType = "int"
ColBlob ColumnType = "blob"
)
func (t ColumnType) String() string {
return string(t)
}
func GetColumnType(col string) (ColumnType, error) {
column, ok := columnTypes[strings.ToLower(col)]
if !ok {
return "", fmt.Errorf("invalid column type: %s", col)
}
return column, nil
}
var columnTypes = map[string]ColumnType{
"null": ColNull,
"text": ColText,
"int": ColInt,
"blob": ColBlob,
}
// AttributeType is the type of attribute
type AttributeType string
const (
AttrPrimaryKey AttributeType = "primary_key"
AttrUnique AttributeType = "unique"
AttrNotNull AttributeType = "not_null"
AttrDefault AttributeType = "default"
AttrMin AttributeType = "min"
AttrMax AttributeType = "max"
AttrMinLength AttributeType = "min_length"
AttrMaxLength AttributeType = "max_length"
)
func (t AttributeType) String() string {
return string(t)
}
func GetAttributeType(attr string) (AttributeType, error) {
attribute, ok := attributeTypes[strings.ToLower(attr)]
if !ok {
return "", fmt.Errorf("invalid attribute type: %s", attr)
}
return attribute, nil
}
// attributeTypes maps the Kuneiform attribute tokens to the schema attribute type
var attributeTypes = map[string]AttributeType{
"primary": AttrPrimaryKey,
"unique": AttrUnique,
"notnull": AttrNotNull,
"default": AttrDefault,
"min": AttrMin,
"max": AttrMax,
"minlen": AttrMinLength,
"maxlen": AttrMaxLength,
}
// IndexType is the type of index
type IndexType string
const (
IdxBtree IndexType = "btree"
IdxUniqueBtree IndexType = "unique_btree"
IdxPrimary IndexType = "primary"
)
func (t IndexType) String() string {
return string(t)
}
// indexTypes maps the Kuneiform index tokens to the schema index type
var indexTypes = map[string]IndexType{
"index": IdxBtree,
"unique": IdxUniqueBtree,
"primary": IdxPrimary,
}
func GetIndexType(idxType string) (IndexType, error) {
index, ok := indexTypes[strings.ToLower(idxType)]
if !ok {
return "", fmt.Errorf("invalid index type: %s", idxType)
}
return index, nil
}
// ForeignKeyActionOn specifies when a foreign key action should occur.
// It can be either "UPDATE" or "DELETE".
type ForeignKeyActionOn string
const (
// ON_UPDATE is used to specify an action should occur when a parent key is updated
ON_UPDATE ForeignKeyActionOn = "UPDATE"
// ON_DELETE is used to specify an action should occur when a parent key is deleted
ON_DELETE ForeignKeyActionOn = "DELETE"
)
// ForeignKeyActionDo specifies what should be done when a foreign key action is triggered.
type ForeignKeyActionDo string
const (
// DO_NO_ACTION does nothing when a parent key is altered
DO_NO_ACTION ForeignKeyActionDo = "NO ACTION"
// DO_RESTRICT prevents the parent key from being altered
DO_RESTRICT ForeignKeyActionDo = "RESTRICT"
// DO_SET_NULL sets the child key(s) to NULL
DO_SET_NULL ForeignKeyActionDo = "SET NULL"
// DO_SET_DEFAULT sets the child key(s) to their default values
DO_SET_DEFAULT ForeignKeyActionDo = "SET DEFAULT"
// DO_CASCADE updates the child key(s) or deletes the records (depending on the action type)
DO_CASCADE ForeignKeyActionDo = "CASCADE"
)
// VisibilityType is the type of visibility
type VisibilityType string
func (t VisibilityType) String() string {
return string(t)
}
const (
VisibilityPublic VisibilityType = "public"
VisibilityPrivate VisibilityType = "private"
)
// MutabilityType is the type of mutability
type MutabilityType string
func (t MutabilityType) String() string {
return string(t)
}
const (
MutabilityUpdate MutabilityType = "update"
MutabilityView MutabilityType = "view"
)
// AuxiliaryType is the type of auxiliary
type AuxiliaryType string
func (t AuxiliaryType) String() string {
return string(t)
}
const (
// AuxiliaryTypeOwner is used to specify that an action caller must be the owner of the database.
AuxiliaryTypeOwner AuxiliaryType = "owner"
)