Class: Query

Inherits:
Object
  • Object
show all
Defined in:
lib/eno/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(**ctx, &block) ⇒ Query

Returns a new instance of Query.



9
10
11
12
# File 'lib/eno/query.rb', line 9

def initialize(**ctx, &block)
  @ctx = ctx
  @block = block
end

Instance Method Details

#as(sym) ⇒ Object



19
20
21
# File 'lib/eno/query.rb', line 19

def as(sym)
  Expressions::Alias.new(self, sym)
end

#except(*queries, **props, &block) ⇒ Object Also known as: ^



61
62
63
64
65
# File 'lib/eno/query.rb', line 61

def except(*queries, **props, &block)
  q1 = self
  queries << Query.new(&block) if queries.empty?
  Query.new(@ctx) { except q1, *queries, **props }
end

#except_all(*queries, &block) ⇒ Object



68
69
70
# File 'lib/eno/query.rb', line 68

def except_all(*queries, &block)
  except(*queries, all: true, &block)
end

#intersect(*queries, **props, &block) ⇒ Object Also known as: &



50
51
52
53
54
# File 'lib/eno/query.rb', line 50

def intersect(*queries, **props, &block)
  q1 = self
  queries << Query.new(&block) if queries.empty?
  Query.new(@ctx) { intersect q1, *queries, **props }
end

#intersect_all(*queries, &block) ⇒ Object



57
58
59
# File 'lib/eno/query.rb', line 57

def intersect_all(*queries, &block)
  intersect(*queries, all: true, &block)
end

#mutate(&block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/eno/query.rb', line 31

def mutate(&block)
  old_block = @block
  Query.new(@ctx) {
    instance_eval(&old_block)
    instance_eval(&block)
  }
end

#to_sql(escape_proc: nil, **ctx) ⇒ Object



14
15
16
17
# File 'lib/eno/query.rb', line 14

def to_sql(escape_proc: nil, **ctx)
  r = SQL::SQL.new(escape_proc: escape_proc, **@ctx.merge(ctx))
  r.to_sql(&@block)
end

#union(*queries, **props, &block) ⇒ Object Also known as: |



39
40
41
42
43
# File 'lib/eno/query.rb', line 39

def union(*queries, **props, &block)
  q1 = self
  queries << Query.new(&block) if queries.empty?
  Query.new(@ctx) { union q1, *queries, **props }
end

#union_all(*queries, &block) ⇒ Object



46
47
48
# File 'lib/eno/query.rb', line 46

def union_all(*queries, &block)
  union(*queries, all: true, &block)
end

#where(&block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/eno/query.rb', line 23

def where(&block)
  old_block = @block
  Query.new(@ctx) {
    instance_eval(&old_block)
    where instance_eval(&block)
  }
end