Skip to content

[Proposal] Support named bind parameters #199

Open

Description

@mehcode
struct Query {
  fn bind_as<T>(name: &'static str, value: T) -> Self;
}
query!("SELECT ?name", name = 320);

Activity

  1. added
    db:mysql
    db:sqlite
    on Mar 30, 2020
  2. abonander commented on Jul 29, 2020

    @abonander
    Collaborator

    With call-site hygiene stabilized in 1.45, the explicit binding could actually be omitted:

    let name = 320;
    query!("select ?name");
  3. jplatte commented on Aug 21, 2021

    @jplatte
    Contributor

    Should this be closed in favor of #875? That one has a bunch more comments.

  4. abonander commented on Aug 30, 2021

    @abonander
    Collaborator

    This is a related issue of supporting named parameters in general. Implementing #875 would close both issues.

  5. CMeldgaard commented on Aug 29, 2024

    @CMeldgaard

    Is there any update on named bindings?

  6. CommanderStorm commented on Aug 29, 2024

    @CommanderStorm
    Contributor

    Not really, otherwise this issue would be closed, or that progress would be linked to this issue.
    You can however help make this happen.. In rust, most people are volunteers ^^
    The contribution guide is here: https://github.com/launchbadge/sqlx/blob/main/CONTRIBUTING.md

    In case your company needs this, you could also consider sponsoring @abonander (I don't speak for launchbadge or Austin => have no inside intel if that is an option, but that should likely be taken offline anyway..)

  7. Flamenco commented on Oct 5, 2024

    @Flamenco

    I have come up with a solution to this issue. I wrote a procedural macro to wrap the sqlx command, the SQL string, and the parameters.

            let aString = "hello";
            let aNumber = 12;
            let aBoolean = true;
    
            let res = bind_by_name!(
                sqlx::query
                "select :aString as aString, :aNumber as aNumber, :aBoolean as aBoolean"
                [aString aNumber aBoolean]
            ).fetch_one(&db.pool).await?
    

    This expands to:

    sqlx::query("select ? as aString, ? as aNumber, ? as aBoolean")
      .bind(aString).bind(aNumber).bind(aBoolean)
      .fetch_one(&db.pool).await?
    

    You can also map values directly:

            let res = bind_by_name!(
                sqlx::query
                "select :aString as aString, :aNumber as aNumber, :aBoolean as aBoolean"
                [aString:"hello" aNumber:12 aBoolean:true]
            ).fetch_one(&db.pool).await?
    

    Whitespace and brackets are flexable

            let res = bind_by_name!(
                sqlx::query "select :aString as foo" {aString:"hello"}
           ).fetch_one(&db.pool).await?
    

    This will work on any sqlx command that accepts a string and has a bind function.

    There is also a postgres version that uses a$ prefix and has numbered placeholders.

            let res = bind_by_name_pg!(
                sqlx::query
                "select $aString as aString, $aNumber as aNumber, $aBoolean as aBoolean"
                [aString:"hello" aNumber:12 aBoolean:true]
            )
    

    That expands to:

    sqlx::query("select $1 as aString, $2 as aNumber, $3 as aBoolean")
      .bind("hello").bind(12).bind(true)
    

    The macro shows edit-time errors if you use a parameter name in your SQL that is not in the map.

    Screenshot 2024-10-04 at 10 40 01 PM Screenshot 2024-10-04 at 10 40 08 PM

    As you can see, I am not a big fan of "noisy" syntax. 😎

    I will post this crate after some feedback and if there is interest.

    Working Screenshots

    Screenshot 2024-10-04 at 10 50 43 PM Screenshot 2024-10-04 at 10 50 48 PM
  8. CMeldgaard commented on Oct 30, 2024

    @CMeldgaard

    @Flamenco, i would love to see this as a crate :D

  9. Flamenco commented on Mar 17, 2025

    @Flamenco

    @CMeldgaard This is still on the TODO list. It's going to take a bit more encouragement from the public before we publish. :D

  10. tyt2y3 commented on Aug 22, 2025

    @tyt2y3

    (I posted this in another issue already), in sea-query 1.0-rc you can do:

    let values = vec![(Uuid::new_v4(), 12, "A"), (Uuid::new_v4(), 14, "B")];
    
    let res = sea_query::sqlx::sqlite::query!(
        sql = r#"INSERT INTO "character" ("uuid", "font_size", "character") VALUES {..(values.0:2),}"#
    ).execute(pool).await?;
    
    let character = Character { id: 1, font_size: 14 };
    
    let res = sea_query::sqlx::sqlite::query!(
        sql = r#"UPDATE "character" SET "font_size" = {character.font_size} WHERE "id" = {character.id}"#
    ).execute(pool).await?;
    
    let ids = [2, 3, 4];
    
    let res = sea_query::sqlx::sqlite::query!(
        sql = r#"DELETE FROM "character" WHERE "id" IN ({..ids})"#
    ).execute(pool).await?;

    full example can be found here: https://github.com/SeaQL/sea-query/blob/master/examples/sqlx_sqlite/src/main.rs

  11. added a commit that references this issue on Dec 31, 2025
    93a926b
  12. andrewgazelka commented on Dec 31, 2025

    @andrewgazelka

    I implemented this feature in my fork using Claude Code: https://github.com/andrewgazelka/sqlx

    Usage examples:

    // Shorthand syntax (variable name matches parameter name)
    query!("SELECT * FROM users WHERE id = :id AND name = :name", id, name)
    
    // Explicit syntax
    query!("SELECT * FROM users WHERE id = :id", id = user_id)
    
    // Mixed
    query!("SELECT * FROM users WHERE id = :id AND name = :name",
           id, name = get_name())

    Supported named parameter formats in SQL:

    • :name (colon prefix - common in Oracle, SQLite)
    • ?name (question mark prefix)
    • @name (at prefix - SQL Server style)

    Key features:

    • SQL parser correctly skips string literals, comments, and PostgreSQL cast operators (::)
    • Duplicate parameters in SQL reuse the same positional placeholder for PostgreSQL ($N) while MySQL/SQLite use separate ? placeholders
    • Named arguments validated against SQL parameters at compile time
    • Arguments automatically reordered to match SQL parameter order
    • Fully backwards compatible with existing positional syntax

    Commit: andrewgazelka@93a926b1

    Happy to open a PR if there's interest in merging this upstream!

  13. abonander commented on Dec 31, 2025

    @abonander
    Collaborator

    @andrewgazelka we're not interested in accepting AI-generated code at this time. It's not worth my extremely limited time or energy to review.

    Case-in-point:

    Edit: oh! And no documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    db:mysqldb:sqliteproposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions