sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Queryx macro

Open Lege19 opened this issue 2 months ago • 0 comments

I have found these related issues/pull requests

Makes #4113 more practical

Description

Currently there is a separate query...! macro for each combination of:

  • Checked/unchecked
  • Query from literal/from file
  • Scalar/Inferred/Provided

Each new feature added to these macros creates more macros.

I specifically have #4113 in mind, where continuing the current way might mean doubling the number of macros.

Prefered solution

One macro to rule them all: queryx!.

This macro would have a more complex syntax and would combine the functionalities of all the current query macros.

I've experimented with this to find out what is reasonably easy to parse.

Proposed syntax:

  • Flags (Optional)
  • Query
  • Parameters (Optional)
  • Type (Optional)

Flags

Zero or more flags. Currently the only flag is unchecked, which changes the checking behavior to be like query_unchecked!

Query

A string literal, or file("path") to read the query from a file.

The query itself is the same as in query! with regards to type and nullability overrides.

Joining multiple literals into a single query with + is not supported, users can achieve the same effect with concat!. I tried implementing this and it can be done, but it makes makes things more complicated. Alternative syntax for this could still be reasonably easy, such as ("1" "2" "3").

Parameters

A comma separated list of expressions wrapped in parenthesis.

The syntax is intended to look like a function call.

Type

Information about the desired return type can be given after a :, if this section is omitted the type will be inferred, and will derive Debug only.

There are multiple options for what can come after the :

  • An identifier: behaves like query_as!
  • scalar: behaves like query_scalar!
  • (Achieving #4113) Zero or more attributes: infers the return type, and applies the provided attributes in addition to deriving Debug

Is this a breaking change? Why or why not?

This is not a breaking change because the existing macro behavior is not changed.

Lege19 avatar Nov 24 '25 18:11 Lege19