From 9a0d92340c0b6e64c56a10114536b98370675d95 Mon Sep 17 00:00:00 2001 From: Miles Georgi Date: Tue, 19 Aug 2008 04:25:44 -0700 Subject: [PATCH] allows kim tom's bytea fix to work with postgres-pr and other adapters that don't provide unescape_bytea --- .../connection_adapters/postgresql_adapter.rb | 27 +++++++++++++++++++- 1 files changed, 26 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index b912b63..33aff14 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -923,7 +923,7 @@ module ActiveRecord 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]) + row[cell_index] = unescape_bytea(row[cell_index]) end hashed_row[fields[cell_index]] = column @@ -963,6 +963,31 @@ module ActiveRecord ORDER BY a.attnum end_sql end + + # Converts a bytea escaped string into the binary value it represents + def unescape_bytea(value) + if PGconn.respond_to?(:unescape_bytea) + PGconn.unescape_bytea(value) + else + 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 + end + end end end end -- 1.5.6.1.1071.g76fb