Skip to content

Commit 444b0f9

Browse files
committed
Fixed bind type casting to re-set the casted value back to the bind, fixed bind parameter tests to work with SQL server
1 parent 9ff57a9 commit 444b0f9

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,11 @@ def do_exec_query(sql, name, binds, options = {})
334334
next if ar_column && column.sql_type == 'timestamp'
335335
v = value
336336
names_and_types << if ar_column
337-
v = value.to_i if column.is_integer? && value.present?
337+
if column.is_integer? && value.present?
338+
v = value.to_i
339+
# Reset the casted value to the bind as required by Rails 4.1
340+
binds[index] = [column, v]
341+
end
338342
"@#{index} #{column.sql_type_for_statement}"
339343
elsif column.acts_like?(:string)
340344
"@#{index} nvarchar(max)"
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
require 'cases/sqlserver_helper'
22
require 'models/topic'
3+
require 'models_sqlserver/topic'
34

4-
class BindParameterTestSqlserver < ActiveRecord::TestCase
5-
end
6-
7-
class ActiveRecord::BindParameterTest < ActiveRecord::TestCase
8-
9-
fixtures :topics
5+
class BindParameterTestSqlServer < ActiveRecord::TestCase
106

117
COERCED_TESTS = [
12-
:test_binds_are_logged
8+
:test_binds_are_logged,
9+
:test_binds_are_logged_after_type_cast
1310
]
1411

1512
include SqlserverCoercedTest
1613

17-
# TODO: put a real test here
14+
fixtures :topics
15+
16+
class LogListener
17+
attr_accessor :calls
18+
19+
def initialize
20+
@calls = []
21+
end
22+
23+
def call(*args)
24+
calls << args
25+
end
26+
end
27+
28+
def setup
29+
super
30+
@connection = ActiveRecord::Base.connection
31+
@listener = LogListener.new
32+
@pk = Topic.columns.find { |c| c.primary }
33+
ActiveSupport::Notifications.subscribe('sql.active_record', @listener)
34+
end
35+
36+
def teardown
37+
ActiveSupport::Notifications.unsubscribe(@listener)
38+
end
39+
1840
def test_coerced_binds_are_logged
19-
assert true, 'they are!'
41+
sub = @connection.substitute_at(@pk, 0)
42+
binds = [[@pk, 1]]
43+
sql = "select * from topics where id = #{sub}"
44+
45+
@connection.exec_query(sql, 'SQL', binds)
46+
47+
message = @listener.calls.find { |args| args[4][:sql].include? sql }
48+
assert_equal binds, message[4][:binds]
2049
end
2150

51+
def test_coerced_binds_are_logged_after_type_cast
52+
sub = @connection.substitute_at(@pk, 0)
53+
binds = [[@pk, "3"]]
54+
sql = "select * from topics where id = #{sub}"
2255

23-
end
56+
@connection.exec_query(sql, 'SQL', binds)
2457

58+
message = @listener.calls.find { |args| args[4][:sql].include? sql }
59+
assert_equal [[@pk, 3]], message[4][:binds]
60+
end
2561

62+
end

0 commit comments

Comments
 (0)