-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_index.go
127 lines (98 loc) · 2.57 KB
/
create_index.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
package bob
import (
"errors"
"strings"
"github.com/lann/builder"
)
type IndexBuilder builder.Builder
type indexData struct {
Unique bool
Spatial bool
Fulltext bool
Name string
TableName string
Columns []IndexColumn
IfNotExists bool
}
type IndexColumn struct {
Name string
Extras []string
Collate string
}
func init() {
builder.Register(IndexBuilder{}, indexData{})
}
func (i IndexBuilder) Unique() IndexBuilder {
return builder.Set(i, "Unique", true).(IndexBuilder)
}
func (i IndexBuilder) Spatial() IndexBuilder {
return builder.Set(i, "Spatial", true).(IndexBuilder)
}
func (i IndexBuilder) Fulltext() IndexBuilder {
return builder.Set(i, "Fulltext", true).(IndexBuilder)
}
func (i IndexBuilder) name(name string) IndexBuilder {
return builder.Set(i, "Name", name).(IndexBuilder)
}
func (i IndexBuilder) ifNotExists() IndexBuilder {
return builder.Set(i, "IfNotExists", true).(IndexBuilder)
}
func (i IndexBuilder) On(table string) IndexBuilder {
return builder.Set(i, "TableName", table).(IndexBuilder)
}
func (i IndexBuilder) Columns(column IndexColumn) IndexBuilder {
return builder.Append(i, "Columns", column).(IndexBuilder)
}
func (i IndexBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(i).(indexData)
return data.ToSql()
}
func (i *indexData) ToSql() (sqlStr string, args []interface{}, err error) {
if i.Name == "" {
err = errors.New("index name is required on create index statement")
return
}
if i.TableName == "" {
err = errors.New("a table name must be specified on create index statement")
return
}
if len(i.Columns) == 0 {
err = errors.New("should at least specify one column for create index statement")
return
}
var sql strings.Builder
sql.WriteString("CREATE ")
if i.Unique {
sql.WriteString("UNIQUE ")
}
if i.Fulltext {
sql.WriteString("FULLTEXT ")
}
if i.Spatial {
sql.WriteString("SPATIAL ")
}
sql.WriteString("INDEX ")
if i.IfNotExists {
sql.WriteString("IF NOT EXISTS ")
}
sql.WriteString(i.Name + " ")
sql.WriteString("ON ")
sql.WriteString(i.TableName + " ")
var columns []string
for _, column := range i.Columns {
var colBuilder strings.Builder
colBuilder.WriteString(column.Name)
if column.Collate != "" {
colBuilder.WriteString(" COLLATE " + column.Collate)
}
if len(column.Extras) > 0 {
colBuilder.WriteString(" " + strings.Join(column.Extras, " "))
}
columns = append(columns, colBuilder.String())
}
sql.WriteString("(")
sql.WriteString(strings.Join(columns, ", "))
sql.WriteString(");")
sqlStr = sql.String()
return
}