-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Next] [Feature] Generalized query placeholders (including support for expanding arrays into multiple placeholders) #875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Big thumbs up from me 👍 If the inline form could also permit |
We should probably follow the syntax recommendation here if we want to allow expressions: https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html#alternative-solution---interpolation let foo = Foo { bar: 50 };
// SELECT $1
sqlx::query!("SELECT {(foo.bar)}"); This also lets our Kleene operator work with these complex expressions: let foo = Foo { bar: [1, 2, 3] };
// SELECT $1, $2, $3
sqlx::query!("SELECT {(foo.bar)*}"); |
I think that 90% of my uses of this are going to be |
The I wonder if it would be possible to add a minimal extensions of this syntax for I'd like to insert all the fields of a struct succinctly - as a nice ergonomic for developers who want to prototyping quickly and leave choosing specific fields as a later optimization. Perhaps if there was a way to generate #[derive(sqlx::Columns)]
struct Foo { a: i32, b: i32, c: i32 }
let foo = Foo { a: 1, b: 2, c: 3 };
let values = foo.column_values();
// INSERT INTO foos (a, b, c) VALUES (?, ? ,?)
sqlx::query!("INSERT INTO foos ({Foo::COLUMNS*}) VALUES (values*)"); |
@D1plo1d I would probably prefer something a little less implicit like the following: #591 That would work okay with this proposal, although if you use the However, we can't implicitly expand the column list from the fields of |
Is there any update to when this might be implemented, or alternatively what blockers currently exist for implementation? I have a use-case of passing a vec of arguments for a |
I would love this as I'm currently writing a lot of code to manually trick indices for postgres. Having this done by name instead would be amazing |
Not being able to use |
@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:
The connector takes care of:
?
,$1
, or@p1
depending on the database driver?, ?, ?, ...
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 ofsqlx::query!
).We are proposing that we add support for the following ( both in
query!
andquery
):Positional Parameter
Named Parameter
Inline Parameter (only macros)
Array Parameter
{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?
The text was updated successfully, but these errors were encountered: