Skip to content

Commit 55edd33

Browse files
committed
Mimic other adapter's quoting for empty strings to integer columns.
* Fixes #164.
1 parent 756f205 commit 55edd33

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* 3.2.9 *
33

44
* The #remove_default_constraint uses #execute_procedure now. Fixes #223. Thanks @gicappa and @clintmiller.
5-
5+
* Mimic other adapters quoting for empty strings passed to integer columns. Fixes #164.
66

77

88
* 3.2.8 *

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def quote(value, column = nil)
1010
case value
1111
when String, ActiveSupport::Multibyte::Chars
1212
if column && column.type == :integer && value.blank?
13-
nil
13+
value.to_i.to_s
1414
elsif column && column.type == :binary
1515
column.class.string_to_binary(value)
1616
elsif value.is_utf8? || (column && column.type == :string)

test/cases/adapter_test_sqlserver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ def setup
377377
@column = Post.columns_hash['id']
378378
end
379379

380-
should "return null for empty string" do
381-
assert_nil @connection.quote('', @column)
380+
should "return 0 for empty string" do
381+
assert_equal '0', @connection.quote('', @column)
382382
end
383383

384384
end

test/cases/base_test_sqlserver.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
require 'cases/sqlserver_helper'
2+
require 'models/post'
3+
require 'models/auto_id'
24

35
class BasicsTest < ActiveRecord::TestCase
46

57
COERCED_TESTS = [:test_column_names_are_escaped]
68

79
include SqlserverCoercedTest
10+
11+
should 'operate as other database adapters when finding primary keys, standards are postgresql adapter' do
12+
assert_nil Post.where(id:'').first
13+
assert_nil Post.where(id:nil).first
14+
assert_raise(ActiveRecord::RecordNotFound) { Post.find('') }
15+
assert_raise(ActiveRecord::RecordNotFound) { Post.find(nil) }
16+
end
817

918
def test_coerced_column_names_are_escaped
1019
assert_equal "[foo]]bar]", ActiveRecord::Base.connection.quote_column_name("foo]bar")

0 commit comments

Comments
 (0)