martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

Make it possible to combine query sets using binary operators #127

Closed ellmetha closed 3 months ago

ellmetha commented 10 months ago

Description

Sometimes it is necessary to combine the results of two distinct query sets. Presently it is not possible to do so without incurring two distinct SQL queries to the database.

To palliate this, let's add support for combining querysets by using the & and | operators. With this new capability, it would become possible to combine two query sets using those operators as follows:

Tag.filter(label: "foo") & Tag.filter(enabled: true)
Tag.filter(label: "foo") | Tag.filter(label: "bar")

In order to make this possible, we should implement the #& and #| methods at the Marten::DB::Query::Set abstraction level.