Skip to content

Commit 9590282

Browse files
authored
fix(compiler): Account for parameters without parents (#2844)
* fix(compiler): Account for parameters without parents Mark their type as unknown * mark those parameters as nullable
1 parent e44f238 commit 9590282

File tree

8 files changed

+256
-0
lines changed

8 files changed

+256
-0
lines changed

internal/compiler/resolve.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,16 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
600600

601601
default:
602602
slog.Debug("unsupported reference type", "type", fmt.Sprintf("%T", n))
603+
defaultP := named.NewInferredParam(ref.name, false)
604+
p, isNamed := params.FetchMerge(ref.ref.Number, defaultP)
605+
a = append(a, Parameter{
606+
Number: ref.ref.Number,
607+
Column: &Column{
608+
Name: p.Name(),
609+
DataType: "any",
610+
IsNamedParam: isNamed,
611+
},
612+
})
603613
}
604614
}
605615
return a, nil
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
https://github.com/sqlc-dev/sqlc/issues/2748
2+
https://github.com/sqlc-dev/sqlc/issues/2786

internal/endtoend/testdata/coalesce_params/mysql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/coalesce_params/mysql/go/models.go

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/coalesce_params/mysql/go/query.sql.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- name: AddEvent :execlastid
2+
INSERT INTO `Event` (
3+
Timezone
4+
) VALUES (
5+
(CASE WHEN sqlc.arg("Timezone") = "calendar" THEN (SELECT cal.Timezone FROM Calendar cal WHERE cal.IdKey = sqlc.arg("calendarIdKey")) ELSE sqlc.arg("Timezone") END)
6+
);
7+
8+
-- name: AddAuthor :execlastid
9+
INSERT INTO authors (
10+
address,
11+
name,
12+
bio
13+
) VALUES (
14+
?,
15+
COALESCE(sqlc.narg("calName"), ""),
16+
COALESCE(sqlc.narg("calDescription"), "")
17+
);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
CREATE TABLE `Calendar` (
2+
`Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
3+
`Relation` bigint(20) unsigned NOT NULL,
4+
`CalendarName` longblob NOT NULL,
5+
`Title` longblob NOT NULL,
6+
`Description` longblob NOT NULL,
7+
`Timezone` varchar(50) NOT NULL,
8+
`UniqueKey` varchar(50) NOT NULL,
9+
`IdKey` varchar(50) NOT NULL,
10+
`MainCalendar` enum('true','false') NOT NULL DEFAULT 'false',
11+
`CreateDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
12+
`ModifyDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
13+
PRIMARY KEY (`Id`),
14+
KEY `Relation` (`Relation`),
15+
KEY `UniqueKey` (`UniqueKey`),
16+
KEY `IdKey` (`IdKey`)
17+
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
18+
19+
20+
CREATE TABLE `Event` (
21+
`Id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
22+
`Relation` bigint(20) unsigned NOT NULL,
23+
`CalendarReference` bigint(20) unsigned NOT NULL,
24+
`UniqueKey` varchar(50) NOT NULL,
25+
`EventName` longblob NOT NULL,
26+
`Description` longblob NOT NULL,
27+
`Location` varchar(500) NOT NULL,
28+
`Timezone` varchar(50) NOT NULL,
29+
`IdKey` varchar(48) DEFAULT NULL,
30+
PRIMARY KEY (`Id`),
31+
KEY `Relation` (`Relation`),
32+
KEY `CalendarReference` (`CalendarReference`),
33+
KEY `UniqueKey` (`UniqueKey`),
34+
KEY `IdKey` (`IdKey`)
35+
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
36+
37+
38+
CREATE TABLE authors (
39+
id BIGINT AUTO_INCREMENT NOT NULL,
40+
address VARCHAR(200) NOT NULL,
41+
name VARCHAR(20) NOT NULL,
42+
bio LONGTEXT NOT NULL
43+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "2"
2+
sql:
3+
- engine: "mysql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"

0 commit comments

Comments
 (0)