diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-27 12:07:06 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-27 12:07:06 -0700 |
commit | 6c235dd95812a7c4e8123b6d34d6f3433805ee1c (patch) | |
tree | 9c148b9d42c13a88fe646494529d9f756e391405 /activerecord/lib/active_record/relation | |
parent | 102a5272c5944a6f715da8332d18e7e0380727d1 (diff) | |
download | rails-6c235dd95812a7c4e8123b6d34d6f3433805ee1c.tar.gz rails-6c235dd95812a7c4e8123b6d34d6f3433805ee1c.tar.bz2 rails-6c235dd95812a7c4e8123b6d34d6f3433805ee1c.zip |
Use an `Attribute` object to represent a bind value
The column is primarily used for type casting, which we're trying to
separate from the idea of a column. Since what we really need is the
combination of a name, type, and value, let's use the object that we
already have to represent that concept, rather than this tuple. No
consumers of the bind values have been changed, only the producers
(outside of tests which care too much about internals). This is
*finally* possible since the bind values are now produced from a
reasonable number of lcoations.
Diffstat (limited to 'activerecord/lib/active_record/relation')
4 files changed, 18 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/relation/from_clause.rb b/activerecord/lib/active_record/relation/from_clause.rb index 8022901524..a93952fa30 100644 --- a/activerecord/lib/active_record/relation/from_clause.rb +++ b/activerecord/lib/active_record/relation/from_clause.rb @@ -10,7 +10,7 @@ module ActiveRecord def binds if value.is_a?(Relation) - value.bind_values + value.bound_attributes else [] end diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 90790322b4..7b605db88c 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -104,11 +104,11 @@ module ActiveRecord result[column_name] = attrs binds += bvs when Relation - binds += value.bind_values + binds += value.bound_attributes else if can_be_bound?(column_name, value) result[column_name] = Arel::Nodes::BindParam.new - binds.push([table.column(column_name), value]) + binds << Attribute.with_cast_value(column_name.to_s, value, table.type(column_name)) end end end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index a270f23d7d..bc5f126a23 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -92,10 +92,20 @@ module ActiveRecord CODE end - def bind_values + def bound_attributes from_clause.binds + arel.bind_values + where_clause.binds + having_clause.binds end + def bind_values + # convert to old style + bound_attributes.map do |attribute| + if attribute.name + column = ConnectionAdapters::Column.new(attribute.name, nil, attribute.type) + end + [column, attribute.value_before_type_cast] + end + end + def create_with_value # :nodoc: @values[:create_with] || {} end diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index 1a70dc447e..ae5667dfd6 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -39,7 +39,7 @@ module ActiveRecord end end - binds = self.binds.select(&:first).to_h.transform_keys(&:name) + binds = self.binds.map { |attr| [attr.name, attr.value] }.to_h equalities.map { |node| name = node.left.name @@ -97,7 +97,7 @@ module ActiveRecord def non_conflicting_binds(other) conflicts = referenced_columns & other.referenced_columns conflicts.map! { |node| node.name.to_s } - binds.reject { |col, _| conflicts.include?(col.name) } + binds.reject { |attr| conflicts.include?(attr.name) } end def inverted_predicates @@ -130,8 +130,8 @@ module ActiveRecord end def binds_except(columns) - binds.reject do |column, _| - columns.include?(column.name) + binds.reject do |attr| + columns.include?(attr.name) end end |