Permalink
Browse files

Merge pull request #10225 from kennyj/fix_bit_string_problem

Fix bit string problem
  • Loading branch information...
rafaelfranca committed Apr 15, 2013
2 parents 9f290e6 + 9339005 commit a80f04c00e3606a85745cdf07f545175c62bd817
@@ -26,6 +26,13 @@ def string_to_time(string)
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|
@@ -18,6 +18,16 @@ def type_cast(value)
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 @@ def self.registered_type?(name)
# 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'
@@ -545,13 +545,13 @@ def test_update_network_address
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

0 comments on commit a80f04c

Please sign in to comment.