- E
- S
Accepts a hash of SQL conditions and replaces those attributes that correspond to a composed_of relationship with their expanded aggregate attribute values. Given:
class Person < ActiveRecord::Base composed_of :address, :class_name => "Address", :mapping => [%w(address_street street), %w(address_city city)] end
Then:
{ :address => Address.new("813 abc st.", "chicago") } # => { :address_street => "813 abc st.", :address_city => "chicago" }
# File activerecord/lib/active_record/sanitization.rb, line 57 57: def expand_hash_conditions_for_aggregates(attrs) 58: expanded_attrs = {} 59: attrs.each do |attr, value| 60: unless (aggregation = reflect_on_aggregation(attr.to_sym)).nil? 61: mapping = aggregate_mapping(aggregation) 62: mapping.each do |field_attr, aggregate_attr| 63: if mapping.size == 1 && !value.respond_to?(aggregate_attr) 64: expanded_attrs[field_attr] = value 65: else 66: expanded_attrs[field_attr] = value.send(aggregate_attr) 67: end 68: end 69: else 70: expanded_attrs[attr] = value 71: end 72: end 73: expanded_attrs 74: end
Alias for sanitize_sql
Alias for sanitize_sql_for_conditions
Accepts an array of conditions. The array has each value sanitized and interpolated into the SQL statement.
["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'"
# File activerecord/lib/active_record/sanitization.rb, line 112 112: def sanitize_sql_array(ary) 113: statement, *values = ary 114: if values.first.is_a?(Hash) && statement =~ /:\w+/ 115: replace_named_bind_variables(statement, values.first) 116: elsif statement.include?('?') 117: replace_bind_variables(statement, values) 118: elsif statement.blank? 119: statement 120: else 121: statement % values.collect { |value| connection.quote_string(value.to_s) } 122: end 123: end
Accepts an array, hash, or string of SQL conditions and sanitizes them into a valid SQL fragment for a SET clause.
{ :name => nil, :group_id => 4 } returns "name = NULL , group_id='4'"
# File activerecord/lib/active_record/sanitization.rb, line 38 38: def sanitize_sql_for_assignment(assignments) 39: case assignments 40: when Array; sanitize_sql_array(assignments) 41: when Hash; sanitize_sql_hash_for_assignment(assignments) 42: else assignments 43: end 44: end
Accepts an array, hash, or string of SQL conditions and sanitizes them into a valid SQL fragment for a WHERE clause.
["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'" { :name => "foo'bar", :group_id => 4 } returns "name='foo''bar' and group_id='4'" "name='foo''bar' and group_id='4'" returns "name='foo''bar' and group_id='4'"
# File activerecord/lib/active_record/sanitization.rb, line 24 24: def sanitize_sql_for_conditions(condition, table_name = self.table_name) 25: return nil if condition.blank? 26: 27: case condition 28: when Array; sanitize_sql_array(condition) 29: when Hash; sanitize_sql_hash_for_conditions(condition, table_name) 30: else condition 31: end 32: end
Alias for sanitize_sql_hash_for_conditions
Sanitizes a hash of attribute/value pairs into SQL conditions for a SET clause.
{ :status => nil, :group_id => 1 } # => "status = NULL , group_id = 1"
Sanitizes a hash of attribute/value pairs into SQL conditions for a WHERE clause.
{ :name => "foo'bar", :group_id => 4 } # => "name='foo''bar' and group_id= 4" { :status => nil, :group_id => [1,2,3] } # => "status IS NULL and group_id IN (1,2,3)" { :age => 13..18 } # => "age BETWEEN 13 AND 18" { 'other_records.id' => 7 } # => "`other_records`.`id` = 7" { :other_records => { :id => 7 } } # => "`other_records`.`id` = 7"
And for value objects on a composed_of relationship:
{ :address => Address.new("123 abc st.", "chicago") } # => "address_street='123 abc st.' and address_city='chicago'"
# File activerecord/lib/active_record/sanitization.rb, line 90 90: def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name) 91: attrs = expand_hash_conditions_for_aggregates(attrs) 92: 93: table = Arel::Table.new(table_name).alias(default_table_name) 94: PredicateBuilder.build_from_hash(arel_engine, attrs, table).map { |b| 95: connection.visitor.accept b 96: }.join(' AND ') 97: end