diff options
Diffstat (limited to 'activerecord')
11 files changed, 38 insertions, 41 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index c1ef86a95b..3a184cee37 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -25,7 +25,7 @@ module ActiveRecord def join_constraints(foreign_table, foreign_klass, node, join_type, tables, scope_chain, chain) joins = [] - bind_values = [] + binds = [] tables = tables.reverse scope_chain_index = 0 @@ -66,7 +66,7 @@ module ActiveRecord end if rel && !rel.arel.constraints.empty? - bind_values.concat rel.bind_values + binds += rel.bound_attributes constraint = constraint.and rel.arel.constraints end @@ -75,7 +75,7 @@ module ActiveRecord column = klass.columns_hash[reflection.type.to_s] substitute = klass.connection.substitute_at(column) - bind_values.push [column, value] + binds << Attribute.with_cast_value(column.name, value, klass.type_for_attribute(column.name)) constraint = constraint.and table[reflection.type].eq substitute end @@ -85,7 +85,7 @@ module ActiveRecord foreign_table, foreign_klass = table, klass end - JoinInformation.new joins, bind_values + JoinInformation.new joins, binds end # Builds equality condition. 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 diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 3707fd3f04..6c8792ee80 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -22,12 +22,11 @@ module ActiveRecord arel_table[column_name] end - def column(column_name) + def type(column_name) if klass - klass.columns_hash[column_name.to_s] + klass.type_for_attribute(column_name.to_s) else - # FIXME: We really shouldn't need to do this. - ConnectionAdapters::Column.new(column_name.to_s, nil, Type::Value.new) + Type::Value.new end end diff --git a/activerecord/test/cases/associations/association_scope_test.rb b/activerecord/test/cases/associations/association_scope_test.rb index fc5dbd0890..472e270f8c 100644 --- a/activerecord/test/cases/associations/association_scope_test.rb +++ b/activerecord/test/cases/associations/association_scope_test.rb @@ -8,7 +8,7 @@ module ActiveRecord test 'does not duplicate conditions' do scope = AssociationScope.scope(Author.new.association(:welcome_posts), Author.connection) - binds = scope.where_clause.binds.map(&:last) + binds = scope.where_clause.binds.map(&:value) assert_equal binds.uniq, binds end end diff --git a/activerecord/test/cases/bind_parameter_test.rb b/activerecord/test/cases/bind_parameter_test.rb index 66663b3e0e..b6ab576d76 100644 --- a/activerecord/test/cases/bind_parameter_test.rb +++ b/activerecord/test/cases/bind_parameter_test.rb @@ -22,8 +22,8 @@ module ActiveRecord def setup super @connection = ActiveRecord::Base.connection - @subscriber = LogListener.new - @pk = Topic.columns_hash[Topic.primary_key] + @subscriber = LogListener.new + @pk = ConnectionAdapters::Column.new(Topic.primary_key, nil, Topic.type_for_attribute(Topic.primary_key)) @subscription = ActiveSupport::Notifications.subscribe('sql.active_record', @subscriber) end diff --git a/activerecord/test/cases/relation/merging_test.rb b/activerecord/test/cases/relation/merging_test.rb index eb76ef6328..fe0854b7b4 100644 --- a/activerecord/test/cases/relation/merging_test.rb +++ b/activerecord/test/cases/relation/merging_test.rb @@ -91,17 +91,6 @@ class RelationMergingTest < ActiveRecord::TestCase assert merged.to_sql.include?("bbq") end - def test_merging_keeps_lhs_bind_parameters - column = Post.columns_hash['id'] - binds = [[column, 20]] - - right = Post.where(id: 20) - left = Post.where(id: 10) - - merged = left.merge(right) - assert_equal binds, merged.bind_values - end - def test_merging_reorders_bind_params post = Post.first right = Post.where(id: 1) diff --git a/activerecord/test/cases/relation/where_clause_test.rb b/activerecord/test/cases/relation/where_clause_test.rb index f3644154b6..db18980e0b 100644 --- a/activerecord/test/cases/relation/where_clause_test.rb +++ b/activerecord/test/cases/relation/where_clause_test.rb @@ -47,15 +47,15 @@ class ActiveRecord::Relation test "merge removes bind parameters matching overlapping equality clauses" do a = WhereClause.new( [table["id"].eq(bind_param), table["name"].eq(bind_param)], - [[column("id"), 1], [column("name"), "Sean"]], + [attribute("id", 1), attribute("name", "Sean")], ) b = WhereClause.new( [table["name"].eq(bind_param)], - [[column("name"), "Jim"]] + [attribute("name", "Jim")] ) expected = WhereClause.new( [table["id"].eq(bind_param), table["name"].eq(bind_param)], - [[column("id"), 1], [column("name"), "Jim"]], + [attribute("id", 1), attribute("name", "Jim")], ) assert_equal expected, a.merge(b) @@ -103,10 +103,10 @@ class ActiveRecord::Relation table["name"].eq(bind_param), table["age"].gteq(bind_param), ], [ - [column("name"), "Sean"], - [column("age"), 30], + attribute("name", "Sean"), + attribute("age", 30), ]) - expected = WhereClause.new([table["age"].gteq(bind_param)], [[column("age"), 30]]) + expected = WhereClause.new([table["age"].gteq(bind_param)], [attribute("age", 30)]) assert_equal expected, where_clause.except("id", "name") end @@ -155,8 +155,8 @@ class ActiveRecord::Relation Arel::Nodes::BindParam.new end - def column(name) - ActiveRecord::ConnectionAdapters::Column.new(name, nil, nil) + def attribute(name, value) + ActiveRecord::Attribute.with_cast_value(name, value, ActiveRecord::Type::Value.new) end end end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index ecb30af12b..1e39a9bd28 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1758,14 +1758,13 @@ class RelationTest < ActiveRecord::TestCase end def test_merging_keeps_lhs_bind_parameters - column = Post.columns_hash['id'] - binds = [[column, 20]] + binds = [ActiveRecord::Attribute.with_cast_value("id", 20, Post.type_for_attribute("id"))] right = Post.where(id: 20) left = Post.where(id: 10) merged = left.merge(right) - assert_equal binds, merged.bind_values + assert_equal binds, merged.bound_attributes end def test_merging_reorders_bind_params |