Skip to content

query_as! variant that populates a builder #4108

@Crazytieguy

Description

@Crazytieguy

I have found these related issues/pull requests

Description

For queries that return a subset of a struct's fields, query_as can't be used and the user has to call query and then manually move each of the fields into the struct.

Prefered solution

It'd be nice to have something like query_as_builder!(expr, query, ...args) that expands to expr.field1(value1).field2(value2) instead of Type {field1: value1, field2: value2}. With typesafe builders like bon this would still provide type safety, while allowing users to just populate the remaining required fields on the builder themselves. It could have the following variantes:

  • query_as_builder!(builder_expr, "SQL", args...) - Checked query
  • query_as_builder_unchecked!(...) - Unchecked variant
  • query_file_as_builder!(...) - Load SQL from file
  • query_file_as_builder_unchecked!(...) - Unchecked file variant

I made a standalone implementation of this that manipulates the macro output from query!, if this feature isn't appropriate for the main sqlx crate then I'd probably publish it under sqlx-query-as-builder.

Is this a breaking change? Why or why not?

Not breaking, new feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions