github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/comment_on_database.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sql 12 13 import ( 14 "context" 15 16 "github.com/cockroachdb/cockroach/pkg/keys" 17 "github.com/cockroachdb/cockroach/pkg/security" 18 "github.com/cockroachdb/cockroach/pkg/sql/privilege" 19 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 20 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 21 ) 22 23 type commentOnDatabaseNode struct { 24 n *tree.CommentOnDatabase 25 dbDesc *sqlbase.DatabaseDescriptor 26 } 27 28 // CommentOnDatabase add comment on a database. 29 // Privileges: CREATE on database. 30 // notes: postgres requires CREATE on the database. 31 func (p *planner) CommentOnDatabase( 32 ctx context.Context, n *tree.CommentOnDatabase, 33 ) (planNode, error) { 34 dbDesc, err := p.ResolveUncachedDatabaseByName(ctx, string(n.Name), true) 35 if err != nil { 36 return nil, err 37 } 38 39 if err := p.CheckPrivilege(ctx, dbDesc, privilege.CREATE); err != nil { 40 return nil, err 41 } 42 43 return &commentOnDatabaseNode{n: n, dbDesc: dbDesc}, nil 44 } 45 46 func (n *commentOnDatabaseNode) startExec(params runParams) error { 47 if n.n.Comment != nil { 48 _, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx( 49 params.ctx, 50 "set-db-comment", 51 params.p.Txn(), 52 sqlbase.InternalExecutorSessionDataOverride{User: security.RootUser}, 53 "UPSERT INTO system.comments VALUES ($1, $2, 0, $3)", 54 keys.DatabaseCommentType, 55 n.dbDesc.ID, 56 *n.n.Comment) 57 if err != nil { 58 return err 59 } 60 } else { 61 _, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx( 62 params.ctx, 63 "delete-db-comment", 64 params.p.Txn(), 65 sqlbase.InternalExecutorSessionDataOverride{User: security.RootUser}, 66 "DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=0", 67 keys.DatabaseCommentType, 68 n.dbDesc.ID) 69 if err != nil { 70 return err 71 } 72 } 73 74 return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord( 75 params.ctx, 76 params.p.txn, 77 EventLogCommentOnDatabase, 78 int32(n.dbDesc.ID), 79 int32(params.extendedEvalCtx.NodeID.SQLInstanceID()), 80 struct { 81 DatabaseName string 82 Statement string 83 User string 84 Comment *string 85 }{ 86 n.n.Name.String(), 87 n.n.String(), 88 params.SessionData().User, 89 n.n.Comment}, 90 ) 91 } 92 93 func (n *commentOnDatabaseNode) Next(runParams) (bool, error) { return false, nil } 94 func (n *commentOnDatabaseNode) Values() tree.Datums { return tree.Datums{} } 95 func (n *commentOnDatabaseNode) Close(context.Context) {}