-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
163 lines (143 loc) · 4.19 KB
/
mysql.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
package generator
import (
"bytes"
"fmt"
"strconv"
"strings"
"go.f110.dev/protoc-ddl/internal/schema"
)
const (
MySQLGeneratorVersion = "v0.1"
)
var (
DataTypeMap = map[string]string{
"TYPE_FLOAT": "FLOAT",
"TYPE_DOUBLE": "DOUBLE",
"TYPE_INT32": "INTEGER",
"TYPE_INT64": "BIGINT",
"TYPE_UINT32": "INTEGER UNSIGNED",
"TYPE_UINT64": "BIGINT UNSIGNED",
"TYPE_SINT32": "INTEGER",
"TYPE_SINT64": "BIGINT",
"TYPE_FIXED32": "INTEGER UNSIGNED",
"TYPE_FIXED64": "BIGINT UNSIGNED",
"TYPE_SFIXED32": "INTEGER",
"TYPE_SFIXED64": "BIGINT",
"TYPE_BOOL": "TINYINT(1)",
"TYPE_BYTES": "BLOB",
"TYPE_STRING": "VARCHAR",
schema.TimestampType: "DATETIME",
}
)
type MySQLGenerator struct{}
func (m MySQLGenerator) Generate(writer *bytes.Buffer, tables *schema.Messages) {
writer.WriteString(fmt.Sprintf("-- Generated by MySQL Generator (%s)\n\n", MySQLGeneratorVersion))
writer.WriteString("SET foreign_key_checks=0;\n\n")
writer.WriteString("")
tables.Each(func(table *schema.Message) {
writer.WriteString(fmt.Sprintf("DROP TABLE IF EXISTS `%s`;\n", table.TableName))
writer.WriteString(fmt.Sprintf("CREATE TABLE `%s` (\n", table.TableName))
colLine := make([]string, 0)
colLine = append(colLine, m.generateColumnDefinition(table)...)
colLine = append(colLine, m.generateIndexDefinition(table)...)
if len(table.PrimaryKeys) > 0 {
primaryKeys := make([]string, 0, len(table.PrimaryKeys))
for _, p := range table.PrimaryKeys {
primaryKeys = append(primaryKeys, fmt.Sprintf("`%s`", p.Name))
}
colLine = append(colLine, fmt.Sprintf("\tPRIMARY KEY(%s)", strings.Join(primaryKeys, ",")))
}
writer.WriteString(strings.Join(colLine, ",\n"))
writer.WriteString("\n")
engine := "InnoDB"
if table.Engine != "" {
engine = table.Engine
}
writer.WriteString(fmt.Sprintf(") Engine=%s;\n\n", engine))
})
writer.WriteString("SET foreign_key_checks=1;\n")
}
func (m MySQLGenerator) generateColumnDefinition(table *schema.Message) []string {
colLine := make([]string, 0)
table.Fields.Each(func(col *schema.Field) {
nullConstraint := "NOT NULL"
if col.Null {
nullConstraint = "NULL"
}
columnOpt := ""
if col.Sequence {
columnOpt += " AUTO_INCREMENT"
}
if col.Default != "" {
switch col.Type {
case "TYPE_INT32", "TYPE_INT64", "TYPE_UINT32", "TYPE_UINT64", "TYPE_SINT32", "TYPE_SINT64":
columnOpt += fmt.Sprintf(" DEFAULT %s", col.Default)
default:
columnOpt += fmt.Sprintf(" DEFAULT \"%s\"", col.Default)
}
}
colLine = append(colLine, fmt.Sprintf("\t`%s` %s %s%s", col.Name, m.columnType(col), nullConstraint, columnOpt))
})
return colLine
}
func (MySQLGenerator) generateIndexDefinition(table *schema.Message) []string {
colLine := make([]string, 0)
for _, i := range table.Indexes {
idxName := i.Name
if idxName == "" {
colNames := make([]string, 0, i.Columns.Len())
i.Columns.Each(func(f *schema.Field) {
colNames = append(colNames, f.Name)
})
idxName = "idx_" + strings.Join(colNames, "_")
}
cols := make([]string, 0, i.Columns.Len())
i.Columns.Each(func(f *schema.Field) {
cols = append(cols, fmt.Sprintf("`%s`", f.Name))
})
indexType := "INDEX"
if i.Unique {
indexType = "UNIQUE"
}
colLine = append(colLine, fmt.Sprintf("\t%s `%s` (%s)", indexType, idxName, strings.Join(cols, ", ")))
}
return colLine
}
func (MySQLGenerator) columnType(col *schema.Field) string {
columnType := ""
if t, ok := DataTypeMap[col.Type]; ok {
switch col.Type {
case "TYPE_STRING":
switch col.OptionalType {
case "text":
columnType = "TEXT"
default:
if col.Size == 0 {
col.Size = 255
}
columnType = t + "(" + strconv.Itoa(col.Size) + ")"
}
case "TYPE_BYTES":
if col.Size > 0 && col.Size < 255 {
columnType = fmt.Sprintf("VARBINARY(%d)", col.Size)
} else {
switch col.OptionalType {
case "tiny":
columnType = "TINYBLOB"
case "blob":
columnType = "BLOB"
case "medium":
columnType = "MEDIUMBLOB"
case "long":
columnType = "LONGBLOB"
default:
columnType = t
}
}
default:
columnType = t
}
return columnType
}
return ""
}