Skip to content

Commit

Permalink
fix(compiler): support identifiers with schema (#2579)
Browse files Browse the repository at this point in the history
* fix(compiler): support identifiers with schema

close #1858

* test: update endtoend
  • Loading branch information
orisano authored Aug 28, 2023
1 parent f9e18c8 commit fefa6f4
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,19 +612,26 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro

func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef) ([]*Column, error) {
parts := stringSlice(node.Fields)
var name, alias string
var schema, name, alias string
switch {
case len(parts) == 1:
name = parts[0]
case len(parts) == 2:
alias = parts[0]
name = parts[1]
case len(parts) == 3:
schema = parts[0]
alias = parts[1]
name = parts[2]
default:
return nil, fmt.Errorf("unknown number of fields: %d", len(parts))
}
var cols []*Column
var found int
for _, t := range tables {
if schema != "" && t.Rel.Schema != schema {
continue
}
if alias != "" && t.Rel.Name != alias {
continue
}
Expand Down

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

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/schema_scoped_list/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ CREATE TABLE foo.bar (id serial not null);

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

-- name: SchemaScopedColList :many
SELECT foo.bar.id FROM foo.bar;

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 @@ -3,3 +3,6 @@ CREATE TABLE foo.bar (id serial not null);

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

-- name: SchemaScopedColList :many
SELECT foo.bar.id FROM foo.bar;

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 @@ -3,3 +3,6 @@ CREATE TABLE foo.bar (id serial not null);

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

-- name: SchemaScopedColList :many
SELECT foo.bar.id FROM foo.bar;

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 @@ -3,3 +3,6 @@ CREATE TABLE foo.bar (id serial not null);

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

-- name: SchemaScopedColList :many
SELECT foo.bar.id FROM foo.bar;

0 comments on commit fefa6f4

Please sign in to comment.