Skip to content

Commit 30b7bd8

Browse files
committed
implement modifying table comments
1 parent 5203d53 commit 30b7bd8

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed

memory/table.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var _ sql.TruncateableTable = (*Table)(nil)
7474
var _ sql.AlterableTable = (*Table)(nil)
7575
var _ sql.IndexAlterableTable = (*Table)(nil)
7676
var _ sql.CollationAlterableTable = (*Table)(nil)
77+
var _ sql.CommentAlterableTable = (*Table)(nil)
7778
var _ sql.ForeignKeyTable = (*Table)(nil)
7879
var _ sql.CheckAlterableTable = (*Table)(nil)
7980
var _ sql.RewritableTable = (*Table)(nil)
@@ -2164,6 +2165,13 @@ func (t *Table) ModifyDefaultCollation(ctx *sql.Context, collation sql.Collation
21642165
return nil
21652166
}
21662167

2168+
//ModifyComment implements sql.CommentAlterableTable
2169+
func (t *Table) ModifyComment(ctx *sql.Context, comment string) error {
2170+
data := t.sessionTableData(ctx)
2171+
data.comment = comment
2172+
return nil
2173+
}
2174+
21672175
// Filters implements the sql.FilteredTable interface.
21682176
func (t *Table) Filters() []sql.Expression {
21692177
return t.filters

sql/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ var (
409409
// ErrAlterTableCollationNotSupported is thrown when the table doesn't support ALTER TABLE COLLATE statements
410410
ErrAlterTableCollationNotSupported = errors.NewKind("table %s cannot have its collation altered")
411411

412+
// ErrAlterTableCommentNotSupported is thrown when the table doesn't support ALTER TABLE COMMENT statements
413+
ErrAlterTableCommentNotSupported = errors.NewKind("table %s cannot have its comment altered")
414+
412415
// ErrCollationNotSupportedOnUniqueTextIndex is thrown when a unique index is created on a TEXT column, with no
413416
// prefix length specified, and the collation is case-insensitive or accent-insensitive, meaning we can't
414417
// reliably use a content-hashed field to detect uniqueness.

sql/plan/alter_table.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,67 @@ func (atc *AlterTableCollation) WithChildren(children ...sql.Node) (sql.Node, er
968968
natc.Table = children[0]
969969
return &natc, nil
970970
}
971+
972+
type AlterTableComment struct {
973+
ddlNode
974+
Table sql.Node
975+
Comment string
976+
}
977+
978+
var _ sql.Node = (*AlterTableComment)(nil)
979+
var _ sql.Databaser = (*AlterTableComment)(nil)
980+
981+
func NewAlterTableComment(table *ResolvedTable, comment string) *AlterTableComment {
982+
return &AlterTableComment{
983+
ddlNode: ddlNode{Db: table.SqlDatabase},
984+
Table: table,
985+
Comment: comment,
986+
}
987+
}
988+
989+
// WithDatabase implements the interface sql.Databaser
990+
func (atc *AlterTableComment) WithDatabase(db sql.Database) (sql.Node, error) {
991+
natc := *atc
992+
natc.Db = db
993+
return &natc, nil
994+
}
995+
996+
// IsReadOnly implements the interface sql.Node
997+
func (atc *AlterTableComment) IsReadOnly() bool {
998+
return false
999+
}
1000+
1001+
// String implements the interface sql.Node
1002+
func (atc *AlterTableComment) String() string {
1003+
return fmt.Sprintf("alter table %s comment %s", atc.Table.String(), atc.Comment)
1004+
}
1005+
1006+
// DebugString implements the interface sql.Node
1007+
func (atc *AlterTableComment) DebugString() string {
1008+
return atc.String()
1009+
}
1010+
1011+
// Resolved implements the interface sql.Node
1012+
func (atc *AlterTableComment) Resolved() bool {
1013+
return atc.Table.Resolved() && atc.ddlNode.Resolved()
1014+
}
1015+
1016+
// Schema implements the interface sql.Node
1017+
func (atc *AlterTableComment) Schema() sql.Schema {
1018+
return atc.Table.Schema()
1019+
}
1020+
1021+
// Children implements the interface sql.Node
1022+
func (atc *AlterTableComment) Children() []sql.Node {
1023+
return []sql.Node{atc.Table}
1024+
}
1025+
1026+
// WithChildren implements the interface sql.Node
1027+
func (atc *AlterTableComment) WithChildren(children ...sql.Node) (sql.Node, error) {
1028+
if len(children) != 1 {
1029+
return nil, sql.ErrInvalidChildrenNumber.New(atc, len(children), 1)
1030+
}
1031+
natc := *atc
1032+
natc.Table = children[0]
1033+
return &natc, nil
1034+
}

sql/planbuilder/ddl.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,10 @@ func (b *Builder) buildAlterTableClause(inScope *scope, ddl *ast.DDL) []*scope {
650650
outScopes = append(outScopes, b.buildAlterCollationSpec(tableScope, ddl, rt))
651651
}
652652

653+
if ddl.AlterCommentSpec != nil {
654+
outScopes = append(outScopes, b.buildAlterCommentSpec(tableScope, ddl, rt))
655+
}
656+
653657
if ddl.NotNullSpec != nil {
654658
outScopes = append(outScopes, b.buildAlterNotNull(tableScope, ddl, rt))
655659
}
@@ -1176,6 +1180,12 @@ func (b *Builder) buildAlterCollationSpec(inScope *scope, ddl *ast.DDL, table *p
11761180
return
11771181
}
11781182

1183+
func (b *Builder) buildAlterCommentSpec(inScope *scope, ddl *ast.DDL, table *plan.ResolvedTable) (outScope *scope) {
1184+
outScope = inScope
1185+
outScope.node = plan.NewAlterTableComment(table, ddl.AlterCommentSpec.Comment)
1186+
return
1187+
}
1188+
11791189
func (b *Builder) buildDefaultExpression(inScope *scope, defaultExpr ast.Expr) *sql.ColumnDefaultValue {
11801190
if defaultExpr == nil {
11811191
return nil

sql/rowexec/ddl.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,18 @@ func (b *BaseBuilder) buildAlterTableCollation(ctx *sql.Context, n *plan.AlterTa
11961196
return rowIterWithOkResultWithZeroRowsAffected(), alterable.ModifyDefaultCollation(ctx, n.Collation)
11971197
}
11981198

1199+
func (b *BaseBuilder) buildAlterTableComment(ctx *sql.Context, n *plan.AlterTableComment, row sql.Row) (sql.RowIter, error) {
1200+
tbl, err := getTableFromDatabase(ctx, n.Database(), n.Table)
1201+
if err != nil {
1202+
return nil, err
1203+
}
1204+
alterable, ok := tbl.(sql.CommentAlterableTable)
1205+
if !ok {
1206+
return nil, sql.ErrAlterTableCommentNotSupported.New(tbl.Name())
1207+
}
1208+
return rowIterWithOkResultWithZeroRowsAffected(), alterable.ModifyComment(ctx, n.Comment)
1209+
}
1210+
11991211
func (b *BaseBuilder) buildCreateForeignKey(ctx *sql.Context, n *plan.CreateForeignKey, row sql.Row) (sql.RowIter, error) {
12001212
db, err := n.DbProvider.Database(ctx, n.FkDef.Database)
12011213
if err != nil {

sql/rowexec/node_builder.gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (b *BaseBuilder) buildNodeExecNoAnalyze(ctx *sql.Context, n sql.Node, row s
5050
return b.buildCreateForeignKey(ctx, n, row)
5151
case *plan.AlterTableCollation:
5252
return b.buildAlterTableCollation(ctx, n, row)
53+
case *plan.AlterTableComment:
54+
return b.buildAlterTableComment(ctx, n, row)
5355
case *plan.CreateRole:
5456
return b.buildCreateRole(ctx, n, row)
5557
case *plan.Loop:

sql/tables.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ type CollationAlterableTable interface {
249249
ModifyDefaultCollation(ctx *Context, collation CollationID) error
250250
}
251251

252+
// CommentAlterableTable represents a table that supports altering its comment.
253+
type CommentAlterableTable interface {
254+
Table
255+
// ModifyComment modifies the table's comment
256+
ModifyComment(ctx *Context, comment string) error
257+
}
258+
252259
// PrimaryKeyTable is a table with a primary key.
253260
type PrimaryKeyTable interface {
254261
// PrimaryKeySchema returns this table's PrimaryKeySchema

0 commit comments

Comments
 (0)