/
table.go
84 lines (63 loc) · 1.73 KB
/
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
package generator
import (
"fmt"
"strings"
"github.com/tukdesk/tuk-gen/meta"
"github.com/tukdesk/tuk-gen/sql/db"
)
type Lines []string
func (this Lines) MaxIdx() int {
return len(this) - 1
}
type Table struct {
*meta.Object
opt Option
}
func (this *Table) DefLines() Lines {
engine := this.Object.Engine
lines := make(Lines, 0, len(this.PrimaryKey)+len(this.Fields)+len(this.Indexes)) // primay key + fields + indexes
pkCols := make([]string, 0, len(this.PrimaryKey))
switch engine {
case db.MYSQL:
for _, pk := range this.PrimaryKey {
lines = append(lines, ColumnDefForMySQL(this.opt, pk))
pkCols = append(pkCols, pk.Column)
}
for _, f := range this.Fields {
lines = append(lines, ColumnDefForMySQL(this.opt, f))
}
for _, i := range this.Indexes {
lines = append(lines, IndexDefForMySQL(i))
}
case db.POSTGRESQL:
for _, pk := range this.PrimaryKey {
lines = append(lines, ColumnDefForPostgres(this.opt, pk))
pkCols = append(pkCols, pk.Column)
}
for _, f := range this.Fields {
lines = append(lines, ColumnDefForPostgres(this.opt, f))
}
}
lines = append(lines, fmt.Sprintf("PRIMARY KEY (%s)", strings.Join(pkCols, ", ")))
return lines
}
func (this *Table) ExtraLines() []string {
lines := make([]string, 0)
engine := this.Object.Engine
switch engine {
case db.MYSQL:
lines = append(lines,
"ENGINE = InnoDB",
"DEFAULT CHARSET utf8mb4",
)
if partitionLines := PartitionLinesForMySQL(this.Partition); len(partitionLines) > 0 {
lines = append(lines, "PARTITION BY")
lines = append(lines, partitionLines...)
}
case db.POSTGRESQL:
for _, i := range this.Object.Indexes {
lines = append(lines, IndexDefForPostgres(this.Object.TableIdentifier(), i))
}
}
return lines
}