diff options
-rw-r--r-- | lib/arel/collectors/bind.rb | 24 | ||||
-rw-r--r-- | lib/arel/collectors/composite.rb | 32 | ||||
-rw-r--r-- | test/collectors/test_bind.rb | 40 | ||||
-rw-r--r-- | test/collectors/test_composite.rb | 47 |
4 files changed, 143 insertions, 0 deletions
diff --git a/lib/arel/collectors/bind.rb b/lib/arel/collectors/bind.rb new file mode 100644 index 0000000000..d816aed90d --- /dev/null +++ b/lib/arel/collectors/bind.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Arel + module Collectors + class Bind + def initialize + @binds = [] + end + + def << str + self + end + + def add_bind bind + @binds << bind + self + end + + def value + @binds + end + end + end +end diff --git a/lib/arel/collectors/composite.rb b/lib/arel/collectors/composite.rb new file mode 100644 index 0000000000..4f6156fe27 --- /dev/null +++ b/lib/arel/collectors/composite.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Arel + module Collectors + class Composite + def initialize(left, right) + @left = left + @right = right + end + + def << str + left << str + right << str + self + end + + def add_bind bind, &block + left.add_bind bind, &block + right.add_bind bind, &block + self + end + + def value + [left.value, right.value] + end + + protected + + attr_reader :left, :right + end + end +end diff --git a/test/collectors/test_bind.rb b/test/collectors/test_bind.rb new file mode 100644 index 0000000000..6b4b651cf7 --- /dev/null +++ b/test/collectors/test_bind.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true +require 'helper' + +require 'arel/collectors/bind' + +module Arel + module Collectors + class TestBind < 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 bvs + table = Table.new(:users) + manager = Arel::SelectManager.new table + manager.where(table[:age].eq(Nodes::BindParam.new(bvs.shift))) + manager.where(table[:name].eq(Nodes::BindParam.new(bvs.shift))) + manager.ast + end + + def test_compile_gathers_all_bind_params + binds = compile(ast_with_binds(["hello", "world"])) + assert_equal ["hello", "world"], binds + + binds = compile(ast_with_binds(["hello2", "world3"])) + assert_equal ["hello2", "world3"], binds + end + end + end +end diff --git a/test/collectors/test_composite.rb b/test/collectors/test_composite.rb new file mode 100644 index 0000000000..b47c37db73 --- /dev/null +++ b/test/collectors/test_composite.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true +require 'helper' + +require 'arel/collectors/bind' +require 'arel/collectors/composite' +require 'arel/collectors/sql_string' + +module Arel + module Collectors + class TestComposite < Arel::Test + def setup + @conn = FakeRecord::Base.new + @visitor = Visitors::ToSql.new @conn.connection + super + end + + def collect node + sql_collector = Collectors::SQLString.new + bind_collector = Collectors::Bind.new + collector = Collectors::Composite.new(sql_collector, bind_collector) + @visitor.accept(node, collector) + end + + def compile node + collect(node).value + end + + def ast_with_binds bvs + table = Table.new(:users) + manager = Arel::SelectManager.new table + manager.where(table[:age].eq(Nodes::BindParam.new(bvs.shift))) + manager.where(table[:name].eq(Nodes::BindParam.new(bvs.shift))) + manager.ast + end + + def test_composite_collector_performs_multiple_collections_at_once + sql, binds = compile(ast_with_binds(["hello", "world"])) + assert_equal 'SELECT FROM "users" WHERE "users"."age" = ? AND "users"."name" = ?', sql + assert_equal ["hello", "world"], binds + + sql, binds = compile(ast_with_binds(["hello2", "world3"])) + assert_equal 'SELECT FROM "users" WHERE "users"."age" = ? AND "users"."name" = ?', sql + assert_equal ["hello2", "world3"], binds + end + end + end +end |