Module: CompositePrimaryKeys::Predicates

Defined in:
lib/composite_primary_keys/composite_predicates.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) cpk_and_predicate(predicates)



3
4
5
6
7
8
9
# File 'lib/composite_primary_keys/composite_predicates.rb', line 3

def cpk_and_predicate(predicates)
  if predicates.length == 1
    predicates.first
  else
    Arel::Nodes::And.new(predicates)
  end
end

- (Object) cpk_id_predicate(table, keys, values)



21
22
23
24
25
26
# File 'lib/composite_primary_keys/composite_predicates.rb', line 21

def cpk_id_predicate(table, keys, values)
  eq_predicates = keys.zip(values).map do |key, value|
    table[key].eq(value)
  end
  cpk_and_predicate(eq_predicates)
end

- (Object) cpk_in_predicate(table, primary_keys, ids)



38
39
40
41
42
43
44
45
46
47
# File 'lib/composite_primary_keys/composite_predicates.rb', line 38

def cpk_in_predicate(table, primary_keys, ids)
  and_predicates = ids.map do |id_set|
    eq_predicates = Array(primary_keys).zip(Array(id_set)).map do |primary_key, value|
      table[primary_key].eq(value)
    end
    cpk_and_predicate(eq_predicates)
  end

  cpk_or_predicate(and_predicates)
end

- (Object) cpk_join_predicate(table1, key1, table2, key2)



28
29
30
31
32
33
34
35
36
# File 'lib/composite_primary_keys/composite_predicates.rb', line 28

def cpk_join_predicate(table1, key1, table2, key2)
  key1_fields = Array(key1).map {|key| table1[key]}
  key2_fields = Array(key2).map {|key| table2[key]}

  eq_predicates = key1_fields.zip(key2_fields).map do |key_field1, key_field2|
    key_field1.eq(key_field2)
  end
  cpk_and_predicate(eq_predicates)
end

- (Object) cpk_or_predicate(predicates)



11
12
13
14
15
16
17
18
19
# File 'lib/composite_primary_keys/composite_predicates.rb', line 11

def cpk_or_predicate(predicates)
  # Can't do or here, arel does the wrong thing and makes a really
  # deeply nested stack that blows up
  predicates = predicates.map do |predicate|
    "(#{predicate.to_sql})"
  end
  predicates = "(#{predicates.join(" OR ")})"
  Arel::Nodes::SqlLiteral.new(predicates)
end