Class: DataMapper::Queries::Conditions

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/queries/conditions.rb

Defined Under Namespace

Classes: NormalizationError

Instance Method Summary collapse

Constructor Details

#initialize(database, options) ⇒ Conditions

Returns a new instance of Conditions.



6
7
8
9
# File 'lib/data_mapper/queries/conditions.rb', line 6

def initialize(database, options)
  @database, @options = database, options
  @has_id = false
end

Instance Method Details

#empty? ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/data_mapper/queries/conditions.rb', line 131

def empty?
  normalized_conditions.empty?
end

#escape(conditions) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/data_mapper/queries/conditions.rb', line 83

def escape(conditions)
  clause = conditions.shift

  clause.gsub(/\?/) do |x|
    # Check if the condition is an in, clause.
    case conditions.first
    when Array then
      '(' << conditions.shift.map { |c| @database.quote_value(c) }.join(', ') << ')'
    when SelectStatement then
      '(' << conditions.shift.to_sql << ')'
    else
      @database.quote_value(conditions.shift)
    end
  end
end

#has_id? ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/data_mapper/queries/conditions.rb', line 99

def has_id?
  normalized_conditions
  @has_id
end

#normalize(clause, collector) ⇒ Object



27
28
29
30
31
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
# File 'lib/data_mapper/queries/conditions.rb', line 27

def normalize(clause, collector)
  case clause
    when Hash then
      clause.each_pair do |k,v|
        if k.kind_of?(Symbol::Operator)
          if k.type == :select
            k.options[:class] ||= @options[:class]
            
            k.options[:select] ||= if k.value.to_s == @database[k.options[:class]].default_foreign_key
              @database[k.options[:class]].key.column_name
            else
              k.value
            end
            
            sub_select = @database.select_statement(k.options.merge(v))
            normalize(["#{@database[@options[:class]][k.value.to_sym].to_sql} IN ?", sub_select], collector)
          else                
            @has_id = true if k.value == :id
            op = case k.type
              when :gt then '>'
              when :gte then '>='
              when :lt then '<'
              when :lte then '<='
              when :not then v.nil? ? 'IS NOT' : (v.kind_of?(Array) ? 'NOT IN' : '<>')
              when :eql then v.nil? ? 'IS' : (v.kind_of?(Array) ? 'IN' : '=')
              when :like then 'LIKE'
              when :in then 'IN'
              else raise ArgumentError.new('Operator type not supported')
            end
            normalize(["#{@database[@options[:class]][k.value.to_sym].to_sql} #{op} ?", v], collector)
          end
        else
          @has_id = true if k == :id
          case v
          when Array then
            normalize(["#{@database[@options[:class]][k.to_sym].to_sql} IN ?", v], collector)
          when SelectStatement then
            normalize(["#{@database[@options[:class]][k.to_sym].to_sql} IN ?", v], collector)
          else
            normalize(["#{@database[@options[:class]][k.to_sym].to_sql} = ?", v], collector)
          end
        end
      end
    when Array then
      return collector if clause.empty?
      @has_id = true if clause.first =~ /(^|\s|\`)id(\`|\s|\=|\<)/ && !clause[1].kind_of?(SelectStatement)
      collector << escape(clause)
    when String then
      @has_id = true if clause =~ /(^|\s|\`)id(\`|\s|\=|\<)/
      collector << clause
    else raise NormalizationError.new(clause)              
  end
  
  return collector
end

#normalized_conditions ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/data_mapper/queries/conditions.rb', line 104

def normalized_conditions
  
  if @normalized_conditions.nil?
    @normalized_conditions = []
    
    table = @database[@options[:class]]
    invalid_keys = nil
    
    implicits = @options.reject do |k,v|            
      standard_key = DataMapper::Session::FIND_OPTIONS.include?(k)
      (invalid_keys ||= []) << k if !standard_key && table[k.to_sym].nil?
      standard_key
    end
    
    raise "Invalid options: #{invalid_keys.inspect}" unless invalid_keys.nil?
    
    normalize(implicits, @normalized_conditions)
    
    if @options.has_key?(:conditions)
      normalize(@options[:conditions], @normalized_conditions)
    end
    
  end
  
  return @normalized_conditions          
end

#to_a ⇒ Object



135
136
137
# File 'lib/data_mapper/queries/conditions.rb', line 135

def to_a
  normalized_conditions
end