-
Notifications
You must be signed in to change notification settings - Fork 12
/
column.go
101 lines (82 loc) · 1.84 KB
/
column.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
package ddlmaker
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/kayac/ddl-maker/dialect"
)
// column is mapping struct field value.
type column struct {
name string
typeName string
tag string
dialect dialect.Dialect
}
func newColumn(name, typeName, tag string, d dialect.Dialect) column {
return column{
name: name,
typeName: typeName,
tag: tag,
dialect: d,
}
}
func (c column) size() (uint64, error) {
specs := c.specs()
if specs["size"] == "" {
return 0, nil
}
return strconv.ParseUint(specs["size"], 10, 64)
}
func (c column) specs() map[string]string {
elems := strings.Split(c.tag, ",")
specs := make(map[string]string, len(elems))
for _, elem := range elems {
ss := strings.Split(elem, "=")
switch len(ss) {
case 1:
specs[ss[0]] = ""
case 2:
specs[ss[0]] = ss[1]
}
}
return specs
}
func (c column) attribute() string {
var attributes []string
specs := c.specs()
if _, ok := specs["null"]; ok {
attributes = append(attributes, "NULL")
} else {
attributes = append(attributes, "NOT NULL")
}
if defaultVal, ok := specs["default"]; ok {
attributes = append(attributes, "DEFAULT")
attributes = append(attributes, defaultVal)
}
if _, ok := specs["auto"]; ok {
attributes = append(attributes, c.dialect.AutoIncrement())
}
return strings.Join(attributes, " ")
}
func (c column) Name() string {
return c.name
}
// ToSQL is convert struct value to sql.
func (c column) ToSQL() string {
var columnType string
specs := c.specs()
if typeName, ok := specs["type"]; ok {
columnType = typeName
} else {
columnType = c.typeName
}
name := c.dialect.Quote(c.name)
size, err := c.size()
if err != nil {
log.Fatalf("error size parse error %v", err)
}
sql := c.dialect.ToSQL(columnType, size)
attribute := c.attribute()
return fmt.Sprintf("%s %s %s", name, sql, attribute)
}