-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathddl.go
More file actions
221 lines (199 loc) · 5.78 KB
/
ddl.go
File metadata and controls
221 lines (199 loc) · 5.78 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package proxy
import (
"fmt"
"strings"
"router"
"github.com/xelabs/go-mysqlstack/driver"
"github.com/xelabs/go-mysqlstack/sqldb"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
)
var (
supportEngines = []string{
"innodb",
"tokudb",
}
)
// CheckCreateTable used to check the CRERATE TABLE statement.
func CheckCreateTable(ddl *sqlparser.DDL) error {
shardKey := ddl.PartitionName
table := ddl.Table.Name.String()
if "dual" == table {
return fmt.Errorf("spanner.ddl.check.create.table[%s].error:not support", table)
}
// shard key and UNIQUE/PRIMARY KEY constraint check.
if shardKey != "" {
shardKeyOK := false
constraintCheckOK := true
// shardKey check and constraint check in column definition
for _, col := range ddl.TableSpec.Columns {
colName := col.Name.String()
if colName == shardKey {
shardKeyOK = true
} else {
switch col.Type.KeyOpt {
case sqlparser.ColKeyUnique, sqlparser.ColKeyUniqueKey, sqlparser.ColKeyPrimary:
constraintCheckOK = false
}
}
}
if !shardKeyOK {
return fmt.Errorf("Sharding Key column '%s' doesn't exist in table", shardKey)
}
if !constraintCheckOK {
return fmt.Errorf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
}
// constraint check in index definition
for _, index := range ddl.TableSpec.Indexes {
constraintCheckOK = false
info := index.Info
if info.Unique || info.Primary {
for _, colIdx := range index.Columns {
colName := colIdx.Column.String()
if colName == shardKey {
constraintCheckOK = true
break
}
}
if !constraintCheckOK {
return fmt.Errorf("The unique/primary constraint should be only defined on the sharding key column[%s]", shardKey)
}
}
}
}
check := false
engine := ddl.TableSpec.Options.Engine
for _, eng := range supportEngines {
if eng == strings.ToLower(engine) {
check = true
break
}
}
// Change the storage engine to InnoDB.
if !check {
ddl.TableSpec.Options.Engine = "InnoDB"
}
return nil
}
func checkDatabaseAndTable(database string, table string, router *router.Router) error {
tblList := router.Tables()
tables, ok := tblList[database]
if !ok {
return sqldb.NewSQLError(sqldb.ER_BAD_DB_ERROR, "", database)
}
found := false
for _, t := range tables {
if t == table {
found = true
break
}
}
if !found {
return sqldb.NewSQLError(sqldb.ER_NO_SUCH_TABLE, "", table)
}
return nil
}
// handleDDL used to handle the DDL command.
// Here we need to deal with database.table grammar.
// Supports:
// 1. CREATE/DROP DATABASE
// 2. CREATE/DROP TABLE ... PARTITION BY HASH(shardkey)
// 3. CREATE/DROP INDEX ON TABLE(columns...)
// 4. ALTER TABLE .. ENGINE=xx
// 5. ALTER TABLE .. ADD COLUMN (column definition)
// 6. ALTER TABLE .. MODIFY COLUMN column definition
// 7. ALTER TABLE .. DROP COLUMN column
func (spanner *Spanner) handleDDL(session *driver.Session, query string, node *sqlparser.DDL) (*sqltypes.Result, error) {
log := spanner.log
router := spanner.router
scatter := spanner.scatter
ddl := node
database := session.Schema()
// Database operation.
if !ddl.Database.IsEmpty() {
database = ddl.Database.String()
}
// Table operation.
if !ddl.Table.Qualifier.IsEmpty() {
database = ddl.Table.Qualifier.String()
}
// Check the database ACL.
if err := router.DatabaseACL(database); err != nil {
return nil, err
}
switch ddl.Action {
case sqlparser.CreateDBStr:
return spanner.ExecuteScatter(query)
case sqlparser.DropDBStr:
// Execute the ddl.
qr, err := spanner.ExecuteScatter(query)
if err != nil {
return nil, err
}
// Drop database from router.
if err := router.DropDatabase(database); err != nil {
return nil, err
}
return qr, nil
case sqlparser.CreateTableStr:
table := ddl.Table.Name.String()
backends := scatter.Backends()
shardKey := ddl.PartitionName
// Check the table and change the engine.
if err := CheckCreateTable(ddl); err != nil {
log.Error("spanner.ddl.check.create.table[%s].error:%+v", table, err)
return nil, err
}
// Create table.
if err := router.CreateTable(database, table, shardKey, backends); err != nil {
return nil, err
}
r, err := spanner.ExecuteDDL(session, database, sqlparser.String(ddl), node)
if err != nil {
// Try to drop table.
router.DropTable(database, table)
return nil, err
}
return r, nil
case sqlparser.DropTableStr:
// Check the database and table is exists.
table := ddl.Table.Name.String()
if err := checkDatabaseAndTable(database, table, router); err != nil {
return nil, err
}
// Execute.
r, err := spanner.ExecuteDDL(session, database, query, node)
if err != nil {
log.Error("spanner.ddl.execute[%v].error[%+v]", query, err)
}
if err := router.DropTable(database, table); err != nil {
log.Error("spanner.ddl.router.drop.table[%s].error[%+v]", table, err)
}
return r, err
case sqlparser.CreateIndexStr, sqlparser.DropIndexStr,
sqlparser.AlterEngineStr, sqlparser.AlterCharsetStr,
sqlparser.AlterAddColumnStr, sqlparser.AlterDropColumnStr, sqlparser.AlterModifyColumnStr,
sqlparser.TruncateTableStr:
// Check the database and table is exists.
table := ddl.Table.Name.String()
if err := checkDatabaseAndTable(database, table, router); err != nil {
return nil, err
}
// Execute.
r, err := spanner.ExecuteDDL(session, database, query, node)
if err != nil {
log.Error("spanner.ddl[%v].error[%+v]", query, err)
}
return r, err
default:
log.Error("spanner.ddl[%v, %+v].access.denied", query, node)
return nil, sqldb.NewSQLError(sqldb.ER_SPECIFIC_ACCESS_DENIED_ERROR, "Access denied; you don't have the privilege for %v operation", ddl.Action)
}
}