From aa808c08c4da92208a69fc28f5651f4e85974525 Mon Sep 17 00:00:00 2001 From: Kim Toms Date: Mon, 18 Aug 2008 17:01:43 -0400 Subject: [PATCH] Add failing test case for postgresql --- activerecord/test/cases/binary_test.rb | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/binary_test.rb b/activerecord/test/cases/binary_test.rb index 7131532..bb116f0 100644 --- a/activerecord/test/cases/binary_test.rb +++ b/activerecord/test/cases/binary_test.rb @@ -18,6 +18,7 @@ unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :DB2Adapter, :Firebir FIXTURES.each do |filename| data = File.read(ASSETS_ROOT + "/#{filename}") + data = '\\101' + data data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding) data.freeze -- 1.5.4.3 From d4d806408e0ebff6d817735ecefbdbe9fed9e0cf Mon Sep 17 00:00:00 2001 From: Kim Toms Date: Mon, 18 Aug 2008 19:54:01 -0400 Subject: [PATCH] Fix postgresql BYTEA problem, ticket #611 --- .../connection_adapters/postgresql_adapter.rb | 50 ++------------------ 1 files changed, 4 insertions(+), 46 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 8564355..b912b63 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -89,51 +89,7 @@ module ActiveRecord end self.class.string_to_binary(value) end - - # Unescapes bytea output from a database to the binary string it represents. - def self.binary_to_string(value) - # In each case, check if the value actually is escaped PostgreSQL bytea output - # or an unescaped Active Record attribute that was just written. - if PGconn.respond_to?(:unescape_bytea) - self.class.module_eval do - define_method(:binary_to_string) do |value| - if value =~ /\\\d{3}/ - PGconn.unescape_bytea(value) - else - value - end - end - end - else - self.class.module_eval do - define_method(:binary_to_string) do |value| - if value =~ /\\\d{3}/ - result = '' - i, max = 0, value.size - while i < max - char = value[i] - if char == ?\\ - if value[i+1] == ?\\ - char = ?\\ - i += 1 - else - char = value[i+1..i+3].oct - i += 3 - end - end - result << char - i += 1 - end - result - else - value - end - end - end - end - self.class.binary_to_string(value) - end - + # Maps PostgreSQL-specific data types to logical Rails types. def simplified_type(field_type) case field_type @@ -869,7 +825,7 @@ module ActiveRecord private # The internal PostgreSQL identifier of the money data type. MONEY_COLUMN_TYPE_OID = 790 #:nodoc: - + BYTEA_COLUMN_TYPE_OID = 17 #:nodoc: # Connects to a PostgreSQL server and sets up the adapter depending on the # connected server's characteristics. def connect @@ -966,6 +922,8 @@ module ActiveRecord when /^-?\D+[\d\.]+,\d{2}$/ # (2) row[cell_index] = column.gsub(/[^-\d,]/, '').sub(/,/, '.') end + elsif res.ftype(cell_index) == BYTEA_COLUMN_TYPE_OID + row[cell_index] = PGconn.unescape_bytea(row[cell_index]) end hashed_row[fields[cell_index]] = column -- 1.5.4.3