Skip to content

fix(compiler): Account for parameters without parents #2844

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
Oct 12, 2023
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
10 changes: 10 additions & 0 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,16 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,

default:
slog.Debug("unsupported reference type", "type", fmt.Sprintf("%T", n))
defaultP := named.NewInferredParam(ref.name, false)
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
a = append(a, Parameter{
Number: ref.ref.Number,
Column: &Column{
Name: p.Name(),
DataType: "any",
IsNamedParam: isNamed,
},
})
}
}
return a, nil
Expand Down
2 changes: 2 additions & 0 deletions internal/endtoend/testdata/coalesce_params/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/sqlc-dev/sqlc/issues/2748
https://github.com/sqlc-dev/sqlc/issues/2786
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/coalesce_params/mysql/go/db.go

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

87 changes: 87 additions & 0 deletions internal/endtoend/testdata/coalesce_params/mysql/go/models.go

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

57 changes: 57 additions & 0 deletions internal/endtoend/testdata/coalesce_params/mysql/go/query.sql.go

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

17 changes: 17 additions & 0 deletions internal/endtoend/testdata/coalesce_params/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- name: AddEvent :execlastid
INSERT INTO `Event` (
Timezone
) VALUES (
(CASE WHEN sqlc.arg("Timezone") = "calendar" THEN (SELECT cal.Timezone FROM Calendar cal WHERE cal.IdKey = sqlc.arg("calendarIdKey")) ELSE sqlc.arg("Timezone") END)
);

-- name: AddAuthor :execlastid
INSERT INTO authors (
address,
name,
bio
) VALUES (
?,
COALESCE(sqlc.narg("calName"), ""),
COALESCE(sqlc.narg("calDescription"), "")
);
43 changes: 43 additions & 0 deletions internal/endtoend/testdata/coalesce_params/mysql/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
CREATE TABLE `Calendar` (
`Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Relation` bigint(20) unsigned NOT NULL,
`CalendarName` longblob NOT NULL,
`Title` longblob NOT NULL,
`Description` longblob NOT NULL,
`Timezone` varchar(50) NOT NULL,
`UniqueKey` varchar(50) NOT NULL,
`IdKey` varchar(50) NOT NULL,
`MainCalendar` enum('true','false') NOT NULL DEFAULT 'false',
`CreateDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ModifyDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`),
KEY `Relation` (`Relation`),
KEY `UniqueKey` (`UniqueKey`),
KEY `IdKey` (`IdKey`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;


CREATE TABLE `Event` (
`Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Relation` bigint(20) unsigned NOT NULL,
`CalendarReference` bigint(20) unsigned NOT NULL,
`UniqueKey` varchar(50) NOT NULL,
`EventName` longblob NOT NULL,
`Description` longblob NOT NULL,
`Location` varchar(500) NOT NULL,
`Timezone` varchar(50) NOT NULL,
`IdKey` varchar(48) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `Relation` (`Relation`),
KEY `CalendarReference` (`CalendarReference`),
KEY `UniqueKey` (`UniqueKey`),
KEY `IdKey` (`IdKey`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;


CREATE TABLE authors (
id BIGINT AUTO_INCREMENT NOT NULL,
address VARCHAR(200) NOT NULL,
name VARCHAR(20) NOT NULL,
bio LONGTEXT NOT NULL
);
9 changes: 9 additions & 0 deletions internal/endtoend/testdata/coalesce_params/mysql/sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "mysql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"