forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See cockroachdb#19472 Release note (sql change): support COMMENT ON TABLE syntax
- Loading branch information
Showing
28 changed files
with
357 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2018 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. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/keys" | ||
"github.com/cockroachdb/cockroach/pkg/sql/privilege" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase" | ||
) | ||
|
||
type commentOnTableNode struct { | ||
n *tree.CommentOnTable | ||
tableDesc *MutableTableDescriptor | ||
dbDesc *sqlbase.DatabaseDescriptor | ||
} | ||
|
||
// CommentOnTable add comment on a table. | ||
// Privileges: CREATE on table. | ||
// notes: postgres requires CREATE on the table. | ||
// mysql requires ALTER, CREATE, INSERT on the table. | ||
func (p *planner) CommentOnTable(ctx context.Context, n *tree.CommentOnTable) (planNode, error) { | ||
tableDesc, err := p.ResolveMutableTableDescriptor(ctx, &n.Table, true, requireTableDesc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if tableDesc == nil { | ||
return newZeroNode(nil /* columns */), nil | ||
} | ||
|
||
if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil { | ||
return nil, err | ||
} | ||
|
||
dbDesc, err := getDatabaseDescByID( | ||
ctx, | ||
p.Txn(), | ||
tableDesc.ParentID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &commentOnTableNode{n: n, tableDesc: tableDesc, dbDesc: dbDesc}, nil | ||
} | ||
|
||
func (n *commentOnTableNode) startExec(params runParams) error { | ||
h := makeOidHasher() | ||
oid := h.TableOid(n.dbDesc, tree.PublicSchema, n.tableDesc.TableDesc()) | ||
|
||
if n.n.Comment != nil { | ||
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.Exec( | ||
params.ctx, | ||
"upsert-comment", | ||
params.p.Txn(), | ||
"UPSERT INTO system.comments VALUES ($1, $2, 0, $3)", | ||
keys.TableCommentType, | ||
oid.DInt, | ||
*n.n.Comment) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
_, err := params.p.extendedEvalCtx.ExecCfg.InternalExecutor.Exec( | ||
params.ctx, | ||
"delete-comment", | ||
params.p.Txn(), | ||
"DELETE FROM system.comments WHERE type=$1 AND object_id=$2 AND sub_id=0", | ||
keys.TableCommentType, | ||
oid.DInt) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return MakeEventLogger(params.extendedEvalCtx.ExecCfg).InsertEventRecord( | ||
params.ctx, | ||
params.p.txn, | ||
EventLogCommentOnTable, | ||
int32(n.tableDesc.ID), | ||
int32(params.extendedEvalCtx.NodeID), | ||
struct { | ||
TableName string | ||
Statement string | ||
User string | ||
Comment *string | ||
}{ | ||
n.n.Table.FQString(), | ||
n.n.String(), | ||
params.SessionData().User, | ||
n.n.Comment}, | ||
) | ||
} | ||
|
||
func (n *commentOnTableNode) Next(runParams) (bool, error) { return false, nil } | ||
func (n *commentOnTableNode) Values() tree.Datums { return tree.Datums{} } | ||
func (n *commentOnTableNode) Close(context.Context) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.