Skip to content

Struct update rest pattern syntax support for named bindings #591

Open

Description

@jplatte

As an extension to #199, it would be nice if SQLx supported the following for databases compatible with named bindings:

query!("INSERT INTO foo (id, a, b, c) VALUES (:id, :a, :b, :c)", id=new_id, ..foo)

// shorthand for
query!(
    "INSERT INTO foo (id, a, b, c) VALUES (:id, :a, :b, :c)",
    id=new_id,
    a=foo.a,
    b=foo.b,
    c=foo.c,
)

Activity

  1. malobre commented on Dec 16, 2021

    @malobre

    I love this suggestion !

    In the context of web APIs, I often end up with a struct containing all the request parameters, something like this:

    struct NewUserParameters {
      email: String,
      password: String,
      // and many more fields
    }

    Followed by some queries such as:

    sqlx::query_as(concat!(
        "INSERT INTO users",
        " (email, password, etc..)",
        " VALUES ($1, $2, etc..)",
        " RETURNING *"
    ))
    .bind(new_user_parameters.email)
    .bind(new_user_parameters.password)
    // etc

    The suggested syntax would greatly reduce repetitions:

    sqlx::query_as!(concat!(
        "INSERT INTO users",
        " (email, password, etc..)",
        " VALUES (:email, :password, etc..)",
        " RETURNING *"
    ), ..new_user_parameters); // Done !
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions