-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_table.go
123 lines (98 loc) · 3.08 KB
/
create_table.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
package mysql
import (
"fmt"
"strconv"
filepathz "github.com/kunitsucom/util.go/path/filepath"
slicez "github.com/kunitsucom/util.go/slices"
ddlast "github.com/kunitsucom/ddlctl/pkg/internal/generator"
)
//nolint:cyclop,funlen,gocognit
func fprintCreateTable(buf *string, indent string, stmt *ddlast.CreateTableStmt) {
// source
if stmt.SourceFile != "" {
fprintComment(buf, "", fmt.Sprintf("source: %s:%d", filepathz.Short(stmt.SourceFile), stmt.SourceLine))
}
// comments
for _, comment := range stmt.Comments {
fprintComment(buf, "", comment)
}
if stmt.CreateTable != "" { //nolint:nestif
// CREATE TABLE and Left Parenthesis
*buf += stmt.CreateTable + " (\n"
hasPrimaryKey := len(stmt.PrimaryKey) > 0
hasTableConstraint := len(stmt.Constraints) > 0
// COLUMNS
fprintCreateTableColumn(buf, indent, stmt.Columns, hasPrimaryKey || hasTableConstraint)
// PRIMARY KEY
if len(stmt.PrimaryKey) > 0 {
*buf += indent + "PRIMARY KEY ("
for i, primaryKey := range stmt.PrimaryKey {
*buf += Quotation + primaryKey + Quotation
if lastPrimaryKeyIndex := len(stmt.PrimaryKey) - 1; i != lastPrimaryKeyIndex {
*buf += ", "
}
}
*buf += ")"
if hasTableConstraint {
*buf += ","
}
*buf += "\n"
}
// CONSTRAINT
for i, constraint := range stmt.Constraints {
fprintCreateTableConstraint(buf, indent, constraint)
if lastConstraintIndex := len(stmt.Constraints) - 1; i != lastConstraintIndex {
*buf += ","
}
*buf += "\n"
}
// Right Parenthesis
*buf += ")"
// OPTIONS
for i, option := range stmt.Options {
*buf += "\n"
fprintCreateTableOption(buf, "", option)
if lastOptionIndex := len(stmt.Options) - 1; i != lastOptionIndex {
*buf += ","
}
}
*buf += ";\n"
}
return //nolint:gosimple
}
func fprintCreateTableColumn(buf *string, indent string, columns []*ddlast.CreateTableColumn, tailComma bool) {
columnNameMaxLength := 0
slicez.Each(columns, func(_ int, elem *ddlast.CreateTableColumn) {
if columnLength := len(elem.ColumnName); columnLength > columnNameMaxLength {
columnNameMaxLength = columnLength
}
})
const quotationCharsLength = 2
columnNameFormat := "%-" + strconv.Itoa(quotationCharsLength+columnNameMaxLength) + "s"
for i, column := range columns {
for _, comment := range column.Comments {
fprintComment(buf, indent, comment)
}
*buf += indent + fmt.Sprintf(columnNameFormat, Quotation+column.ColumnName+Quotation) + " " + column.TypeConstraint
if lastColumn := len(columns) - 1; i == lastColumn && !tailComma {
*buf += "\n"
} else {
*buf += ",\n"
}
}
return //nolint:gosimple
}
func fprintCreateTableConstraint(buf *string, indent string, constraint *ddlast.CreateTableConstraint) {
for _, comment := range constraint.Comments {
fprintComment(buf, indent, comment)
}
*buf += indent + constraint.Constraint
return //nolint:gosimple
}
func fprintCreateTableOption(buf *string, indent string, option *ddlast.CreateTableOption) {
for _, comment := range option.Comments {
fprintComment(buf, indent, comment)
}
*buf += indent + option.Option
return //nolint:gosimple
}