Description
@abonander and I have been discussing the benefits of providing a more Python style of SQL placeholders for parameters
In Python database connectors, it looks like this:
conn.execute("SELECT %(name)s", name="Hello")
The connector takes care of:
- Using
?
,$1
, or@p1
depending on the database driver - Expanding arrays into
?, ?, ?, ...
- Allowing named parameters where none are supported such as PostgreSQL
The really interesting part is Intellij understands that %(name)s
is a SQL parameter and correctly parses that query and still provides proper code completion ( as a side note, Intellij Rust recently gained support for SQL highlighting and code completion inside of sqlx::query!
).
We are proposing that we add support for the following ( both in query!
and query
):
Positional Parameter
// "SELECT ?" in MySQL
// "SELECT $1" in PostgreSQL
sqlx::query!("SELECT {0}", 10);
Named Parameter
// "SELECT @id" in MSSQL
// "SELECT $id" in SQLite
// "SELECT $1" in PostgreSQL
sqlx::query!("SELECT {id}", id=10);
Inline Parameter (only macros)
// "SELECT @id" in MSSQL
// "SELECT $id" in SQLite
// "SELECT $1" in PostgreSQL
let id = 10;
sqlx::query!("SELECT {id}");
Array Parameter
// MySQL = "SELECT * FROM foo WHERE id IN (?, ?, ?, ...)"
// PostgreSQL = "SELECT * FROM foo WHERE id IN ($1, $2, $3, ...)"
sqlx::query!("SELECT * FROM foo WHERE id IN ({ids+})", ids=[1, 2, 3]);
{ids+}
or{ids*}
becomes a comma-delimited list of parameters{ids+}
for a0
length array becomesNULL
{ids*}
for a0
length array becomes `` (nothing)
I was on the fence about doing something like this but we can teach Intellij to recognize {...}
as a SQL parameter which would let Intellij continue to parse and provide completion in our SQL queries.
Thoughts?