From d5411fb37ca9a35f49a55fd52d2aefb7956b3e41 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 11 Jul 2014 07:36:03 -0600 Subject: Don't rely on the column SQL type for bit string quoting --- .../connection_adapters/postgresql/oid/bit.rb | 26 ++++++++++++++++++++++ .../connection_adapters/postgresql/quoting.rb | 20 ++++++----------- 2 files changed, 33 insertions(+), 13 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb index 243ecd13cf..1dbb40ca1d 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb @@ -19,6 +19,32 @@ module ActiveRecord value end end + + def type_cast_for_database(value) + Data.new(super) if value + end + + class Data + def initialize(value) + @value = value + end + + def to_s + value + end + + def binary? + /\A[01]*\Z/ === value + end + + def hex? + /\A[0-9A-F]*\Z/i === value + end + + protected + + attr_reader :value + end end end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 5359c5b666..cf5c8d288e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -18,8 +18,6 @@ module ActiveRecord def quote(value, column = nil) #:nodoc: return super unless column - sql_type = type_to_sql(column.type, column.limit, column.precision, column.scale) - case value when Float if value.infinite? || value.nan? @@ -27,16 +25,6 @@ module ActiveRecord else super end - when String - case sql_type - when /^bit/ - case value - when /\A[01]*\Z/ then "B'#{value}'" # Bit-string notation - when /\A[0-9A-F]*\Z/i then "X'#{value}'" # Hexadecimal notation - end - else - super - end else super end @@ -100,6 +88,12 @@ module ActiveRecord "'#{escape_bytea(value.to_s)}'" when OID::Xml::Data "xml '#{quote_string(value.to_s)}'" + when OID::Bit::Data + if value.binary? + "B'#{value}'" + elsif value.hex? + "X'#{value}'" + end else super end @@ -112,7 +106,7 @@ module ActiveRecord # See http://deveiate.org/code/pg/PGconn.html#method-i-exec_prepared-doc # for more information { value: value.to_s, format: 1 } - when OID::Xml::Data + when OID::Xml::Data, OID::Bit::Data value.to_s else super -- cgit v1.2.3