forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop.go
147 lines (129 loc) · 4.05 KB
/
drop.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
// Copyright 2015 The Cockroach Authors.
//
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: XisiHuang (cockhuangxh@163.com)
package sql
import (
"fmt"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/structured"
)
// DropTable drops a table.
// Privileges: WRITE on table.
// Notes: postgres allows only the table owner to DROP a table.
// mysql requires the DROP privilege on the table.
func (p *planner) DropTable(n *parser.DropTable) (planNode, error) {
// TODO(XisiHuang): should do truncate and delete descriptor in
// the same txn
for i, tableQualifiedName := range n.Names {
if err := tableQualifiedName.NormalizeTableName(p.session.Database); err != nil {
return nil, err
}
dbDesc, err := p.getDatabaseDesc(tableQualifiedName.Database())
if err != nil {
return nil, err
}
tbKey := tableKey{dbDesc.ID, tableQualifiedName.Table()}
nameKey := tbKey.Key()
gr, err := p.txn.Get(nameKey)
if err != nil {
return nil, err
}
if !gr.Exists() {
if n.IfExists {
// Noop.
continue
}
// Key does not exist, but we want it to: error out.
return nil, fmt.Errorf("table %q does not exist", tbKey.Name())
}
tableDesc := structured.TableDescriptor{}
if err := p.txn.GetProto(gr.ValueBytes(), &tableDesc); err != nil {
return nil, err
}
if err := tableDesc.Validate(); err != nil {
return nil, err
}
if !tableDesc.HasPrivilege(p.user, parser.PrivilegeWrite) {
return nil, fmt.Errorf("user %s does not have %s privilege on table %s",
p.user, parser.PrivilegeWrite, tableDesc.Name)
}
if _, err = p.Truncate(&parser.Truncate{Tables: n.Names[i : i+1]}); err != nil {
return nil, err
}
// Delete table descriptor
descKey := gr.ValueBytes()
b := &client.Batch{}
b.Del(descKey)
b.Del(nameKey)
err = p.txn.Run(b)
if err != nil {
return nil, err
}
}
return &valuesNode{}, nil
}
// DropDatabase drops a database.
// Privileges: WRITE on database.
// Notes: postgres allows only the database owner to DROP a database.
// mysql requires the DROP privileges on the database.
// TODO(XisiHuang): our DROP DATABASE is like the postgres DROP SCHEMA
// (cockroach database == postgres schema). the postgres default of not
// dropping the schema if there are dependent objects is more sensible
// (see the RESTRICT and CASCADE options).
func (p *planner) DropDatabase(n *parser.DropDatabase) (planNode, error) {
if n.Name == "" {
return nil, errEmptyDatabaseName
}
nameKey := structured.MakeNameMetadataKey(structured.RootNamespaceID, string(n.Name))
gr, err := p.txn.Get(nameKey)
if err != nil {
return nil, err
}
if !gr.Exists() {
if n.IfExists {
// Noop.
return &valuesNode{}, nil
}
return nil, fmt.Errorf("database %q does not exist", n.Name)
}
descKey := gr.ValueBytes()
desc := structured.DatabaseDescriptor{}
if err := p.txn.GetProto(descKey, &desc); err != nil {
return nil, err
}
if err := desc.Validate(); err != nil {
return nil, err
}
if !desc.HasPrivilege(p.user, parser.PrivilegeWrite) {
return nil, fmt.Errorf("user %s does not have %s privilege on database %s",
p.user, parser.PrivilegeWrite, desc.Name)
}
tbNames, err := p.getTableNames(&desc)
if err != nil {
return nil, err
}
if _, err := p.DropTable(&parser.DropTable{Names: tbNames}); err != nil {
return nil, err
}
b := &client.Batch{}
b.Del(descKey)
b.Del(nameKey)
if err := p.txn.Run(b); err != nil {
return nil, err
}
return &valuesNode{}, nil
}