Skip to content

Commit f4eacef

Browse files
Robert Holtkyleconroy
Robert Holt
andauthored
Add support for SQL Views (#1044)
Co-authored-by: Kyle Conroy <kyle@conroy.org>
1 parent 0028bf5 commit f4eacef

File tree

19 files changed

+382
-13
lines changed

19 files changed

+382
-13
lines changed

internal/compiler/compile.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/kyleconroy/sqlc/internal/multierr"
1515
"github.com/kyleconroy/sqlc/internal/opts"
1616
"github.com/kyleconroy/sqlc/internal/sql/ast"
17-
"github.com/kyleconroy/sqlc/internal/sql/catalog"
1817
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
1918
"github.com/kyleconroy/sqlc/internal/sql/sqlpath"
2019
)
@@ -54,7 +53,7 @@ func enumValueName(value string) string {
5453
}
5554

5655
// end copypasta
57-
func parseCatalog(p Parser, c *catalog.Catalog, schemas []string) error {
56+
func (c *Compiler) parseCatalog(schemas []string) error {
5857
files, err := sqlpath.Glob(schemas)
5958
if err != nil {
6059
return err
@@ -67,13 +66,13 @@ func parseCatalog(p Parser, c *catalog.Catalog, schemas []string) error {
6766
continue
6867
}
6968
contents := migrations.RemoveRollbackStatements(string(blob))
70-
stmts, err := p.Parse(strings.NewReader(contents))
69+
stmts, err := c.parser.Parse(strings.NewReader(contents))
7170
if err != nil {
7271
merr.Add(filename, contents, 0, err)
7372
continue
7473
}
7574
for i := range stmts {
76-
if err := c.Update(stmts[i]); err != nil {
75+
if err := c.catalog.Update(stmts[i], c); err != nil {
7776
merr.Add(filename, contents, stmts[i].Pos(), err)
7877
continue
7978
}

internal/compiler/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (c *Compiler) Catalog() *catalog.Catalog {
4242
}
4343

4444
func (c *Compiler) ParseCatalog(schema []string) error {
45-
return parseCatalog(c.parser, c.catalog, schema)
45+
return c.parseCatalog(schema)
4646
}
4747

4848
func (c *Compiler) ParseQueries(queries []string, o opts.Parser) error {

internal/compiler/output_columns.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,39 @@ package compiler
33
import (
44
"errors"
55
"fmt"
6+
"github.com/kyleconroy/sqlc/internal/sql/catalog"
67

78
"github.com/kyleconroy/sqlc/internal/sql/ast"
89
"github.com/kyleconroy/sqlc/internal/sql/astutils"
910
"github.com/kyleconroy/sqlc/internal/sql/lang"
1011
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
1112
)
1213

14+
// OutputColumns determines which columns a statement will output
15+
func (c *Compiler) OutputColumns(stmt ast.Node) ([]*catalog.Column, error) {
16+
qc, err := buildQueryCatalog(c.catalog, stmt)
17+
if err != nil {
18+
return nil, err
19+
}
20+
cols, err := outputColumns(qc, stmt)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
catCols := make([]*catalog.Column, 0, len(cols))
26+
for _, col := range cols {
27+
catCols = append(catCols, &catalog.Column{
28+
Name: col.Name,
29+
Type: ast.TypeName{Name: col.DataType},
30+
IsNotNull: col.NotNull,
31+
IsArray: col.IsArray,
32+
Comment: col.Comment,
33+
Length: col.Length,
34+
})
35+
}
36+
return catCols, nil
37+
}
38+
1339
func hasStarRef(cf *ast.ColumnRef) bool {
1440
for _, item := range cf.Fields.Items {
1541
if _, ok := item.(*ast.A_Star); ok {

internal/endtoend/testdata/create_view/mysql/go/db.go

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

internal/endtoend/testdata/create_view/mysql/go/models.go

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

internal/endtoend/testdata/create_view/mysql/go/query.sql.go

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: GetFirst :many
2+
SELECT * FROM first_view;
3+
4+
-- name: GetSecond :many
5+
SELECT * FROM second_view WHERE val2 = $1;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE foo (val text not null);
2+
3+
CREATE VIEW first_view AS SELECT * FROM foo;
4+
CREATE VIEW second_view AS SELECT * FROM foo;
5+
CREATE VIEW third_view AS SELECT * FROM foo;
6+
7+
ALTER TABLE foo ADD COLUMN val2 integer;
8+
CREATE OR REPLACE VIEW second_view AS SELECT * FROM foo;
9+
10+
DROP VIEW third_view;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/create_view/postgresql/go/db.go

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

internal/endtoend/testdata/create_view/postgresql/go/models.go

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

internal/endtoend/testdata/create_view/postgresql/go/query.sql.go

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- name: GetFirst :many
2+
SELECT * FROM first_view;
3+
4+
-- name: GetSecond :many
5+
SELECT * FROM second_view WHERE val2 = $1;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE foo (val text not null);
2+
3+
CREATE VIEW first_view AS SELECT * FROM foo;
4+
CREATE VIEW second_view AS SELECT * FROM foo;
5+
CREATE VIEW third_view AS SELECT * FROM foo;
6+
7+
ALTER TABLE foo ADD COLUMN val2 integer;
8+
CREATE OR REPLACE VIEW second_view AS SELECT * FROM foo;
9+
10+
DROP VIEW third_view;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "postgresql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,6 @@ func (c *cc) convertDeleteStmt(n *pcast.DeleteStmt) *ast.DeleteStmt {
302302
}
303303

304304
func (c *cc) convertDropTableStmt(n *pcast.DropTableStmt) ast.Node {
305-
// TODO: Remove once views are supported.
306-
if n.IsView {
307-
return todo(n)
308-
}
309305
drop := &ast.DropTableStmt{IfExists: n.IfExists}
310306
for _, name := range n.Tables {
311307
drop.Tables = append(drop.Tables, parseTableName(name))
@@ -667,7 +663,14 @@ func (c *cc) convertCreateUserStmt(n *pcast.CreateUserStmt) ast.Node {
667663
}
668664

669665
func (c *cc) convertCreateViewStmt(n *pcast.CreateViewStmt) ast.Node {
670-
return todo(n)
666+
return &ast.ViewStmt{
667+
View: c.convertTableName(n.ViewName),
668+
Aliases: &ast.List{},
669+
Query: c.convert(n.Select),
670+
Replace: n.OrReplace,
671+
Options: &ast.List{},
672+
WithCheckOption: ast.ViewCheckOption(n.CheckOption),
673+
}
671674
}
672675

673676
func (c *cc) convertDeallocateStmt(n *pcast.DeallocateStmt) ast.Node {

0 commit comments

Comments
 (0)