From a0d4c8d1bf2198608e2e319ff833560f88d20855 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 13 Apr 2011 15:35:53 -0700 Subject: using the database adapter to typecast before executing prepared statement --- .../connection_adapters/mysql_adapter.rb | 2 +- .../connection_adapters/postgresql_adapter.rb | 14 +++++++++++++- activerecord/test/cases/binary_test.rb | 19 +++++++++++++++++++ activerecord/test/schema/schema.rb | 1 + 4 files changed, 34 insertions(+), 2 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index f461162dde..085357678c 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -403,7 +403,7 @@ module ActiveRecord end stmt.execute(*binds.map { |col, val| - col ? col.type_cast(val) : val + type_cast(val, col) }) if metadata = stmt.result_metadata cols = cache[:cols] ||= metadata.fetch_fields.map { |field| diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 0884968363..e74ec84e81 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -360,6 +360,18 @@ module ActiveRecord end end + def type_cast(value, column) + return super unless column + + case value + when String + return super unless 'bytea' == column.sql_type + escape_bytea(value) + else + super + end + end + # Quotes strings for use in SQL input. def quote_string(s) #:nodoc: @connection.escape(s) @@ -530,7 +542,7 @@ module ActiveRecord # Clear the queue @connection.get_last_result @connection.send_query_prepared(key, binds.map { |col, val| - col ? col.type_cast(val) : val + type_cast(val, col) }) @connection.block result = @connection.get_last_result diff --git a/activerecord/test/cases/binary_test.rb b/activerecord/test/cases/binary_test.rb index 8545ba97cc..06c14cb108 100644 --- a/activerecord/test/cases/binary_test.rb +++ b/activerecord/test/cases/binary_test.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require "cases/helper" # Without using prepared statements, it makes no sense to test @@ -9,6 +10,24 @@ unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter) class BinaryTest < ActiveRecord::TestCase FIXTURES = %w(flowers.jpg example.log) + def test_mixed_encoding + str = "\x80" + str.force_encoding('ASCII-8BIT') if str.respond_to?(:force_encoding) + + binary = Binary.new :name => 'いただきます!', :data => str + binary.save! + binary.reload + assert_equal str, binary.data + + name = binary.name + + # Mysql adapter doesn't properly encode things, so we have to do it + if current_adapter?(:MysqlAdapter) + name.force_encoding('UTF-8') if name.respond_to?(:force_encoding) + end + assert_equal 'いただきます!', name + end + def test_load_save Binary.delete_all diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 362475de36..ceadb05644 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -67,6 +67,7 @@ ActiveRecord::Schema.define do end create_table :binaries, :force => true do |t| + t.string :name t.binary :data end -- cgit v1.2.3