提交 83633b80 编写于 作者: A Aaron Patterson

avoid creating objects when we can

上级 18a7b767
......@@ -1270,8 +1270,7 @@ def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name
attrs = expand_hash_conditions_for_aggregates(attrs)
table = Arel::Table.new(self.table_name, :engine => arel_engine, :as => default_table_name)
builder = PredicateBuilder.new(arel_engine)
builder.build_from_hash(attrs, table).map{ |b| b.to_sql }.join(' AND ')
PredicateBuilder.build_from_hash(arel_engine, attrs, table).map{ |b| b.to_sql }.join(' AND ')
end
alias_method :sanitize_sql_hash, :sanitize_sql_hash_for_conditions
......
module ActiveRecord
class PredicateBuilder
def initialize(engine)
@engine = engine
end
def build_from_hash(attributes, default_table)
class PredicateBuilder # :nodoc:
def self.build_from_hash(engine, attributes, default_table)
predicates = attributes.map do |column, value|
table = default_table
if value.is_a?(Hash)
table = Arel::Table.new(column, :engine => @engine)
build_from_hash(value, table)
table = Arel::Table.new(column, :engine => engine)
build_from_hash(engine, value, table)
else
column = column.to_s
if column.include?('.')
table_name, column = column.split('.', 2)
table = Arel::Table.new(table_name, :engine => @engine)
table = Arel::Table.new(table_name, :engine => engine)
end
attribute = table[column] || Arel::Attribute.new(table, column)
......@@ -38,6 +33,5 @@ def build_from_hash(attributes, default_table)
predicates.flatten
end
end
end
......@@ -204,7 +204,7 @@ def build_where(opts, other = [])
[@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
when Hash
attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
PredicateBuilder.new(table.engine).build_from_hash(attributes, table)
PredicateBuilder.build_from_hash(table.engine, attributes, table)
else
[opts]
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册