forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_type.go
96 lines (83 loc) · 2.84 KB
/
column_type.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
package migrate
import (
"fmt"
"reflect"
"strings"
)
const (
UNIQUE int = iota
PRIMARYKEY
AUTOINCREMENT
NULL
NOTNULL
)
// columnType will be injected to migration script
// along with MigrationDriver. `AttrMap` is used to
// defines distinct column's attribute between database
// implementation. e.g. 'AUTOINCREMENT' in sqlite and
// 'AUTO_INCREMENT' in mysql.
type columnType struct {
Driver string
AttrMap map[int]string
}
// defaultMap defines default values for column's attribute
// lookup.
var defaultMap = map[int]string{
UNIQUE: "UNIQUE",
PRIMARYKEY: "PRIMARY KEY",
AUTOINCREMENT: "AUTOINCREMENT",
NULL: "NULL",
NOTNULL: "NOT NULL",
}
// Integer returns column definition for INTEGER typed column.
// Additional attributes may be specified as string or predefined key
// listed in defaultMap.
func (c *columnType) Integer(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s INTEGER %s", colName, c.parseAttr(spec))
}
// String returns column definition for VARCHAR(255) typed column.
func (c *columnType) String(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s VARCHAR(255) %s", colName, c.parseAttr(spec))
}
// Text returns column definition for TEXT typed column.
func (c *columnType) Text(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s TEXT %s", colName, c.parseAttr(spec))
}
// Blob returns column definition for BLOB typed column
func (c *columnType) Blob(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s BLOB %s", colName, c.parseAttr(spec))
}
// Timestamp returns column definition for TIMESTAMP typed column
func (c *columnType) Timestamp(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s TIMESTAMP %s", colName, c.parseAttr(spec))
}
// Bool returns column definition for BOOLEAN typed column
func (c *columnType) Bool(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s BOOLEAN %s", colName, c.parseAttr(spec))
}
// Varchar returns column definition for VARCHAR typed column.
// column's max length is specified as `length`.
func (c *columnType) Varchar(colName string, length int, spec ...interface{}) string {
return fmt.Sprintf("%s VARCHAR(%d) %s", colName, length, c.parseAttr(spec))
}
// attr returns string representation of column attribute specified as key for defaultMap.
func (c *columnType) attr(flag int) string {
if v, ok := c.AttrMap[flag]; ok {
return v
}
return defaultMap[flag]
}
// parseAttr reflects spec value for its type and returns the string
// representation returned by `attr`
func (c *columnType) parseAttr(spec []interface{}) string {
var attrs []string
for _, v := range spec {
switch reflect.ValueOf(v).Kind() {
case reflect.Int:
attrs = append(attrs, c.attr(v.(int)))
case reflect.String:
attrs = append(attrs, v.(string))
}
}
return strings.Join(attrs, " ")
}