Skip to content

PrefixExpr() produces wrong dollar parameter placeholders #286

@atombender

Description

@atombender

Example query:

sql, params := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
	Select("*").
	PrefixExpr(
		squirrel.StatementBuilder.
			Select("time").
			From("smurfs").
			Where("id = ?", "x").
			Prefix("WITH a AS (").Suffix(")"),
	).
	From("a").
	Where("time > ?", time.Now()).
	MustSql()

This generates the following SQL (reformatted for eligibility):

WITH a AS (
  SELECT time FROM smurfs WHERE id = $1
)
SELECT * FROM a WHERE time > $1

with parameters:

["x", "2021-05-21 13:27:32.380139 +0200"]

Obviously this should be:

WITH a AS (
  SELECT time FROM smurfs WHERE id = $1
)
SELECT * FROM a WHERE time > $2

Unfortunate workaround (not tested):

q1, p1 := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
	Select("time").
	From("document_changes").
	Where("id = ?", "x").
	Prefix("WITH a AS (").Suffix(")").
	MusSql()

q2, p2 := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
	Select("*").
	Prefix(q1).
	From("a").
	Where("time > ?", time.Now()).
	MustSql()

sql := q1 + q2
params := append(p1, p2...)

Presumably the bug also affects SuffixExpr().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions