From 01a2ac12b90691315dec7792f98f895819c53e06 Mon Sep 17 00:00:00 2001 From: kennyj Date: Wed, 14 Nov 2012 01:43:34 +0900 Subject: Fix #7619. 0x prefix must be added when assigning hexadecimal string into bit column in Postgresql, because solving ambiguity. --- activerecord/CHANGELOG.md | 4 ++++ .../lib/active_record/connection_adapters/postgresql/cast.rb | 6 ++++-- activerecord/test/cases/adapters/postgresql/datatype_test.rb | 8 +++++++- 3 files changed, 15 insertions(+), 3 deletions(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 61c217857d..52300fd53f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* `0x` prefix must be added when assigning hexadecimal string into `bit` column in PostgreSQL. + + *kennyj* + * Added Statement Cache to allow the caching of a single statement. The cache works by duping the relation returned from yielding a statement, which allows skipping the AST building phase for following executes. The cache returns results in array format. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index 5789a2ae5f..a9ef11aa83 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -28,8 +28,10 @@ module ActiveRecord def string_to_bit(value) case value - when /^[01]*$/ then value # Bit-string notation - when /^[0-9A-F]*$/i then value.hex.to_s(2) # Hexadecimal notation + when /^0x/i + value[2..-1].hex.to_s(2) # Hexadecimal notation + else + value # Bit-string notation end end diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb index 13731beb01..8c17372286 100644 --- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb +++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb @@ -545,7 +545,7 @@ _SQL def test_update_bit_string new_bit_string = '11111111' - new_bit_string_varying = 'FF' + new_bit_string_varying = '0xFF' assert @first_bit_string.bit_string = new_bit_string assert @first_bit_string.bit_string_varying = new_bit_string_varying assert @first_bit_string.save @@ -553,6 +553,12 @@ _SQL assert_equal @first_bit_string.bit_string, new_bit_string assert_equal @first_bit_string.bit_string, @first_bit_string.bit_string_varying end + + def test_invalid_hex_string + new_bit_string = 'FF' + @first_bit_string.bit_string = new_bit_string + assert_raise(ActiveRecord::StatementInvalid) { assert @first_bit_string.save } + end def test_update_oid new_value = 567890 -- cgit v1.2.3