Multiple identical slices in one query are not handled properly
Version
1.27.0
What happened?
Consider this query:
SELECT *
FROM table1 AS t1 JOIN table2 AS t2
ON t1.fk = t2.fk
AND t1.fk IN (sqlc.slice('IDs')) OR t2.fk IN (sqlc.slice('IDs'));
The code which is generated for it have only one line for handling slice:
query = strings.Replace(query, "/*SLICE:IDs*/?", strings.Repeat(",?", len(ids))[1:], 1)
strings.Replace is called with last argument 1, so only one of two slices is handled.
Playground URL
https://play.sqlc.dev/p/69da31c00c3be911650469be15b8b0d12fb5f99d7be81e34e9208471bf0488fe
What operating system are you using?
Windows
What database engines are you using?
SQLite
What type of code are you generating?
Go
The only workaround I found is to name arguments of slices differently, but provide same values twice from the caller:
SELECT *
FROM table1 AS t1 JOIN table2 AS t2
ON t1.fk = t2.fk
AND t1.fk IN (sqlc.slice('IDs1')) OR t2.fk IN (sqlc.slice('IDs2'));
I just ran into this problem. I tried using WITH to put the values into a temporary table, but it just silently ignored the macro completely:
-- name: Test :many
WITH ids(id) AS (
VALUES (sqlc.slice('IDs'))
)
SELECT *
FROM table1 AS t1 JOIN table2 AS t2
ON t1.fk = t2.fk
AND t1.fk IN ids OR t2.fk IN ids;
const test = `-- name: Test :many
WITH ids(id) AS (
VALUES (sqlc.slice('IDs'))
)
SELECT t1.fk, t2.fk
FROM table1 AS t1 JOIN table2 AS t2
ON t1.fk = t2.fk
AND t1.fk IN ids OR t2.fk IN ids
`
type TestRow struct {
Fk string
Fk_2 string
}
Though, even if it hadn't, it wouldn't have worked because the macro would have been replaced with ?, ?, ?, but VALUES would have needed (?), (?), (?) to create separate rows.