Skip to content

Order of parameters is not preserved when cannonicalizing sql structure #29

@LostKobrakai

Description

@LostKobrakai
Mix.install([{:sql, "~> 0.4.0"}])
# Mix.install([{:sql, github: "elixir-dbvisor/sql", branch: "main"}])

defmodule Bug do
  import SQL

  def run do
    name = "alice"
    min_age = 18

    {sql, params} =
      ~SQL[WHERE name = {{name}} SELECT id, {{min_age}} AS threshold FROM users]
      |> SQL.to_sql()

    IO.puts("""
    SQL:    #{sql}
    Params: #{inspect(params)}

    First  ? (SELECT threshold) bound to: #{inspect(Enum.at(params, 0))} — expected: #{inspect(min_age)}
    Second ? (WHERE name)       bound to: #{inspect(Enum.at(params, 1))} — expected: #{inspect(name)}
    """)
  end
end

Bug.run()
SQL:     select id, ? as threshold from users where name = ?
Params: ["alice", 18]

First  ? (SELECT threshold) bound to: "alice" — expected: 18
Second ? (WHERE name)       bound to: 18 — expected: "alice"

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions