Method: DataMapper::Persevere::Query#process_condition

Defined in:
lib/persevere_adapter/query.rb

#process_condition(condition) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/persevere_adapter/query.rb', line 51

def process_condition(condition)
  case condition
    # Persevere 1.0 regular expressions are disable for security so we pass them back for DataMapper query filtering
    # without regular expressions, the like operator is inordinately challenging hence we pass it back
    # when :regexp then "RegExp(\"#{condition.value.source}\").test(#{condition.subject.name})"
  when DataMapper::Query::Conditions::RegexpComparison then []
  when DataMapper::Query::Conditions::LikeComparison then "#{condition.subject.field}='#{condition.loaded_value.gsub('%', '*')}'"
  when DataMapper::Query::Conditions::AndOperation then 
    inside = condition.operands.map { |op| process_condition(op) }.flatten
    inside.empty? ? []  : "(#{inside.join("&")})"
  when DataMapper::Query::Conditions::OrOperation then "(#{condition.operands.map { |op| process_condition(op) }.join("|")})"
  when DataMapper::Query::Conditions::NotOperation then 
    inside = process_condition(condition.operand) 
    inside.empty? ? [] : "!(%s)" % inside
  when DataMapper::Query::Conditions::InclusionComparison then 
    result_string = Array.new
    condition.value.to_a.each do |candidate|
      if condition.subject.is_a?(DataMapper::Associations::Relationship)
        result_string << "#{condition.subject.child_key.first.name}=#{candidate.key.first}" #munge_condition(condition)
      else
        result_string << "#{condition.subject.name}=#{candidate}"
      end
    end
    if result_string.length > 0
      "(#{result_string.join("|")})"
    else
      "#{condition.subject.name}=''"
    end
  when DataMapper::Query::Conditions::EqualToComparison,
    DataMapper::Query::Conditions::GreaterThanComparison,
    DataMapper::Query::Conditions::LessThanComparison, 
    DataMapper::Query::Conditions::GreaterThanOrEqualToComparison,
    DataMapper::Query::Conditions::LessThanOrEqualToComparison then
    munge_condition(condition)
  when DataMapper::Query::Conditions::NullOperation then []
  when Array then
    old_statement, bind_values = condition
    statement = old_statement.dup
    bind_values.each{ |bind_value| statement.sub!('?', bind_value.to_s) }
    statement.gsub(' ', '')
  else condition.to_s.gsub(' ', '')
  end
end