Skip to content

Add support for Views #1044

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 15, 2021
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
7 changes: 3 additions & 4 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/kyleconroy/sqlc/internal/multierr"
"github.com/kyleconroy/sqlc/internal/opts"
"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/catalog"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
"github.com/kyleconroy/sqlc/internal/sql/sqlpath"
)
Expand Down Expand Up @@ -54,7 +53,7 @@ func enumValueName(value string) string {
}

// end copypasta
func parseCatalog(p Parser, c *catalog.Catalog, schemas []string) error {
func (c *Compiler) parseCatalog(schemas []string) error {
files, err := sqlpath.Glob(schemas)
if err != nil {
return err
Expand All @@ -67,13 +66,13 @@ func parseCatalog(p Parser, c *catalog.Catalog, schemas []string) error {
continue
}
contents := migrations.RemoveRollbackStatements(string(blob))
stmts, err := p.Parse(strings.NewReader(contents))
stmts, err := c.parser.Parse(strings.NewReader(contents))
if err != nil {
merr.Add(filename, contents, 0, err)
continue
}
for i := range stmts {
if err := c.Update(stmts[i]); err != nil {
if err := c.catalog.Update(stmts[i], c); err != nil {
merr.Add(filename, contents, stmts[i].Pos(), err)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Compiler) Catalog() *catalog.Catalog {
}

func (c *Compiler) ParseCatalog(schema []string) error {
return parseCatalog(c.parser, c.catalog, schema)
return c.parseCatalog(schema)
}

func (c *Compiler) ParseQueries(queries []string, o opts.Parser) error {
Expand Down
26 changes: 26 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,39 @@ package compiler
import (
"errors"
"fmt"
"github.com/kyleconroy/sqlc/internal/sql/catalog"

"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/astutils"
"github.com/kyleconroy/sqlc/internal/sql/lang"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
)

// OutputColumns determines which columns a statement will output
func (c *Compiler) OutputColumns(stmt ast.Node) ([]*catalog.Column, error) {
qc, err := buildQueryCatalog(c.catalog, stmt)
if err != nil {
return nil, err
}
cols, err := outputColumns(qc, stmt)
if err != nil {
return nil, err
}

catCols := make([]*catalog.Column, 0, len(cols))
for _, col := range cols {
catCols = append(catCols, &catalog.Column{
Name: col.Name,
Type: ast.TypeName{Name: col.DataType},
IsNotNull: col.NotNull,
IsArray: col.IsArray,
Comment: col.Comment,
Length: col.Length,
})
}
return catCols, nil
}

func hasStarRef(cf *ast.ColumnRef) bool {
for _, item := range cf.Fields.Items {
if _, ok := item.(*ast.A_Star); ok {
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/create_view/mysql/go/db.go

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

21 changes: 21 additions & 0 deletions internal/endtoend/testdata/create_view/mysql/go/models.go

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

62 changes: 62 additions & 0 deletions internal/endtoend/testdata/create_view/mysql/go/query.sql.go

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

5 changes: 5 additions & 0 deletions internal/endtoend/testdata/create_view/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- name: GetFirst :many
SELECT * FROM first_view;

-- name: GetSecond :many
SELECT * FROM second_view WHERE val2 = $1;
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/create_view/mysql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE foo (val text not null);

CREATE VIEW first_view AS SELECT * FROM foo;
CREATE VIEW second_view AS SELECT * FROM foo;
CREATE VIEW third_view AS SELECT * FROM foo;

ALTER TABLE foo ADD COLUMN val2 integer;
CREATE OR REPLACE VIEW second_view AS SELECT * FROM foo;

DROP VIEW third_view;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/create_view/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "mysql",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/create_view/postgresql/go/db.go

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

21 changes: 21 additions & 0 deletions internal/endtoend/testdata/create_view/postgresql/go/models.go

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

63 changes: 63 additions & 0 deletions internal/endtoend/testdata/create_view/postgresql/go/query.sql.go

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

5 changes: 5 additions & 0 deletions internal/endtoend/testdata/create_view/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- name: GetFirst :many
SELECT * FROM first_view;

-- name: GetSecond :many
SELECT * FROM second_view WHERE val2 = $1;
10 changes: 10 additions & 0 deletions internal/endtoend/testdata/create_view/postgresql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE foo (val text not null);

CREATE VIEW first_view AS SELECT * FROM foo;
CREATE VIEW second_view AS SELECT * FROM foo;
CREATE VIEW third_view AS SELECT * FROM foo;

ALTER TABLE foo ADD COLUMN val2 integer;
CREATE OR REPLACE VIEW second_view AS SELECT * FROM foo;

DROP VIEW third_view;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/create_view/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "postgresql",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
13 changes: 8 additions & 5 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,6 @@ func (c *cc) convertDeleteStmt(n *pcast.DeleteStmt) *ast.DeleteStmt {
}

func (c *cc) convertDropTableStmt(n *pcast.DropTableStmt) ast.Node {
// TODO: Remove once views are supported.
if n.IsView {
return todo(n)
}
drop := &ast.DropTableStmt{IfExists: n.IfExists}
for _, name := range n.Tables {
drop.Tables = append(drop.Tables, parseTableName(name))
Expand Down Expand Up @@ -667,7 +663,14 @@ func (c *cc) convertCreateUserStmt(n *pcast.CreateUserStmt) ast.Node {
}

func (c *cc) convertCreateViewStmt(n *pcast.CreateViewStmt) ast.Node {
return todo(n)
return &ast.ViewStmt{
View: c.convertTableName(n.ViewName),
Aliases: &ast.List{},
Query: c.convert(n.Select),
Replace: n.OrReplace,
Options: &ast.List{},
WithCheckOption: ast.ViewCheckOption(n.CheckOption),
}
}

func (c *cc) convertDeallocateStmt(n *pcast.DeallocateStmt) ast.Node {
Expand Down
Loading