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

Add from_clear to allow emptying current from tables in select statement #716

Merged
merged 1 commit into from
Dec 14, 2023

Conversation

dragonnn
Copy link
Contributor

PR Info

Sea-orm does allow to modify the underlying sea-query statements with can be really useful but sea-query itself doesn't have a way to change the table a SelectStatement is done. This can be really handy when a database has a few tables that do share the same columns but are separated for other reasons, in my use case I have tables like that: data, data_now, data_1s, data_1m. All of them are identical to each other (data_now does have different PK keys, but that isn't important in my case), with using from_clear I can avoid having separate Entities for each table and avoid a lot of boilerplate when mapping them to a single Grpc struct that my backend outputs.

New Features

  • add new function to SelectStatement from_clear() with does remove all from statements from it

Changes

  • new function from_clear() added to SelectStatement

Let me know if anything more is need in this PR or if you have a better idea to archive what I need for my use case. Or a better name for from_clear() because I am myself not that happy with it.

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 20, 2023

Just being curious, why not build the same query with different Table passed in as parameters?

@dragonnn
Copy link
Contributor Author

Just being curious, why not build the same query with different Table passed in as parameters?

Because I am using sea-orm, so I don't build the query myself in the first place. Here is a sample of the code where I am using it:

    pub async fn get_now_by_device_uuid(device_uuid: Uuid, orm: &impl ConnectionTrait) -> DbResult<Option<Self>> {
        let mut query = Entity::find().filter(Column::DeviceUuid.eq(device_uuid));
        sea_orm::QueryTrait::query(&mut query).clear_from().from_as(super::data_now::Entity.table_ref(), Entity);
        query.one(orm).await
    }

Doing the whole query would be for sure more involving as getting the existing sea-orm query and just swapping the table out.

@dragonnn
Copy link
Contributor Author

If no one has any comments to it I would like to convert from draft to ready :). I would like really to get it merged so I don't have to use my fork in my projects.

@dragonnn dragonnn marked this pull request as ready for review October 27, 2023 19:49
Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay

@tyt2y3 tyt2y3 merged commit 256dee4 into SeaQL:master Dec 14, 2023
Copy link

🎉 Released In 0.30.5 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

@SeaQL SeaQL deleted a comment from github-actions bot Dec 29, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants