github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/comment_on_column.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/catalog/resolver" 19 "github.com/cockroachdb/cockroach/pkg/sql/privilege" 20 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 21 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 22 ) 23 24 type commentOnColumnNode struct { 25 n *tree.CommentOnColumn 26 tableDesc *ImmutableTableDescriptor 27 } 28 29 // CommentOnColumn add comment on a column. 30 // Privileges: CREATE on table. 31 func (p *planner) CommentOnColumn(ctx context.Context, n *tree.CommentOnColumn) (planNode, error) { 32 var tableName tree.TableName 33 if n.ColumnItem.TableName != nil { 34 tableName = n.ColumnItem.TableName.ToTableName() 35 } 36 tableDesc, err := p.ResolveUncachedTableDescriptor(ctx, &tableName, true, resolver.ResolveRequireTableDesc) 37 if err != nil { 38 return nil, err 39 } 40 41 if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil { 42 return nil, err 43 } 44 45 return &commentOnColumnNode{n: n, tableDesc: tableDesc}, nil 46 } 47 48 func (n *commentOnColumnNode) startExec(params runParams) error { 49 col, _, err := n.tableDesc.FindColumnByName(n.n.ColumnItem.ColumnName) 50 if err != nil { 51 return err 52 } 53 54 if n.n.Comment != nil { 55 _, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx( 56 params.ctx, 57 "set-column-comment", 58 params.p.Txn(), 59 sqlbase.InternalExecutorSessionDataOverride{User: security.RootUser}, 60 "UPSERT INTO system.comments VALUES ($1, $2, $3, $4)", 61 keys.ColumnCommentType, 62 n.tableDesc.ID, 63 col.ID, 64 *n.n.Comment) 65 if err != nil { 66 return err 67 } 68 } else { 69 _, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.ExecEx( 70 params.ctx, 71 "delete-column-comment", 72 params.p.Txn(), 73 sqlbase.InternalExecutorSessionDataOverride{User: security.RootUser}, 74 "DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=$3", 75 keys.ColumnCommentType, 76 n.tableDesc.ID, 77 col.ID) 78 if err != nil { 79 return err 80 } 81 } 82 83 return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord( 84 params.ctx, 85 params.p.txn, 86 EventLogCommentOnColumn, 87 int32(n.tableDesc.ID), 88 int32(params.extendedEvalCtx.NodeID.SQLInstanceID()), 89 struct { 90 TableName string 91 ColumnName string 92 Statement string 93 User string 94 Comment *string 95 }{ 96 n.tableDesc.Name, 97 string(n.n.ColumnItem.ColumnName), 98 n.n.String(), 99 params.SessionData().User, 100 n.n.Comment}, 101 ) 102 } 103 104 func (n *commentOnColumnNode) Next(runParams) (bool, error) { return false, nil } 105 func (n *commentOnColumnNode) Values() tree.Datums { return tree.Datums{} } 106 func (n *commentOnColumnNode) Close(context.Context) {}