Skip to content

Commit

Permalink
Merge pull request #161 from Noemj/master
Browse files Browse the repository at this point in the history
Mysql2 prepared statements bind substitution fix
  • Loading branch information
tenderlove committed Feb 5, 2013
2 parents d8005bc + 30a74c0 commit 80f11e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/arel/visitors/bind_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ def accept node, &block
end

private

def visit_Arel_Nodes_Assignment o
if o.right.is_a? Arel::Nodes::BindParam
"#{visit o.left} = #{visit o.right}"
else
super
end
end

def visit_Arel_Nodes_BindParam o
if @block
@block.call
else
super
end
end

end
end
end
21 changes: 20 additions & 1 deletion test/visitors/test_bind_visitor.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
require 'helper'
require 'arel/visitors/bind_visitor'
require 'support/fake_record'

module Arel
module Visitors
class TestBindVisitor < MiniTest::Unit::TestCase
class TestBindVisitor < MiniTest::Unit::TestCase

##
# 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 Table.engine
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) { "replace" }
actual.must_be_like "\"name\" = replace"
end

def test_visitor_yields_on_binds
visitor = Class.new(Arel::Visitors::Visitor) {
def initialize omg
Expand Down

0 comments on commit 80f11e3

Please sign in to comment.