aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-15 11:34:50 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-04-15 11:34:50 -0700
commita80f04c00e3606a85745cdf07f545175c62bd817 (patch)
treeb626c32f118205f094e94323ab53230db15feb7d
parent9f290e6e677575012c4d00bc698e723a134478f6 (diff)
parent9339005bb5504839e2a868f9510f0610da657400 (diff)
downloadrails-a80f04c00e3606a85745cdf07f545175c62bd817.tar.gz
rails-a80f04c00e3606a85745cdf07f545175c62bd817.tar.bz2
rails-a80f04c00e3606a85745cdf07f545175c62bd817.zip
Merge pull request #10225 from kennyj/fix_bit_string_problem
Fix bit string problem
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/cast.rb7
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid.rb14
-rw-r--r--activerecord/test/cases/adapters/postgresql/datatype_test.rb6
3 files changed, 22 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
index 14ef07a75e..5789a2ae5f 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
@@ -26,6 +26,13 @@ module ActiveRecord
end
end
+ 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
+ end
+ end
+
def hstore_to_string(object)
if Hash === object
object.map { |k,v|
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
index 14c97dcefc..1be116ce10 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -18,6 +18,16 @@ module ActiveRecord
end
end
+ class Bit < Type
+ def type_cast(value)
+ if String === value
+ ConnectionAdapters::PostgreSQLColumn.string_to_bit value
+ else
+ value
+ end
+ end
+ end
+
class Bytea < Type
def type_cast(value)
return if value.nil?
@@ -323,14 +333,14 @@ module ActiveRecord
# FIXME: why are we keeping these types as strings?
alias_type 'tsvector', 'text'
alias_type 'interval', 'text'
- alias_type 'bit', 'text'
- alias_type 'varbit', 'text'
alias_type 'macaddr', 'text'
alias_type 'uuid', 'text'
register_type 'money', OID::Money.new
register_type 'bytea', OID::Bytea.new
register_type 'bool', OID::Boolean.new
+ register_type 'bit', OID::Bit.new
+ register_type 'varbit', OID::Bit.new
register_type 'float4', OID::Float.new
alias_type 'float8', 'float4'
diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
index 1e6ae85a25..13731beb01 100644
--- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb
@@ -545,13 +545,13 @@ _SQL
def test_update_bit_string
new_bit_string = '11111111'
- new_bit_string_varying = '11111110'
+ new_bit_string_varying = 'FF'
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
assert @first_bit_string.reload
- assert_equal new_bit_string, @first_bit_string.bit_string
- assert_equal new_bit_string_varying, @first_bit_string.bit_string_varying
+ 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_update_oid