diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2017-07-21 08:33:09 -0400 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2017-07-21 08:33:09 -0400 |
commit | db1bb4e9a728a437d16f8bdb48c3b772c3e4edb0 (patch) | |
tree | f0f7623e9b730679adcb0dfe97f9c8415a91328f /test/visitors | |
parent | f031a3b9aa6a8093802e0188abce58e0b997078e (diff) | |
download | rails-db1bb4e9a728a437d16f8bdb48c3b772c3e4edb0.tar.gz rails-db1bb4e9a728a437d16f8bdb48c3b772c3e4edb0.tar.bz2 rails-db1bb4e9a728a437d16f8bdb48c3b772c3e4edb0.zip |
Add a value field `Nodes::BindParam`
This is part of a greater refactoring to have the `BindParam` nodes hold
onto their values. We want to generally keep the AST decoupled from what
you're actually doing with those values, but ultimately the usage of
`BindParam` is almost identical to how you'd use `Casted` or `Quoted`.
Forcing consumers of Arel's API to maintain the bind values separately
from the AST makes manipulating the AST essentially impossible, as you
would need to perform a full walk of the AST to determine whether a
given node contains bind parameters, and which value it maps to.
By storing the value on the bind parameter directly, we can collect them
in another AST pass (realistically it'll be part of the same pass that
performs SQL construction for performance reasons). This will
dramatically simplify AST manipulation for Rails or any other consumers
that work with bind params.
As part of this change I've removed the `BindVisitor`, which appears to
be dead code, and had tests break from this change.
Diffstat (limited to 'test/visitors')
-rw-r--r-- | test/visitors/test_bind_visitor.rb | 61 | ||||
-rw-r--r-- | test/visitors/test_dot.rb | 2 | ||||
-rw-r--r-- | test/visitors/test_oracle.rb | 8 | ||||
-rw-r--r-- | test/visitors/test_oracle12.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_postgres.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_to_sql.rb | 4 |
6 files changed, 11 insertions, 72 deletions
diff --git a/test/visitors/test_bind_visitor.rb b/test/visitors/test_bind_visitor.rb deleted file mode 100644 index 3e0578a6a1..0000000000 --- a/test/visitors/test_bind_visitor.rb +++ /dev/null @@ -1,61 +0,0 @@ -# frozen_string_literal: true -require 'helper' -require 'arel/visitors/bind_visitor' -require 'support/fake_record' - -module Arel - module Visitors - class TestBindVisitor < Arel::Test - attr_reader :collector - - def setup - @collector = Collectors::SQLString.new - super - end - - ## - # Tests visit_Arel_Nodes_Assignment correctly - # substitutes binds with values from block - def test_assignment_binds_are_substituted - table = Table.new(:users) - um = Arel::UpdateManager.new - bp = Nodes::BindParam.new - um.set [[table[:name], bp]] - visitor = Class.new(Arel::Visitors::ToSql) { - include Arel::Visitors::BindVisitor - }.new Table.engine.connection - - assignment = um.ast.values[0] - actual = visitor.accept(assignment, collector) { - "replace" - } - assert actual - value = actual.value - assert_like "\"name\" = replace", value - end - - def test_visitor_yields_on_binds - visitor = Class.new(Arel::Visitors::ToSql) { - include Arel::Visitors::BindVisitor - }.new nil - - bp = Nodes::BindParam.new - called = false - visitor.accept(bp, collector) { called = true } - assert called - end - - def test_visitor_only_yields_on_binds - visitor = Class.new(Arel::Visitors::ToSql) { - include Arel::Visitors::BindVisitor - }.new(nil) - - bp = Arel.sql 'omg' - called = false - - visitor.accept(bp, collector) { called = true } - refute called - end - end - end -end diff --git a/test/visitors/test_dot.rb b/test/visitors/test_dot.rb index 1d27d1a5cb..8067ff5b49 100644 --- a/test/visitors/test_dot.rb +++ b/test/visitors/test_dot.rb @@ -74,7 +74,7 @@ module Arel end def test_Arel_Nodes_BindParam - node = Arel::Nodes::BindParam.new + node = Arel::Nodes::BindParam.new(nil) collector = Collectors::PlainString.new assert_match '[label="<f0>Arel::Nodes::BindParam"]', @visitor.accept(node, collector).value end diff --git a/test/visitors/test_oracle.rb b/test/visitors/test_oracle.rb index b1921f0cbc..cdadec8b15 100644 --- a/test/visitors/test_oracle.rb +++ b/test/visitors/test_oracle.rb @@ -127,8 +127,8 @@ module Arel it 'creates a subquery when there is limit and offset with BindParams' do stmt = Nodes::SelectStatement.new - stmt.limit = Nodes::Limit.new(Nodes::BindParam.new) - stmt.offset = Nodes::Offset.new(Nodes::BindParam.new) + stmt.limit = Nodes::Limit.new(Nodes::BindParam.new(nil)) + stmt.offset = Nodes::Offset.new(Nodes::BindParam.new(nil)) sql = compile stmt sql.must_be_like %{ SELECT * FROM ( @@ -184,8 +184,8 @@ module Arel describe "Nodes::BindParam" do it "increments each bind param" do - query = @table[:name].eq(Arel::Nodes::BindParam.new) - .and(@table[:id].eq(Arel::Nodes::BindParam.new)) + query = @table[:name].eq(Arel::Nodes::BindParam.new(nil)) + .and(@table[:id].eq(Arel::Nodes::BindParam.new(nil))) compile(query).must_be_like %{ "users"."name" = :a1 AND "users"."id" = :a2 } diff --git a/test/visitors/test_oracle12.rb b/test/visitors/test_oracle12.rb index c908a51d4f..a6bbfe3077 100644 --- a/test/visitors/test_oracle12.rb +++ b/test/visitors/test_oracle12.rb @@ -48,8 +48,8 @@ module Arel describe "Nodes::BindParam" do it "increments each bind param" do - query = @table[:name].eq(Arel::Nodes::BindParam.new) - .and(@table[:id].eq(Arel::Nodes::BindParam.new)) + query = @table[:name].eq(Arel::Nodes::BindParam.new(nil)) + .and(@table[:id].eq(Arel::Nodes::BindParam.new(nil))) compile(query).must_be_like %{ "users"."name" = :a1 AND "users"."id" = :a2 } diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb index 26cc721871..3b4fdc0427 100644 --- a/test/visitors/test_postgres.rb +++ b/test/visitors/test_postgres.rb @@ -176,8 +176,8 @@ module Arel describe "Nodes::BindParam" do it "increments each bind param" do - query = @table[:name].eq(Arel::Nodes::BindParam.new) - .and(@table[:id].eq(Arel::Nodes::BindParam.new)) + query = @table[:name].eq(Arel::Nodes::BindParam.new(nil)) + .and(@table[:id].eq(Arel::Nodes::BindParam.new(nil))) compile(query).must_be_like %{ "users"."name" = $1 AND "users"."id" = $2 } diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index 31279b0ae2..445d9c476c 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -17,13 +17,13 @@ module Arel end it 'works with BindParams' do - node = Nodes::BindParam.new + node = Nodes::BindParam.new(nil) sql = compile node sql.must_be_like '?' end it 'does not quote BindParams used as part of a Values' do - bp = Nodes::BindParam.new + bp = Nodes::BindParam.new(nil) values = Nodes::Values.new([bp]) sql = compile values sql.must_be_like 'VALUES (?)' |