Skip to content

Struct update rest pattern syntax support for named bindings #591

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

Open
jplatte opened this issue Jul 29, 2020 · 1 comment
Open

Struct update rest pattern syntax support for named bindings #591

jplatte opened this issue Jul 29, 2020 · 1 comment

Comments

@jplatte
Copy link
Contributor

jplatte commented Jul 29, 2020

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,
)
@malobre
Copy link

malobre commented Dec 16, 2021

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants