SeaQL / sea-query

🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
https://www.sea-ql.org
Other
1.13k stars 185 forks source link

Feat: Adds `SelectStatement::apply` method for functionally building query in another function #730

Closed brahmlower closed 8 months ago

brahmlower commented 8 months ago

Heya! 👋 Thanks for all your work on the library- I've really enjoyed working with it! I ran into a quality of life shortcoming with the API around the SelectStatement builder: I wanted a clean way of building a portion of the query in another function, similar to the pattern used by the conditions method, but without the actual conditional part 😁

PR Info

New Features

Bug Fixes

None

Breaking Changes

None

Changes

None as of initial authorship, however the SelectStatement::conditions method could be updated to call out to the new apply function if so desired, but it'd be a little pedantic:

    pub fn conditions<T, F>(&mut self, b: bool, if_true: T, if_false: F) -> &mut Self
    where
        T: FnOnce(&mut Self),
        F: FnOnce(&mut Self),
    {
        if b {
-            if_true(self)
+            self.apply(if_true); 
        } else {
-            if_false(self)
+            self.apply(if_false);
        }
        self
    }
brahmlower commented 8 months ago

Can you add a doc test?

Oh absolutely! In hindsight it's pretty obvious that I should have included that in the first place 😅 I'll start working on that now, should have it pushed relatively soon

brahmlower commented 8 months ago

Mkay I've added the doctest example. I just copied and updated the example used for conditionals for the sake of consistency, but I'm happy to make changes if you'd prefer something different 👍

brahmlower commented 8 months ago

Hey @tyt2y3 👋 I just wanted to check in to see if there were any other concerns with this PR?

tyt2y3 commented 8 months ago

Hi! Just came back. Happy new year!

github-actions[bot] commented 8 months ago

:tada: Released In 0.30.7 :tada:

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 ⭐.

brahmlower commented 8 months ago

Hey thanks for merging this yesterday! Didn't intend to rush ya, so sorry about that- hope you had a good break, and happy new year to you too! 🥳