Class: Facetious::Facet
- Inherits:
-
Struct
- Object
- Struct
- Facetious::Facet
- Defined in:
- lib/facetious.rb
Constant Summary collapse
- ValueConverter =
{ string: proc {|value| "'" + value.gsub(/'/, "''") + "'" }, integer: proc {|value| Integer(value).to_s }, date: proc {|value| raise "REVISIT: Don't use DATE VALUE before implementing it" value }, integers: proc {|value| '(' + (value-['']).map{|i| Integer(i).to_s}.join(',') + ')' }, strings: proc {|value| '(' + (value-['']).map{|str| sql_value(:string, str)}.join(',') + ')' } }
Instance Attribute Summary collapse
-
#data_type ⇒ Object
Returns the value of attribute data_type.
-
#field_name ⇒ Object
Returns the value of attribute field_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#title ⇒ Object
Returns the value of attribute title.
-
#where ⇒ Object
Returns the value of attribute where.
Instance Method Summary collapse
Instance Attribute Details
#data_type ⇒ Object
Returns the value of attribute data_type
5 6 7 |
# File 'lib/facetious.rb', line 5 def data_type @data_type end |
#field_name ⇒ Object
Returns the value of attribute field_name
5 6 7 |
# File 'lib/facetious.rb', line 5 def field_name @field_name end |
#name ⇒ Object
Returns the value of attribute name
5 6 7 |
# File 'lib/facetious.rb', line 5 def name @name end |
#title ⇒ Object
Returns the value of attribute title
5 6 7 |
# File 'lib/facetious.rb', line 5 def title @title end |
#where ⇒ Object
Returns the value of attribute where
5 6 7 |
# File 'lib/facetious.rb', line 5 def where @where end |
Instance Method Details
#condition_for(value) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 |
# File 'lib/facetious.rb', line 32 def condition_for value name = field_name.to_s # puts "condition_for facet #{name}, value #{value.inspect}" conditions = case value when /\A\s*-\s*\Z/ if where =~ /\s*\(?\s*EXISTS\b/i nil # No condition will be used, rather the EXISTS will be negated. else '('+name+' IS NULL OR '+name+" = '')" end when /\A\s*\*\s*\Z/ "#{name} != ''" # Which also means IS NOT NULL, incidentally when /,/ '('+ value.split(/,/).map(&:strip).map do |alternate| condition_for alternate end.join(' OR ') + ')' when /\A(>=?)(.*)/ # Greater than name + " #{$1} " + sql_value(data_type, $2) when /\A(<=?)(.*)/ # Less than name + " #{$1} " + sql_value(data_type, $2) when /\A~(.*)/ # Near this value # name + ... when /\A(.*)\.\.(.*)\z/ # Between name + " >= " + sql_value(data_type, $1) + " AND " + name + " <= " + sql_value(data_type, $2) when /%/ name + " LIKE " + sql_value(data_type, value) when Array '(' + value.map{|v| condition_for v }*' OR ' + ')' when Range name + " >= " + sql_value(data_type, value.begin.to_s) + " AND " + name + " <= " + sql_value(data_type, value.end.to_s) else # Equals if [:integers, :strings].include?(data_type) name + " IN " + sql_value(data_type, value.to_s) else name + " = " + sql_value(data_type, value.to_s) end end if sql = where if sql.include?('?') if conditions sql.gsub(/\?/, " AND "+conditions) else # Make an EXISTS clause into NOT EXISTS (see above) 'NOT '+sql.gsub(/\?/, '') end else if sql.match /{{.*}}/ name + sql.gsub(/{{.*}}/, sql_value(data_type, value)) else sql + "WHERE\t"+conditions end end else conditions end end |
#sql_value(data_type, value) ⇒ Object
26 27 28 29 30 |
# File 'lib/facetious.rb', line 26 def sql_value data_type, value conversion_proc = ValueConverter[data_type] raise "facet data type #{data_type} is not recognised" unless conversion_proc conversion_proc.call(value) end |