-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddl.go
52 lines (46 loc) · 1.24 KB
/
ddl.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
package nosql
import (
"fmt"
"strings"
)
// DDLType enum
type DDLType int
const (
// Create new table
Create DDLType = iota
// Add new columns
Add
// Modify existed columns type
Modify
// Remove existed columns
Remove
)
// GenerateDDL from table defination
func GenerateDDL(table Table, ddlType DDLType) string {
switch ddlType {
case Create:
return GenerateDDLFromDefination(table)
case Modify:
return GenerateAlterModifyDDL(table)
default:
return ""
}
}
// GenerateDDLFromDefination used for generate Table Create DDL
func GenerateDDLFromDefination(table Table) string {
columns := []string{}
for _, column := range table.columns {
columns = append(columns, fmt.Sprintf("%s %s(%d)", column.columnName, column.columnType, column.columnLength))
}
rt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s);", table.tableName, strings.Join(columns, ","))
return rt
}
// GenerateAlterModifyDDL used for modify existed column type
func GenerateAlterModifyDDL(table Table) string {
columns := []string{}
for _, column := range table.columns {
columns = append(columns, fmt.Sprintf("ALTER TABLE %s MODIFY %s %s(%d);", table.tableName, column.columnName, column.columnType, column.columnLength))
}
rt := strings.Join(columns, "")
return rt
}