casbin-rs / sqlx-adapter

Asynchronous casbin adapter for mysql, postgres, sqlite based on sqlx-rs

Home Page:https://github.com/casbin/casbin-rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MySQL & PostgreSQL compatible query macro

hackerchai opened this issue · comments

Currently I have to write code like this to support MySQL and PostgreSQL query.

#[cfg(feature = "postgres")]
    sqlx::query!(
        "DELETE FROM tables WHERE
                    ptype = $1 AND
                    v0 = $2 AND
                    v1 = $3 AND
                    v2 = $4 AND
                    v3 = $5 AND
                    v4 = $6 AND
                    v5 = $7",
        pt.to_string(),
        rule[0],
        rule[1],
        rule[2],
        rule[3],
        rule[4],
        rule[5]
    )
    .execute(&mut conn)
#[cfg(feature = "mysql")]
sqlx::query!(
        "DELETE FROM tables WHERE
                    ptype = ? AND
                    v0 = ? AND
                    v1 = ? AND
                    v2 = ? AND
                    v3 = ? AND
                    v4 = ? AND
                    v5 = ?",
        pt.to_string(),
        rule[0],
        rule[1],
        rule[2],
        rule[3],
        rule[4],
        rule[5]
    )
    .execute(&mut conn)

I think a custom macro will help to avoid duplicated codes like this.

Sqlx issue can track here: launchbadge/sqlx#306

It seems that sqlx community has rejected such a change. So we closed it here for now. Correct me if I'm understanding it wrongly.