perdumonocle / sql-builder

Simple SQL code generator.
MIT License
123 stars 13 forks source link

Is there UUID support? #10

Open onx2 opened 3 years ago

onx2 commented 3 years ago

Is there now (or in the future) UUID support or should I just convert to a string?

pub async fn find_by_id(ctx: &Context<'_>, id: Uuid) -> Result<Option<Organization>> {
    let pool = ctx.data::<PgPool>()?;

    let sql = SqlBuilder::select_from(ORGANIZATION_TABLE_NAME)
        .field("*")
        // Right now I need to convert to a string
        .and_where("id = $1".bind(&id.to_string()))
        .sql()?;

    Ok(sqlx::query_as::<_, Organization>(sql.as_str())
        .fetch_optional(pool)
        .await?)
}

Also, would you recommend using .and_where("id = $1".bind(&SqlName::new(id).safe())) to prevent sql injection since the id is passed from the client or is my understanding of SqlName incorrect?

mike-lloyd03 commented 1 year ago

@onx2 Did you ever sort this out? I'm in the same boat.

onx2 commented 1 year ago

@mike-lloyd03 I didn't put any effort into resolving this issue because I was just playing around with some personal projects. For my professional work I've been using Diesel. You could also check out sqlx which would be a closer comparison to this package than diesel.

If you want to stick with this package you might be able to submit a PR but it looks like it isn't maintained anymore (last commit). Your best bet is probably forking and maybe modifying arg.rs to support UUID.

mike-lloyd03 commented 1 year ago

Believe it or not, I'm actually using this to generate the sql that I pass into sqlx. The and_when function saves a lot of redundancy. But I didn't realize isn't been over 2 years since last it was updated. I might just go back to writing my own queries.

But also uuid.to_string() seems to work fine.