From 9c87737c9abefdf059a06f1213e8dee2d87f308a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 9 Apr 2014 11:58:44 -0700 Subject: adding a bind value collector --- lib/arel/collectors/bind.rb | 32 ++++++++++++++++++ lib/arel/collectors/sql_string.rb | 5 +-- lib/arel/visitors/to_sql.rb | 5 ++- test/collectors/test_bind_collector.rb | 62 ++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 lib/arel/collectors/bind.rb create mode 100644 test/collectors/test_bind_collector.rb diff --git a/lib/arel/collectors/bind.rb b/lib/arel/collectors/bind.rb new file mode 100644 index 0000000000..4309de75c9 --- /dev/null +++ b/lib/arel/collectors/bind.rb @@ -0,0 +1,32 @@ +module Arel + module Collectors + class Bind + def initialize + @parts = [] + end + + def << str + @parts << str + self + end + + def add_bind bind + @parts << bind + self + 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 + end + end + end +end diff --git a/lib/arel/collectors/sql_string.rb b/lib/arel/collectors/sql_string.rb index 824b0a1712..526de66416 100644 --- a/lib/arel/collectors/sql_string.rb +++ b/lib/arel/collectors/sql_string.rb @@ -16,11 +16,8 @@ module Arel self end - def start; self; end - def finish; self; end - def add_bind bind - self << bind + self << bind.to_s self end end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index fd1332a4db..9cda88454b 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -693,7 +693,10 @@ module Arel def literal o, collector; collector << o.to_s; end - alias :visit_Arel_Nodes_BindParam :literal + def visit_Arel_Nodes_BindParam o, collector + collector.add_bind o + end + alias :visit_Arel_Nodes_SqlLiteral :literal alias :visit_Bignum :literal alias :visit_Fixnum :literal diff --git a/test/collectors/test_bind_collector.rb b/test/collectors/test_bind_collector.rb new file mode 100644 index 0000000000..c7748d7d10 --- /dev/null +++ b/test/collectors/test_bind_collector.rb @@ -0,0 +1,62 @@ +require 'helper' +require 'arel/collectors/bind' + +module Arel + module Collectors + class TestBindCollector < Arel::Test + def setup + @conn = FakeRecord::Base.new + @visitor = Visitors::ToSql.new @conn.connection + super + end + + def collect node + @visitor.accept(node, Collectors::Bind.new) + end + + def compile node + collect(node).value + end + + def ast_with_binds bv + table = Table.new(:users) + manager = Arel::SelectManager.new Table.engine, table + manager.where(table[:age].eq(bv)) + manager.where(table[:name].eq(bv)) + manager.ast + end + + def test_leaves_binds + node = Nodes::BindParam.new 'omg' + list = compile node + assert_equal node, list.first + assert_equal node.class, list.first.class + end + + def test_adds_strings + bv = Nodes::BindParam.new('?') + 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('?') + collector = collect ast_with_binds bv + + values = collector.value + + offsets = values.map.with_index { |v,i| + [v,i] + }.find_all { |(v,i)| 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 + end + end + end +end -- cgit v1.2.3