Skip to content

Feature request: Templates for conditional queries #16

Description

@o-dasher

image
It would be interesting to have a way to declare a conditional query template that would help reduce a lot of code repetition such as in this case. It could work something along the way of this "pseudo-like code":

    create_query_template!(
       id_kind,
       match kind {
           SettingKind::Guild => "guild",
           SettingKind::Channel => "channel",
           SettingKind::User => "user"
       }
   );

   sqlx_conditional_queries::conditional_query_as!(
       BigID,
       "
       INSERT INTO discord_{#id_kind} (id{#args}) VALUES ({to_id}{#binds})
       ON CONFLICT DO NOTHING RETURNING id
       ",
       #(with(id_kind), args, binds) = match kind {
           SettingKind::Guild => ("", ""),
           SettingKind::Channel => (", guild_id", ", {guild_id_stored}"),
           SettingKind::User => ("", "")
       }
   )
   .fetch_one(pool)
   .await?;

   let booru_setting_insertion = sqlx_conditional_queries::conditional_query_as!(
       ID,
       "
       INSERT INTO booru_setting ({#id_kind}_id) VALUES ({to_id})
       ON CONFLICT DO NOTHING RETURNING id
       ",
       #with(id_kind)
   )
   .fetch_one(pool)
   .await?;

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions