Skip to content

Is there any plan to allow using other macros in query!()? #712

Closed
@qwfy

Description

@qwfy

Often, you need to write similar but distinct sqls (eg. all sqls have the same columns but different wheres) based on the inputs, (note all combinations of inputs are known at the development time, so this in theory can be done at compile time), it would reduce code duplication (thus reduce the burden of maintaining and testing), this could be done via code like this:

if condition1() {
  sqlx::query!(generate_sql_and_args!(condition1 = true, value)...
} else {
  sqlx::query!(generate_sql_and_args!(condition2 = true)...

// generate_sql_and_args!(condition1 = true) would expand to:
// "select col from tab where col = $1", value
// the the first query! becomes:
sqlx::query!("select col from tab where col = $1", value)
}

This (semi-dynamic query) is something that we use quite often, it would be wonderful that this can be expressed in a type safe manner.

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