Skip to content

compiler: Find columns in default schemas #1003

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
May 4, 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
21 changes: 16 additions & 5 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ func resolveCatalogRefs(c *catalog.Catalog, rvs []*ast.RangeVar, args []paramRef
if defaultTable == nil {
defaultTable = table.Rel
}
if _, exists := typeMap[table.Rel.Schema]; !exists {
typeMap[table.Rel.Schema] = map[string]map[string]*catalog.Column{}
schema := table.Rel.Schema
if schema == "" {
schema = c.DefaultSchema
}
typeMap[table.Rel.Schema][table.Rel.Name] = map[string]*catalog.Column{}
if _, exists := typeMap[schema]; !exists {
typeMap[schema] = map[string]map[string]*catalog.Column{}
}
typeMap[schema][table.Rel.Name] = map[string]*catalog.Column{}
for _, c := range table.Columns {
cc := c
typeMap[table.Rel.Schema][table.Rel.Name][c.Name] = cc
typeMap[schema][table.Rel.Name][c.Name] = cc
}
return nil
}
Expand Down Expand Up @@ -146,7 +150,11 @@ func resolveCatalogRefs(c *catalog.Catalog, rvs []*ast.RangeVar, args []paramRef

var found int
for _, table := range search {
if c, ok := typeMap[table.Schema][table.Name][key]; ok {
schema := table.Schema
if schema == "" {
schema = c.DefaultSchema
}
if c, ok := typeMap[schema][table.Name][key]; ok {
found += 1
if ref.name != "" {
key = ref.name
Expand Down Expand Up @@ -308,6 +316,9 @@ func resolveCatalogRefs(c *catalog.Catalog, rvs []*ast.RangeVar, args []paramRef
schema = fqn.Schema
rel = fqn.Name
}
if schema == "" {
schema = c.DefaultSchema
}
if c, ok := typeMap[schema][rel][key]; ok {
a = append(a, Parameter{
Number: ref.ref.Number,
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/insert_values_public/mysql/go/db.go

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

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/insert_values_public/mysql/go/models.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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE foo (a text, b integer);

/* name: InsertValues :exec */
INSERT INTO public.foo (a, b) VALUES (?, ?);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/insert_values_public/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": "query.sql",
"queries": "query.sql"
}
]
}

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,4 @@
CREATE TABLE foo (a text, b integer);

-- name: InsertValues :exec
INSERT INTO public.foo (a, b) VALUES ($1, $2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "postgresql",
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}