diff options
-rw-r--r-- | lib/arel/collectors/substitute_binds.rb | 29 | ||||
-rw-r--r-- | lib/arel/visitors/oracle.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/oracle12.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/postgresql.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 2 | ||||
-rw-r--r-- | test/collectors/test_substitute_bind_collector.rb | 66 |
6 files changed, 35 insertions, 68 deletions
diff --git a/lib/arel/collectors/substitute_binds.rb b/lib/arel/collectors/substitute_binds.rb index 62589c44e8..99d2215aaa 100644 --- a/lib/arel/collectors/substitute_binds.rb +++ b/lib/arel/collectors/substitute_binds.rb @@ -2,36 +2,27 @@ module Arel module Collectors class SubstituteBinds - def initialize - @parts = [] + def initialize(quoter, delegate_collector) + @quoter = quoter + @delegate = delegate_collector end def << str - @parts << str + delegate << str self end def add_bind bind - @parts << bind - self + self << quoter.quote(bind) end - def value; @parts; end - - def substitute_binds bvs - bvs = bvs.dup - @parts.map do |val| - if Arel::Nodes::BindParam === val - bvs.shift - else - val - end - end + def value + delegate.value end - def compile bvs - substitute_binds(bvs).join - end + protected + + attr_reader :quoter, :delegate end end end diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb index 3b452836db..c1e41be66f 100644 --- a/lib/arel/visitors/oracle.rb +++ b/lib/arel/visitors/oracle.rb @@ -145,7 +145,7 @@ module Arel end def visit_Arel_Nodes_BindParam o, collector - collector.add_bind(o) { |i| ":a#{i}" } + collector.add_bind(o.value) { |i| ":a#{i}" } end end diff --git a/lib/arel/visitors/oracle12.rb b/lib/arel/visitors/oracle12.rb index ce90e994ae..648047ae61 100644 --- a/lib/arel/visitors/oracle12.rb +++ b/lib/arel/visitors/oracle12.rb @@ -53,7 +53,7 @@ module Arel end def visit_Arel_Nodes_BindParam o, collector - collector.add_bind(o) { |i| ":a#{i}" } + collector.add_bind(o.value) { |i| ":a#{i}" } end end end diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index f0991a2f11..2a935e4318 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -46,7 +46,7 @@ module Arel end def visit_Arel_Nodes_BindParam o, collector - collector.add_bind(o) { |i| "$#{i}" } + collector.add_bind(o.value) { |i| "$#{i}" } end def visit_Arel_Nodes_GroupingElement o, collector diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 3f1e390dcc..3ed80c8f06 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -737,7 +737,7 @@ module Arel def literal o, collector; collector << o.to_s; end def visit_Arel_Nodes_BindParam o, collector - collector.add_bind(o) { "?" } + collector.add_bind(o.value) { "?" } end alias :visit_Arel_Nodes_SqlLiteral :literal diff --git a/test/collectors/test_substitute_bind_collector.rb b/test/collectors/test_substitute_bind_collector.rb index adcaf98319..de3e3655da 100644 --- a/test/collectors/test_substitute_bind_collector.rb +++ b/test/collectors/test_substitute_bind_collector.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'helper' require 'arel/collectors/substitute_binds' +require 'arel/collectors/sql_string' module Arel module Collectors @@ -11,61 +12,36 @@ module Arel super end - def collect node - @visitor.accept(node, Collectors::SubstituteBinds.new) - end - - def compile node - collect(node).value - end - - def ast_with_binds bv + def ast_with_binds table = Table.new(:users) manager = Arel::SelectManager.new table - manager.where(table[:age].eq(bv)) - manager.where(table[:name].eq(bv)) + manager.where(table[:age].eq(Nodes::BindParam.new("hello"))) + manager.where(table[:name].eq(Nodes::BindParam.new("world"))) manager.ast end - def test_leaves_binds - node = Nodes::BindParam.new(nil) - list = compile node - assert_equal node, list.first - assert_equal node.class, list.first.class - end - - def test_adds_strings - bv = Nodes::BindParam.new(nil) - list = compile ast_with_binds bv - assert_operator list.length, :>, 0 - assert_equal bv, list.grep(Nodes::BindParam).first - assert_equal bv.class, list.grep(Nodes::BindParam).first.class - end - - def test_substitute_binds - bv = Nodes::BindParam.new(nil) - collector = collect ast_with_binds bv - - values = collector.value - - offsets = values.map.with_index { |v,i| - [v,i] - }.find_all { |(v,_)| Nodes::BindParam === v }.map(&:last) - - list = collector.substitute_binds ["hello", "world"] - assert_equal "hello", list[offsets[0]] - assert_equal "world", list[offsets[1]] - - assert_equal 'SELECT FROM "users" WHERE "users"."age" = hello AND "users"."name" = world', list.join + def compile(node, quoter) + collector = Collectors::SubstituteBinds.new(quoter, Collectors::SQLString.new) + @visitor.accept(node, collector).value end def test_compile - bv = Nodes::BindParam.new(nil) - collector = collect ast_with_binds bv - - sql = collector.compile ["hello", "world"] + quoter = Object.new + def quoter.quote(val) + val.to_s + end + sql = compile(ast_with_binds, quoter) assert_equal 'SELECT FROM "users" WHERE "users"."age" = hello AND "users"."name" = world', sql end + + def test_quoting_is_delegated_to_quoter + quoter = Object.new + def quoter.quote(val) + val.inspect + end + sql = compile(ast_with_binds, quoter) + assert_equal 'SELECT FROM "users" WHERE "users"."age" = "hello" AND "users"."name" = "world"', sql + end end end end |