Skip to content

Support user-defined types with the query_as! and query_file_as! macros #148

Closed
@mehcode

Description

@mehcode

An example of what I would like to see for usage

#[derive(sqlx::Type)]
enum Role { Admin, Staff, Normal, Advisor }

#[sqlx::record] 
struct User { id: i32, name: String, role: Role }

let usr: User = sqlx::query_as!("SELECT id, name FROM user")
    .fetch_one(&mut conn).await?;
  • The #[sqlx::record] is merely saying I would be okay with requiring a proc attribute here if we need to inspect the struct and attach additional metadata to inspect later ( which I think we do )

I think this is blocked on Rust stabilizing const_panic and const_if. I can't think of another clean way to support this apart from generating a series of const _: () = { ... } blocks where we enforce the type equivalence (between the SQL result type and the given Rust type).

I'm definitely open to other suggestions though. What @abonander and I don't want though is to significantly degrade the errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockedenhancementNew feature or requestmacrosneeds rust featureThis issue or PR needs a Rust feature to be stabilized before it can be merged into master

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions