Skip to content

Commit

Permalink
feat(analyzer): Return zero values when encountering unexpected ast n…
Browse files Browse the repository at this point in the history
…odes (#3069)

This allows users to run generate on any parseable query, even
when sqlc doesn't understand it.

Closes #2377
  • Loading branch information
andrewmbenton committed Dec 20, 2023
1 parent 58f7316 commit 55f3250
Show file tree
Hide file tree
Showing 24 changed files with 210 additions and 47 deletions.
13 changes: 0 additions & 13 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)

var table *ast.TableName
switch n := raw.Stmt.(type) {
case *ast.CallStmt:
case *ast.SelectStmt:
case *ast.DeleteStmt:
case *ast.DoStmt:
case *ast.InsertStmt:
if err := check(validate.InsertStmt(n)); err != nil {
return nil, err
Expand All @@ -148,15 +144,6 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
if err := check(err); err != nil {
return nil, err
}
case *ast.ListenStmt:
case *ast.NotifyStmt:
case *ast.TruncateStmt:
case *ast.UpdateStmt:
case *ast.RefreshMatViewStmt:
default:
if err := check(ErrUnsupportedStatementType); err != nil {
return nil, err
}
}

if err := check(validate.FuncCall(c.catalog, c.combo, raw)); err != nil {
Expand Down
12 changes: 5 additions & 7 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
}
for _, stmt := range stmts {
query, err := c.parseQuery(stmt.Raw, src, o)
if err == ErrUnsupportedStatementType {
continue
}
if err != nil {
var e *sqlerr.Error
loc := stmt.Raw.Pos()
Expand All @@ -95,6 +92,10 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
}
continue
}
if query == nil {
continue
}
query.Metadata.Filename = filepath.Base(filename)
queryName := query.Metadata.Name
if queryName != "" {
if _, exists := set[queryName]; exists {
Expand All @@ -103,10 +104,7 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
}
set[queryName] = struct{}{}
}
query.Metadata.Filename = filepath.Base(filename)
if query != nil {
q = append(q, query)
}
q = append(q, query)
}
}
if len(merr.Errs()) > 0 {
Expand Down
20 changes: 2 additions & 18 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
return nil, err
}

var targets *ast.List
targets := &ast.List{}
switch n := node.(type) {
case *ast.DeleteStmt:
targets = n.ReturningList
Expand Down Expand Up @@ -114,16 +114,8 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
if isUnion {
return c.outputColumns(qc, n.Larg)
}
case *ast.DoStmt:
targets = &ast.List{}
case *ast.CallStmt:
targets = &ast.List{}
case *ast.TruncateStmt, *ast.RefreshMatViewStmt, *ast.NotifyStmt, *ast.ListenStmt:
targets = &ast.List{}
case *ast.UpdateStmt:
targets = n.ReturningList
default:
return nil, fmt.Errorf("outputColumns: unsupported node type: %T", n)
}

var cols []*Column
Expand Down Expand Up @@ -487,7 +479,7 @@ func (r *tableVisitor) Visit(n ast.Node) astutils.Visitor {
// Return an error if a table is referenced twice
// Return an error if an unknown column is referenced
func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
var list *ast.List
list := &ast.List{}
switch n := node.(type) {
case *ast.DeleteStmt:
list = n.Relations
Expand All @@ -514,14 +506,6 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
astutils.Walk(&tv, n.FromClause)
astutils.Walk(&tv, n.Relations)
list = &tv.list
case *ast.DoStmt:
list = &ast.List{}
case *ast.CallStmt:
list = &ast.List{}
case *ast.NotifyStmt, *ast.ListenStmt:
list = &ast.List{}
default:
return nil, fmt.Errorf("sourceTables: unsupported node type: %T", n)
}

var tables []*Table
Expand Down
2 changes: 0 additions & 2 deletions internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/sqlc-dev/sqlc/internal/sql/validate"
)

var ErrUnsupportedStatementType = errors.New("parseQuery: unsupported statement type")

func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query, error) {
ctx := context.Background()

Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/exec_create_table/mysql/db/db.go

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.

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
@@ -0,0 +1,2 @@
-- name: CreateTable :exec
CREATE TABLE test (id INTEGER NOT NULL);
Empty file.
8 changes: 8 additions & 0 deletions internal/endtoend/testdata/exec_create_table/mysql/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
sql:
- queries: mysql.query.sql
schema: mysql.schema.sql
engine: mysql
gen:
go:
out: db
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/exec_create_table/postgresql/db/db.go

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.

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
@@ -0,0 +1,2 @@
-- name: CreateTable :exec
CREATE TABLE test (id INTEGER NOT NULL);
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
sql:
- queries: postgresql.query.sql
schema: postgresql.schema.sql
engine: postgresql
gen:
go:
out: db

31 changes: 31 additions & 0 deletions internal/endtoend/testdata/exec_create_table/sqlite/db/db.go

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.

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/exec_create_table/sqlite/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
sql:
- queries: sqlite.query.sql
schema: sqlite.schema.sql
engine: sqlite
gen:
go:
out: db
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: CreateTable :exec
CREATE TABLE test (id INTEGER NOT NULL);
Empty file.
6 changes: 0 additions & 6 deletions internal/endtoend/testdata/vet_failures/query.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text
);

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/vet_failures/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2
sql:
- schema: "query.sql"
- schema: "schema.sql"
queries: "query.sql"
engine: "postgresql"
gen:
Expand Down

0 comments on commit 55f3250

Please sign in to comment.