Sequel Boolean Combinations of Datasets

Given that I have some data set methods foo, barandbaz

class User < Sequel::Model
  dataset_module do
    def foo
      # Some complicated dataset here
      where(:c => 42, :d => 23)
    end

    def bar
      # Even more complicated dataset here
      where(:a => 5, :b => 23).or(:a => 23, :b => 5)
    end

    def baz
      where(:d => 17)
    end
  end
end

I want to query the database for (foo || bar) && (bar || baz)(or another complex dataset). So I tried

User.where{|u| (u.foo | u.bar) & (u.bar | u.baz)}

EDIT : Clarification:

What i got

SELECT * FROM users WHERE ((`foo` OR `bar`) AND (`bar` OR `baz`))

What i wanted

SELECT * FROM users WHERE ((<dataset foo> OR <dataset bar>) AND (<dataset bar> OR <dataset baz>))

where <dataset xyz>mean my specific datasets. So

<dataset foo> defined as (c = 42 AND d = 23)

<dataset bar> defined as ((a = 5 AND b = 23) OR (a = 23, b = 5))

<dataset baz> defined as (d = 17)

How to connect dataset methods using AND, ORand (most important) parentheses?

+4
source share
2 answers

. "AND" "OR" .

?

fooOrbar = User.foo.union(User.bar)
barOrbaz = User.bar.union(User.baz)
result = fooOrbar.intersect(barOrbaz)
+1

Source: https://habr.com/ru/post/1533333/


All Articles