aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-15 13:48:39 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-15 13:48:39 +0000
commitad63c96ff217ac20b561fd99d91c43859657250a (patch)
treec8f09c416caea042e14a631468382b167098560d /activerecord/lib/active_record/connection_adapters
parent2997f9c7889f1afd7b1ee245f7289a781a5586dc (diff)
downloadrails-ad63c96ff217ac20b561fd99d91c43859657250a.tar.gz
rails-ad63c96ff217ac20b561fd99d91c43859657250a.tar.bz2
rails-ad63c96ff217ac20b561fd99d91c43859657250a.zip
Fixed binary support for PostgreSQL #444 [alex@byzantine.no]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@410 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb30
1 files changed, 29 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 359cb067a2..ef939d747b 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -37,6 +37,7 @@ module ActiveRecord
end
module ConnectionAdapters
+
class PostgreSQLAdapter < AbstractAdapter # :nodoc:
def select_all(sql, name = nil)
select(sql, name)
@@ -76,6 +77,14 @@ module ActiveRecord
def commit_db_transaction() execute "COMMIT" end
def rollback_db_transaction() execute "ROLLBACK" end
+ def quote(value, column = nil)
+ if value.class == String && column && column.type == :binary
+ quote_bytea(value)
+ else
+ super
+ end
+ end
+
def quote_column_name(name)
return "\"#{name}\""
end
@@ -96,13 +105,31 @@ module ActiveRecord
fields = res.fields
results.each do |row|
hashed_row = {}
- row.each_index { |cel_index| hashed_row[fields[cel_index]] = row[cel_index] }
+ row.each_index do |cel_index|
+ column = row[cel_index]
+ if res.type(cel_index) == 17 # type oid for bytea
+ column = unescape_bytea(column)
+ end
+ hashed_row[fields[cel_index]] = column
+ end
rows << hashed_row
end
end
return rows
end
+ def quote_bytea(s)
+ "'#{escape_bytea(s)}'"
+ end
+
+ def escape_bytea(s)
+ s.gsub(/\\/) { '\\\\\\\\' }.gsub(/[^\\]/) { |c| sprintf('\\\\%03o', c[0].to_i) }
+ end
+
+ def unescape_bytea(s)
+ s.gsub(/\\([0-9][0-9][0-9])/) { $1.oct.chr }.gsub(/\\\\/) { '\\' }
+ end
+
def split_table_schema(table_name)
schema_split = table_name.split('.')
schema_name = "public"
@@ -141,6 +168,7 @@ module ActiveRecord
when 'numeric', 'real', 'money' then 'float'
when 'character varying', 'interval' then 'string'
when 'timestamp without time zone' then 'datetime'
+ when 'bytea' then 'binary'
else field_type
end