/
dbddl.go
237 lines (196 loc) · 6.54 KB
/
dbddl.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"fmt"
"github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)
// CreateDB creates an in memory database that lasts the length of the process only.
type CreateDB struct {
Catalog sql.Catalog
DbName string
IfNotExists bool
Collation sql.CollationID
}
var _ sql.Node = (*CreateDB)(nil)
var _ sql.CollationCoercible = (*CreateDB)(nil)
func (c *CreateDB) Resolved() bool {
return true
}
func (c *CreateDB) IsReadOnly() bool {
return false
}
func (c *CreateDB) String() string {
ifNotExists := ""
if c.IfNotExists {
ifNotExists = " if not exists"
}
return fmt.Sprintf("%s database%s %v", sqlparser.CreateStr, ifNotExists, c.DbName)
}
func (c *CreateDB) Schema() sql.Schema {
return types.OkResultSchema
}
func (c *CreateDB) Children() []sql.Node {
return nil
}
func (c *CreateDB) WithChildren(children ...sql.Node) (sql.Node, error) {
return NillaryWithChildren(c, children...)
}
// CheckPrivileges implements the interface sql.Node.
func (c *CreateDB) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(sql.PrivilegeCheckSubject{}, sql.PrivilegeType_Create))
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*CreateDB) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}
// Database returns the name of the database that will be used.
func (c *CreateDB) Database() string {
return c.DbName
}
func NewCreateDatabase(dbName string, ifNotExists bool, collation sql.CollationID) *CreateDB {
return &CreateDB{
DbName: dbName,
IfNotExists: ifNotExists,
Collation: collation,
}
}
// DropDB removes a databases from the Catalog and updates the active database if it gets removed itself.
type DropDB struct {
Catalog sql.Catalog
DbName string
IfExists bool
// EventScheduler is used to notify EventSchedulerStatus of database deletion,
// so the events of this database in the scheduler will be removed.
EventScheduler sql.EventScheduler
}
var _ sql.Node = (*DropDB)(nil)
var _ sql.CollationCoercible = (*DropDB)(nil)
var _ sql.EventSchedulerStatement = (*DropDB)(nil)
func (d *DropDB) Resolved() bool {
return true
}
func (d *DropDB) IsReadOnly() bool {
return false
}
func (d *DropDB) String() string {
ifExists := ""
if d.IfExists {
ifExists = " if exists"
}
return fmt.Sprintf("%s database%s %v", sqlparser.DropStr, ifExists, d.DbName)
}
func (d *DropDB) Schema() sql.Schema {
return types.OkResultSchema
}
func (d *DropDB) Children() []sql.Node {
return nil
}
func (d *DropDB) WithChildren(children ...sql.Node) (sql.Node, error) {
return NillaryWithChildren(d, children...)
}
// WithEventScheduler is used to drop all events from EventSchedulerStatus for DROP DATABASE.
func (d *DropDB) WithEventScheduler(scheduler sql.EventScheduler) sql.Node {
na := *d
na.EventScheduler = scheduler
return &na
}
// CheckPrivileges implements the interface sql.Node.
func (d *DropDB) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(sql.PrivilegeCheckSubject{}, sql.PrivilegeType_Drop))
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*DropDB) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}
func NewDropDatabase(dbName string, ifExists bool) *DropDB {
return &DropDB{
DbName: dbName,
IfExists: ifExists,
}
}
// AlterDB alters a database from the Catalog.
type AlterDB struct {
Catalog sql.Catalog
dbName string
Collation sql.CollationID
}
var _ sql.Node = (*AlterDB)(nil)
var _ sql.CollationCoercible = (*AlterDB)(nil)
// Resolved implements the interface sql.Node.
func (c *AlterDB) Resolved() bool {
return true
}
func (c *AlterDB) IsReadOnly() bool {
return false
}
// String implements the interface sql.Node.
func (c *AlterDB) String() string {
var dbName string
if len(c.dbName) > 0 {
dbName = fmt.Sprintf(" %s", c.dbName)
}
return fmt.Sprintf("%s database%s collate %s", sqlparser.AlterStr, dbName, c.Collation.Name())
}
// Schema implements the interface sql.Node.
func (c *AlterDB) Schema() sql.Schema {
return types.OkResultSchema
}
// Children implements the interface sql.Node.
func (c *AlterDB) Children() []sql.Node {
return nil
}
// WithChildren implements the interface sql.Node.
func (c *AlterDB) WithChildren(children ...sql.Node) (sql.Node, error) {
return NillaryWithChildren(c, children...)
}
// CheckPrivileges implements the interface sql.Node.
func (c *AlterDB) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
subject := sql.PrivilegeCheckSubject{
Database: c.Database(ctx),
}
return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_Alter))
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*AlterDB) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}
// Database returns the name of the database that will be used.
func (c *AlterDB) Database(ctx *sql.Context) string {
if len(c.dbName) == 0 {
return ctx.GetCurrentDatabase()
}
return c.dbName
}
// NewAlterDatabase returns a new AlterDB.
func NewAlterDatabase(dbName string, collation sql.CollationID) *AlterDB {
return &AlterDB{
dbName: dbName,
Collation: collation,
}
}
// GetDatabaseCollation returns a database's collation. Also handles when a database does not explicitly support collations.
func GetDatabaseCollation(ctx *sql.Context, db sql.Database) sql.CollationID {
collatedDb, ok := db.(sql.CollatedDatabase)
if !ok {
return sql.Collation_Default
}
collation := collatedDb.GetCollation(ctx)
if collation == sql.Collation_Unspecified {
return sql.Collation_Default
}
return collation
}