Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iden all columns #654

Open
Sytten opened this issue Jul 10, 2023 · 3 comments
Open

Iden all columns #654

Sytten opened this issue Jul 10, 2023 · 3 comments

Comments

@Sytten
Copy link
Contributor

Sytten commented Jul 10, 2023

Motivation

I am reviving #90 because I had time to mature our solution and I think it is relevant to the general community.
Almost all our Iden look like:

use sea_query::Iden;

#[derive(Iden, Clone)]
pub enum Responses {
    Table,
    Id,
    StatusCode,
    Raw,
    Length,
    RoundtripTime,
    CreatedAt,
}

impl Responses {
    pub fn all_table_columns() -> Vec<(Responses, Responses)> {
        vec![
            (Responses::Table, Responses::Id),
            (Responses::Table, Responses::StatusCode),
            (Responses::Table, Responses::Raw),
            (Responses::Table, Responses::Length),
            (Responses::Table, Responses::RoundtripTime),
            (Responses::Table, Responses::CreatedAt),
        ]
    }

    pub fn all_columns() -> Vec<Responses> {
        vec![
            Responses::Id,
            Responses::StatusCode,
            Responses::Raw,
            Responses::Length,
            Responses::RoundtripTime,
            Responses::CreatedAt,
        ]
    }
}

This is super useful for query building because you have a central place to change if you add a new column to your table.
Both methods needed since sometimes you can't specify a table name (like in a CTE scenario), but most of the time you want to specify the table name.

Proposed Solutions

I think we should add a way to generate those methods or a variant of them with the Iden macro.
Or maybe it could be another macro like IdenIter.

I would also try to get rid of the Vec if possible and maybe even add some helpers on the Iterator.
I could see something like Responses::all_columns().except(Responses::Length)

Additional Information

I tried EnumIter but it really it not as convenient because of the need to add the table name.

@tyt2y3
Copy link
Member

tyt2y3 commented Jul 11, 2023

I think this is doable in SeaORM, because it is shipping it's own version of DeriveIden SeaQL/sea-orm#1740

@Sytten
Copy link
Contributor Author

Sytten commented Jul 11, 2023

This particular project doesn't pull sea-orm (it's a combination of diesel and sea-query). Worst case I can create another crate for this macro.

@tyt2y3
Copy link
Member

tyt2y3 commented Jul 11, 2023

If we put this inside sea-query, may be we can have a new macro, may be IdenIter / IdenList. And the methods can probably look like:

fn iter_columns() -> impl Iterator<Item = Self> {
    ..
}

fn all_columns() -> Vec<Self> {
    Self::iter_columns().collect()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants