Class DataMapper::Adapters::Sql::Mappings::Conditions
In: lib/data_mapper/adapters/sql/mappings/conditions.rb
lib/data_mapper/adapters/sql/mappings/conditions.rb
Parent: Object

Methods

Classes and Modules

Class DataMapper::Adapters::Sql::Mappings::Conditions::ConditionsError

Public Class methods

[Source]

    # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 7
 7:           def initialize(table, adapter, qualify_columns=false, options={})
 8:             @table = table
 9:             @qualify_columns = qualify_columns
10:             
11:             # BEGIN: Partion out the options hash into general options,
12:             # and conditions.
13:             standard_find_options = adapter.class::FIND_OPTIONS
14:             conditions_hash = {}
15:             
16:             options.each do |key,value|
17:               unless standard_find_options.include?(key) && key != :conditions
18:                 conditions_hash[key] = value
19:               end
20:             end
21:             # END
22:             
23:             @conditions = parse_conditions(conditions_hash)
24:           end

[Source]

    # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 7
 7:           def initialize(table, adapter, qualify_columns=false, options={})
 8:             @table = table
 9:             @qualify_columns = qualify_columns
10:             
11:             # BEGIN: Partion out the options hash into general options,
12:             # and conditions.
13:             standard_find_options = adapter.class::FIND_OPTIONS
14:             conditions_hash = {}
15:             
16:             options.each do |key,value|
17:               unless standard_find_options.include?(key) && key != :conditions
18:                 conditions_hash[key] = value
19:               end
20:             end
21:             # END
22:             
23:             @conditions = parse_conditions(conditions_hash)
24:           end

Public Instance methods

[Source]

     # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 134
134:           def equality_operator(value, default = '=')
135:             case value
136:             when NilClass then 'IS'
137:             when Array then 'IN'
138:             else default
139:             end
140:           end

[Source]

     # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 134
134:           def equality_operator(value, default = '=')
135:             case value
136:             when NilClass then 'IS'
137:             when Array then 'IN'
138:             else default
139:             end
140:           end

expression_to_sql takes a set of arguments, and turns them into a an Array of generated SQL, followed by optional Values to interpolate as SQL-Parameters.

Parameters: clause The name of the column as a Symbol, a raw-SQL String, a Mappings::Column instance, or a Symbol::Operator. value The Value for the condition. collector An Array representing all conditions that is appended to by expression_to_sql

Returns: Undefined Output. The work performed is added to the collector argument. Example:

  conditions = []
  expression_to_sql(:name, 'Bob', conditions)
  => +undefined return value+
  conditions.inspect
  => ["name = ?", 'Bob']

[Source]

     # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 106
106:           def expression_to_sql(clause, value, collector)
107:             case clause
108:             when Symbol::Operator then
109:               operator = case clause.type
110:               when :gt then '>'
111:               when :gte then '>='
112:               when :lt then '<'
113:               when :lte then '<='
114:               when :not then inequality_operator(value)
115:               when :eql then equality_operator(value)
116:               when :like then equality_operator(value, 'LIKE')
117:               when :in then equality_operator(value)
118:               else raise ArgumentError.new('Operator type not supported')
119:               end
120:               #Table[column name] is column.to_sql(true/false based on associations or not)
121:               collector << ["#{@table[clause].to_sql(@qualify_columns)} #{operator} ?", value]
122:             when Symbol then
123:               collector << ["#{@table[clause].to_sql(@qualify_columns)} #{equality_operator(value)} ?", value]
124:             when String then
125:               collector << [clause, *value]
126:             when Mappings::Column then
127:               collector << ["#{clause.to_sql(@qualify_columns)} #{equality_operator(value)} ?", value]
128:             else raise "CAN HAS CRASH? #{clause.inspect}"
129:             end
130:           rescue => e
131:             raise ConditionsError.new(clause, value, e)
132:           end

expression_to_sql takes a set of arguments, and turns them into a an Array of generated SQL, followed by optional Values to interpolate as SQL-Parameters.

Parameters: clause The name of the column as a Symbol, a raw-SQL String, a Mappings::Column instance, or a Symbol::Operator. value The Value for the condition. collector An Array representing all conditions that is appended to by expression_to_sql

Returns: Undefined Output. The work performed is added to the collector argument. Example:

  conditions = []
  expression_to_sql(:name, 'Bob', conditions)
  => +undefined return value+
  conditions.inspect
  => ["name = ?", 'Bob']

[Source]

     # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 106
106:           def expression_to_sql(clause, value, collector)
107:             case clause
108:             when Symbol::Operator then
109:               operator = case clause.type
110:               when :gt then '>'
111:               when :gte then '>='
112:               when :lt then '<'
113:               when :lte then '<='
114:               when :not then inequality_operator(value)
115:               when :eql then equality_operator(value)
116:               when :like then equality_operator(value, 'LIKE')
117:               when :in then equality_operator(value)
118:               else raise ArgumentError.new('Operator type not supported')
119:               end
120:               #Table[column name] is column.to_sql(true/false based on associations or not)
121:               collector << ["#{@table[clause].to_sql(@qualify_columns)} #{operator} ?", value]
122:             when Symbol then
123:               collector << ["#{@table[clause].to_sql(@qualify_columns)} #{equality_operator(value)} ?", value]
124:             when String then
125:               collector << [clause, *value]
126:             when Mappings::Column then
127:               collector << ["#{clause.to_sql(@qualify_columns)} #{equality_operator(value)} ?", value]
128:             else raise "CAN HAS CRASH? #{clause.inspect}"
129:             end
130:           rescue => e
131:             raise ConditionsError.new(clause, value, e)
132:           end

[Source]

     # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 142
142:           def inequality_operator(value, default = '<>')
143:             case value
144:             when NilClass then 'IS NOT'
145:             when Array then 'NOT IN'
146:             else default
147:             end
148:           end

[Source]

     # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 142
142:           def inequality_operator(value, default = '<>')
143:             case value
144:             when NilClass then 'IS NOT'
145:             when Array then 'NOT IN'
146:             else default
147:             end
148:           end

[Source]

    # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 59
59:           def parse_conditions(conditions_hash)
60:             collection = []
61: 
62:             case x = conditions_hash.delete(:conditions)
63:             when Array then
64:               # DO NOT mutate incoming Array values!!!
65:               # Otherwise the mutated version may impact all the
66:               # way up to the options passed to the finders,
67:               # and have unintended side-effects.
68:               array_copy = x.dup
69:               clause = array_copy.shift
70:               expression_to_sql(clause, array_copy, collection)
71:             when Hash then
72:               x.each_pair do |key,value|
73:                 expression_to_sql(key, value, collection)
74:               end
75:             else
76:               raise "Unable to parse conditions: #{x.inspect}" if x
77:             end
78:             
79:             if @table.paranoid?
80:               conditions_hash[@table.paranoid_column.name] = nil
81:             end
82:             
83:             conditions_hash.each_pair do |key,value|
84:               expression_to_sql(key, value, collection)
85:             end
86: 
87:             collection              
88:           end

[Source]

    # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 59
59:           def parse_conditions(conditions_hash)
60:             collection = []
61: 
62:             case x = conditions_hash.delete(:conditions)
63:             when Array then
64:               # DO NOT mutate incoming Array values!!!
65:               # Otherwise the mutated version may impact all the
66:               # way up to the options passed to the finders,
67:               # and have unintended side-effects.
68:               array_copy = x.dup
69:               clause = array_copy.shift
70:               expression_to_sql(clause, array_copy, collection)
71:             when Hash then
72:               x.each_pair do |key,value|
73:                 expression_to_sql(key, value, collection)
74:               end
75:             else
76:               raise "Unable to parse conditions: #{x.inspect}" if x
77:             end
78:             
79:             if @table.paranoid?
80:               conditions_hash[@table.paranoid_column.name] = nil
81:             end
82:             
83:             conditions_hash.each_pair do |key,value|
84:               expression_to_sql(key, value, collection)
85:             end
86: 
87:             collection              
88:           end

Generate a statement after ‘WHERE’ based on the initialization arguments.

[Source]

    # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 28
28:           def to_params_sql
29:             parameters = []
30:             sql = ""
31:           
32:             unless @conditions.empty?
33:               sql << ' WHERE ('
34:             
35:               last_index = @conditions.size
36:               current_index = 0
37: 
38:               @conditions.each do |condition|
39:                 case condition
40:                 when String then sql << condition
41:                 when Array then
42:                     sql << condition.shift
43:                     parameters += condition
44:                 else
45:                   raise "Unable to parse condition: #{condition.inspect}" if condition
46:                 end
47:               
48:                 if (current_index += 1) == last_index
49:                   sql << ')'
50:                 else
51:                   sql << ') AND ('
52:                 end
53:               end
54:             end
55:             
56:             parameters.unshift(sql)
57:           end

Generate a statement after ‘WHERE’ based on the initialization arguments.

[Source]

    # File lib/data_mapper/adapters/sql/mappings/conditions.rb, line 28
28:           def to_params_sql
29:             parameters = []
30:             sql = ""
31:           
32:             unless @conditions.empty?
33:               sql << ' WHERE ('
34:             
35:               last_index = @conditions.size
36:               current_index = 0
37: 
38:               @conditions.each do |condition|
39:                 case condition
40:                 when String then sql << condition
41:                 when Array then
42:                     sql << condition.shift
43:                     parameters += condition
44:                 else
45:                   raise "Unable to parse condition: #{condition.inspect}" if condition
46:                 end
47:               
48:                 if (current_index += 1) == last_index
49:                   sql << ')'
50:                 else
51:                   sql << ') AND ('
52:                 end
53:               end
54:             end
55:             
56:             parameters.unshift(sql)
57:           end

[Validate]