Skip to content

Commit fcfa8a4

Browse files
orisanokyleconroy
andauthored
feat: add supporting COMMENT ON VIEW (#2249)
* feat: add supporting COMMENT ON VIEW close #2241 * test: Add tests for COMMENT ON VIEW --------- Co-authored-by: Kyle Conroy <kyle@conroy.org>
1 parent 090cb54 commit fcfa8a4

File tree

15 files changed

+152
-0
lines changed

15 files changed

+152
-0
lines changed

internal/endtoend/testdata/comment_on/postgresql/pgx/v4/go/models.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comment_on/postgresql/pgx/v4/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comment_on/postgresql/pgx/v4/query.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ CREATE TABLE foo.bar (
44
baz text NOT NULL
55
);
66

7+
CREATE VIEW foo.bat AS SELECT * FROM foo.bar;
8+
79
CREATE TYPE foo.mood AS ENUM ('sad', 'ok', 'happy');
810

911
COMMENT ON SCHEMA foo IS 'this is the foo schema';
1012
COMMENT ON TYPE foo.mood IS 'this is the mood type';
1113
COMMENT ON TABLE foo.bar IS 'this is the bar table';
1214
COMMENT ON COLUMN foo.bar.baz IS 'this is the baz column';
15+
COMMENT ON VIEW foo.bat IS 'this is the bat view ';
1316

1417
-- name: ListBar :many
1518
SELECT * FROM foo.bar;
19+
20+
-- name: ListBat :many
21+
SELECT * FROM foo.bat;

internal/endtoend/testdata/comment_on/postgresql/pgx/v5/go/models.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comment_on/postgresql/pgx/v5/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comment_on/postgresql/pgx/v5/query.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ CREATE TABLE foo.bar (
44
baz text NOT NULL
55
);
66

7+
CREATE VIEW foo.bat AS SELECT * FROM foo.bar;
8+
79
CREATE TYPE foo.mood AS ENUM ('sad', 'ok', 'happy');
810

911
COMMENT ON SCHEMA foo IS 'this is the foo schema';
1012
COMMENT ON TYPE foo.mood IS 'this is the mood type';
1113
COMMENT ON TABLE foo.bar IS 'this is the bar table';
1214
COMMENT ON COLUMN foo.bar.baz IS 'this is the baz column';
15+
COMMENT ON VIEW foo.bat IS 'this is the bat view ';
1316

1417
-- name: ListBar :many
1518
SELECT * FROM foo.bar;
19+
20+
-- name: ListBat :many
21+
SELECT * FROM foo.bat;

internal/endtoend/testdata/comment_on/postgresql/stdlib/go/models.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comment_on/postgresql/stdlib/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comment_on/postgresql/stdlib/query.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ CREATE TABLE foo.bar (
44
baz text NOT NULL
55
);
66

7+
CREATE VIEW foo.bat AS SELECT * FROM foo.bar;
8+
79
CREATE TYPE foo.mood AS ENUM ('sad', 'ok', 'happy');
810

911
COMMENT ON SCHEMA foo IS 'this is the foo schema';
1012
COMMENT ON TYPE foo.mood IS 'this is the mood type';
1113
COMMENT ON TABLE foo.bar IS 'this is the bar table';
1214
COMMENT ON COLUMN foo.bar.baz IS 'this is the baz column';
15+
COMMENT ON VIEW foo.bat IS 'this is the bat view ';
1316

1417
-- name: ListBar :many
1518
SELECT * FROM foo.bar;
19+
20+
-- name: ListBat :many
21+
SELECT * FROM foo.bat;

internal/engine/postgresql/parse.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,16 @@ func translate(node *nodes.Node) (ast.Node, error) {
379379
Comment: makeString(n.Comment),
380380
}, nil
381381

382+
case nodes.ObjectType_OBJECT_VIEW:
383+
rel, err := parseRelation(n.Object)
384+
if err != nil {
385+
return nil, fmt.Errorf("COMMENT ON VIEW: %w", err)
386+
}
387+
return &ast.CommentOnViewStmt{
388+
View: rel.TableName(),
389+
Comment: makeString(n.Comment),
390+
}, nil
391+
382392
}
383393
return nil, errSkip
384394

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ast
2+
3+
type CommentOnViewStmt struct {
4+
View *TableName
5+
Comment *string
6+
}
7+
8+
func (n *CommentOnViewStmt) Pos() int {
9+
return 0
10+
}

internal/sql/astutils/rewrite.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
164164
case *ast.CommentOnTypeStmt:
165165
a.apply(n, "Type", nil, n.Type)
166166

167+
case *ast.CommentOnViewStmt:
168+
a.apply(n, "View", nil, n.View)
169+
167170
case *ast.CreateTableStmt:
168171
a.apply(n, "Name", nil, n.Name)
169172

internal/sql/astutils/walk.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ func Walk(f Visitor, node ast.Node) {
7777
Walk(f, n.Type)
7878
}
7979

80+
case *ast.CommentOnViewStmt:
81+
if n.View != nil {
82+
Walk(f, n.View)
83+
}
84+
8085
case *ast.CompositeTypeStmt:
8186
if n.TypeName != nil {
8287
Walk(f, n.TypeName)

internal/sql/catalog/catalog.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
7676
case *ast.CommentOnTypeStmt:
7777
err = c.commentOnType(n)
7878

79+
case *ast.CommentOnViewStmt:
80+
err = c.commentOnView(n)
81+
7982
case *ast.CompositeTypeStmt:
8083
err = c.createCompositeType(n)
8184

internal/sql/catalog/comment_on.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,16 @@ func (c *Catalog) commentOnType(stmt *ast.CommentOnTypeStmt) error {
6161
}
6262
return nil
6363
}
64+
65+
func (c *Catalog) commentOnView(stmt *ast.CommentOnViewStmt) error {
66+
_, t, err := c.getTable(stmt.View)
67+
if err != nil {
68+
return err
69+
}
70+
if stmt.Comment != nil {
71+
t.Comment = *stmt.Comment
72+
} else {
73+
t.Comment = ""
74+
}
75+
return nil
76+
}

0 commit comments

Comments
 (0)