github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/comment_on_index.go (about) 1 // Copyright 2019 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 commentOnIndexNode struct { 24 n *tree.CommentOnIndex 25 tableDesc *sqlbase.TableDescriptor 26 indexDesc *sqlbase.IndexDescriptor 27 } 28 29 // CommentOnIndex adds a comment on an index. 30 // Privileges: CREATE on table. 31 func (p *planner) CommentOnIndex(ctx context.Context, n *tree.CommentOnIndex) (planNode, error) { 32 tableDesc, indexDesc, err := p.getTableAndIndex(ctx, &n.Index, privilege.CREATE) 33 if err != nil { 34 return nil, err 35 } 36 37 return &commentOnIndexNode{n: n, tableDesc: tableDesc.TableDesc(), indexDesc: indexDesc}, nil 38 } 39 40 func (n *commentOnIndexNode) startExec(params runParams) error { 41 if n.n.Comment != nil { 42 err := params.p.upsertIndexComment( 43 params.ctx, 44 n.tableDesc.ID, 45 n.indexDesc.ID, 46 *n.n.Comment) 47 if err != nil { 48 return err 49 } 50 } else { 51 err := params.p.removeIndexComment(params.ctx, n.tableDesc.ID, n.indexDesc.ID) 52 if err != nil { 53 return err 54 } 55 } 56 57 return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord( 58 params.ctx, 59 params.p.txn, 60 EventLogCommentOnIndex, 61 int32(n.tableDesc.ID), 62 int32(params.extendedEvalCtx.NodeID.SQLInstanceID()), 63 struct { 64 TableName string 65 IndexName string 66 Statement string 67 User string 68 Comment *string 69 }{ 70 n.tableDesc.Name, 71 string(n.n.Index.Index), 72 n.n.String(), 73 params.SessionData().User, 74 n.n.Comment}, 75 ) 76 } 77 78 func (p *planner) upsertIndexComment( 79 ctx context.Context, tableID sqlbase.ID, indexID sqlbase.IndexID, comment string, 80 ) error { 81 _, err := p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx( 82 ctx, 83 "set-index-comment", 84 p.Txn(), 85 sqlbase.InternalExecutorSessionDataOverride{User: security.RootUser}, 86 "UPSERT INTO system.comments VALUES ($1, $2, $3, $4)", 87 keys.IndexCommentType, 88 tableID, 89 indexID, 90 comment) 91 92 return err 93 } 94 95 func (p *planner) removeIndexComment( 96 ctx context.Context, tableID sqlbase.ID, indexID sqlbase.IndexID, 97 ) error { 98 _, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.ExecEx( 99 ctx, 100 "delete-index-comment", 101 p.txn, 102 sqlbase.InternalExecutorSessionDataOverride{User: security.RootUser}, 103 "DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=$3", 104 keys.IndexCommentType, 105 tableID, 106 indexID) 107 108 return err 109 } 110 111 func (n *commentOnIndexNode) Next(runParams) (bool, error) { return false, nil } 112 func (n *commentOnIndexNode) Values() tree.Datums { return tree.Datums{} } 113 func (n *commentOnIndexNode) Close(context.Context) {}