Skip to content

feat: add supporting COMMENT ON VIEW #2249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ CREATE TABLE foo.bar (
baz text NOT NULL
);

CREATE VIEW foo.bat AS SELECT * FROM foo.bar;

CREATE TYPE foo.mood AS ENUM ('sad', 'ok', 'happy');

COMMENT ON SCHEMA foo IS 'this is the foo schema';
COMMENT ON TYPE foo.mood IS 'this is the mood type';
COMMENT ON TABLE foo.bar IS 'this is the bar table';
COMMENT ON COLUMN foo.bar.baz IS 'this is the baz column';
COMMENT ON VIEW foo.bat IS 'this is the bat view ';

-- name: ListBar :many
SELECT * FROM foo.bar;

-- name: ListBat :many
SELECT * FROM foo.bat;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ CREATE TABLE foo.bar (
baz text NOT NULL
);

CREATE VIEW foo.bat AS SELECT * FROM foo.bar;

CREATE TYPE foo.mood AS ENUM ('sad', 'ok', 'happy');

COMMENT ON SCHEMA foo IS 'this is the foo schema';
COMMENT ON TYPE foo.mood IS 'this is the mood type';
COMMENT ON TABLE foo.bar IS 'this is the bar table';
COMMENT ON COLUMN foo.bar.baz IS 'this is the baz column';
COMMENT ON VIEW foo.bat IS 'this is the bat view ';

-- name: ListBar :many
SELECT * FROM foo.bar;

-- name: ListBat :many
SELECT * FROM foo.bat;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ CREATE TABLE foo.bar (
baz text NOT NULL
);

CREATE VIEW foo.bat AS SELECT * FROM foo.bar;

CREATE TYPE foo.mood AS ENUM ('sad', 'ok', 'happy');

COMMENT ON SCHEMA foo IS 'this is the foo schema';
COMMENT ON TYPE foo.mood IS 'this is the mood type';
COMMENT ON TABLE foo.bar IS 'this is the bar table';
COMMENT ON COLUMN foo.bar.baz IS 'this is the baz column';
COMMENT ON VIEW foo.bat IS 'this is the bat view ';

-- name: ListBar :many
SELECT * FROM foo.bar;

-- name: ListBat :many
SELECT * FROM foo.bat;
10 changes: 10 additions & 0 deletions internal/engine/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ func translate(node *nodes.Node) (ast.Node, error) {
Comment: makeString(n.Comment),
}, nil

case nodes.ObjectType_OBJECT_VIEW:
rel, err := parseRelation(n.Object)
if err != nil {
return nil, fmt.Errorf("COMMENT ON VIEW: %w", err)
}
return &ast.CommentOnViewStmt{
View: rel.TableName(),
Comment: makeString(n.Comment),
}, nil

}
return nil, errSkip

Expand Down
10 changes: 10 additions & 0 deletions internal/sql/ast/comment_on_view_stmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ast

type CommentOnViewStmt struct {
View *TableName
Comment *string
}

func (n *CommentOnViewStmt) Pos() int {
return 0
}
3 changes: 3 additions & 0 deletions internal/sql/astutils/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
case *ast.CommentOnTypeStmt:
a.apply(n, "Type", nil, n.Type)

case *ast.CommentOnViewStmt:
a.apply(n, "View", nil, n.View)

case *ast.CreateTableStmt:
a.apply(n, "Name", nil, n.Name)

Expand Down
5 changes: 5 additions & 0 deletions internal/sql/astutils/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func Walk(f Visitor, node ast.Node) {
Walk(f, n.Type)
}

case *ast.CommentOnViewStmt:
if n.View != nil {
Walk(f, n.View)
}

case *ast.CompositeTypeStmt:
if n.TypeName != nil {
Walk(f, n.TypeName)
Expand Down
3 changes: 3 additions & 0 deletions internal/sql/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
case *ast.CommentOnTypeStmt:
err = c.commentOnType(n)

case *ast.CommentOnViewStmt:
err = c.commentOnView(n)

case *ast.CompositeTypeStmt:
err = c.createCompositeType(n)

Expand Down
13 changes: 13 additions & 0 deletions internal/sql/catalog/comment_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ func (c *Catalog) commentOnType(stmt *ast.CommentOnTypeStmt) error {
}
return nil
}

func (c *Catalog) commentOnView(stmt *ast.CommentOnViewStmt) error {
_, t, err := c.getTable(stmt.View)
if err != nil {
return err
}
if stmt.Comment != nil {
t.Comment = *stmt.Comment
} else {
t.Comment = ""
}
return nil
}