Module: Card::Query::Helpers

Included in:
Card::Query
Defined in:
lib/card/query/helpers.rb

Instance Method Summary collapse

Instance Method Details

#id_from_val(val) ⇒ Object



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

def id_from_val val
  case val
  when Integer then val
  when String  then Card.fetch_id(val)
  end
end

#join_cards(val, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/card/query/helpers.rb', line 61

def join_cards val, opts={}
  conditions_on_join = opts.delete :conditions_on_join
  s = subquery
  join_opts = { from: self, to: s }.merge opts
  card_join = Join.new join_opts
  joins << card_join unless opts[:from].is_a? Join
  s.conditions_on_join = card_join if conditions_on_join
  s.interpret val
  s
end

#join_references(key, val) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/card/query/helpers.rb', line 21

def join_references key, val
  r = Reference.new(key, val, self)
  refjoin = Join.new(from: self, to: r, to_field: r.infield)
  joins << refjoin
  restrict_reference r, refjoin if r.cardquery
  r.conditions.each do |condition|
    refjoin.conditions << "#{r.table_alias}.#{condition}"
  end
end

#restrict(id_field, val) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/card/query/helpers.rb', line 4

def restrict id_field, val
  if (id = id_from_val(val))
    interpret id_field => id
  else
    join_cards val, from_field: id_field
  end
end

#restrict_reference(ref, refjoin) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/card/query/helpers.rb', line 12

def restrict_reference ref, refjoin
  val = ref.cardquery
  if (id = id_from_val(val))
    add_condition "#{ref.table_alias}.#{ref.outfield} = #{id}"
  else
    join_cards val, from: refjoin, from_field: ref.outfield
  end
end

#table_aliasObject



38
39
40
41
42
43
44
45
46
# File 'lib/card/query/helpers.rb', line 38

def table_alias
  @table_alias ||= begin
    if @unjoined
      @superquery.table_alias
    else
      "c#{table_id}"
    end
  end
end

#table_id(force = false) ⇒ Object

generates an id used to identify a table variable in the sql statement



49
50
51
52
53
54
55
# File 'lib/card/query/helpers.rb', line 49

def table_id force=false
  if force
    tick_table_seq!
  else
    @table_id ||= tick_table_seq!
  end
end

#tick_table_seq!Object



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

def tick_table_seq!
  root.table_seq = root.table_seq.to_i + 1
end