-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcomment_on_database.go
99 lines (89 loc) · 2.84 KB
/
comment_on_database.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
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
)
type commentOnDatabaseNode struct {
n *tree.CommentOnDatabase
dbDesc catalog.DatabaseDescriptor
}
// CommentOnDatabase add comment on a database.
// Privileges: CREATE on database.
// notes: postgres requires CREATE on the database.
func (p *planner) CommentOnDatabase(
ctx context.Context, n *tree.CommentOnDatabase,
) (planNode, error) {
if err := checkSchemaChangeEnabled(
ctx,
p.ExecCfg(),
"COMMENT ON DATABASE",
); err != nil {
return nil, err
}
dbDesc, err := p.Descriptors().GetImmutableDatabaseByName(ctx, p.txn,
string(n.Name), tree.DatabaseLookupFlags{Required: true})
if err != nil {
return nil, err
}
if err := p.CheckPrivilege(ctx, dbDesc, privilege.CREATE); err != nil {
return nil, err
}
return &commentOnDatabaseNode{n: n, dbDesc: dbDesc}, nil
}
func (n *commentOnDatabaseNode) startExec(params runParams) error {
if n.n.Comment != nil {
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx(
params.ctx,
"set-db-comment",
params.p.Txn(),
sessiondata.InternalExecutorOverride{User: security.RootUserName()},
"UPSERT INTO system.comments VALUES ($1, $2, 0, $3)",
keys.DatabaseCommentType,
n.dbDesc.GetID(),
*n.n.Comment)
if err != nil {
return err
}
} else {
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx(
params.ctx,
"delete-db-comment",
params.p.Txn(),
sessiondata.InternalExecutorOverride{User: security.RootUserName()},
"DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=0",
keys.DatabaseCommentType,
n.dbDesc.GetID())
if err != nil {
return err
}
}
comment := ""
if n.n.Comment != nil {
comment = *n.n.Comment
}
return params.p.logEvent(params.ctx,
n.dbDesc.GetID(),
&eventpb.CommentOnDatabase{
DatabaseName: n.n.Name.String(),
Comment: comment,
NullComment: n.n.Comment == nil,
})
}
func (n *commentOnDatabaseNode) Next(runParams) (bool, error) { return false, nil }
func (n *commentOnDatabaseNode) Values() tree.Datums { return tree.Datums{} }
func (n *commentOnDatabaseNode) Close(context.Context) {}