Remove checks against column.type in abstract adapter quoting

The intention is to eventually remove `column` from the arguments list
both for `quote` and for `type_cast` entirely. This is the first step
to that end.
This commit is contained in:
Sean Griffin 2014-05-25 09:30:13 -07:00
parent d075c84320
commit 597d9c8af8
5 changed files with 33 additions and 54 deletions

@ -9,26 +9,22 @@ def quote(value, column = nil)
# records are quoted as their primary key
return value.quoted_id if value.respond_to?(:quoted_id)
# FIXME: The only case we get an object other than nil or a real column
# is `SchemaStatements#add_column` with a PG array that has a non-empty default
# value. Is this really the only case? Are we missing tests for other types?
# We should have a real column object passed (or nil) here, and check for that
# instead
if column.respond_to?(:type_cast_for_database)
value = column.type_cast_for_database(value)
end
case value
when String, ActiveSupport::Multibyte::Chars
value = value.to_s
return "'#{quote_string(value)}'" unless column
case column.type
when :integer then value.to_i.to_s
when :float then value.to_f.to_s
else
"'#{quote_string(value)}'"
end
when true, false
if column && column.type == :integer
value ? '1' : '0'
else
value ? quoted_true : quoted_false
end
# BigDecimals need to be put in a non-normalized form and quoted.
"'#{quote_string(value.to_s)}'"
when true then quoted_true
when false then quoted_false
when nil then "NULL"
# BigDecimals need to be put in a non-normalized form and quoted.
when BigDecimal then value.to_s('F')
when Numeric, ActiveSupport::Duration then value.to_s
when Date, Time then "'#{quoted_date(value)}'"
@ -58,24 +54,11 @@ def type_cast(value, column)
case value
when String, ActiveSupport::Multibyte::Chars
value = value.to_s
return value unless column
case column.type
when :integer then value.to_i
when :float then value.to_f
else
value
end
when true, false
if column && column.type == :integer
value ? 1 : 0
else
value ? 't' : 'f'
end
# BigDecimals need to be put in a non-normalized form and quoted.
value.to_s
when true then 't'
when false then 'f'
when nil then nil
# BigDecimals need to be put in a non-normalized form and quoted.
when BigDecimal then value.to_s('F')
when Numeric then value
when Date, Time then quoted_date(value)

@ -12,6 +12,8 @@ def klass
::Float
end
alias type_cast_for_database type_cast
private
def cast_value(value)

@ -12,6 +12,8 @@ def klass
::Fixnum
end
alias type_cast_for_database type_cast
private
def cast_value(value)

@ -15,10 +15,10 @@ def setup
def test_type_cast_binary_encoding_without_logger
@conn.extend(Module.new { def logger; end })
column = Struct.new(:type, :name).new(:string, "foo")
cast_type = Type::String.new
binary = SecureRandom.hex
expected = binary.dup.encode!(Encoding::UTF_8)
assert_equal expected, @conn.type_cast(binary, column)
assert_equal expected, @conn.type_cast(binary, cast_type)
end
def test_type_cast_symbol

@ -3,14 +3,6 @@
module ActiveRecord
module ConnectionAdapters
class QuotingTest < ActiveRecord::TestCase
class FakeColumn < ActiveRecord::ConnectionAdapters::Column
attr_accessor :type
def initialize type
@type = type
end
end
def setup
@quoter = Class.new { include Quoting }.new
end
@ -101,12 +93,12 @@ def test_quote_nil
def test_quote_true
assert_equal @quoter.quoted_true, @quoter.quote(true, nil)
assert_equal '1', @quoter.quote(true, Struct.new(:type).new(:integer))
assert_equal '1', @quoter.quote(true, Type::Integer.new)
end
def test_quote_false
assert_equal @quoter.quoted_false, @quoter.quote(false, nil)
assert_equal '0', @quoter.quote(false, Struct.new(:type).new(:integer))
assert_equal '0', @quoter.quote(false, Type::Integer.new)
end
def test_quote_float
@ -166,26 +158,26 @@ def test_quote_as_mb_chars_no_column
end
def test_quote_string_int_column
assert_equal "1", @quoter.quote('1', FakeColumn.new(:integer))
assert_equal "1", @quoter.quote('1.2', FakeColumn.new(:integer))
assert_equal "1", @quoter.quote('1', Type::Integer.new)
assert_equal "1", @quoter.quote('1.2', Type::Integer.new)
end
def test_quote_string_float_column
assert_equal "1.0", @quoter.quote('1', FakeColumn.new(:float))
assert_equal "1.2", @quoter.quote('1.2', FakeColumn.new(:float))
assert_equal "1.0", @quoter.quote('1', Type::Float.new)
assert_equal "1.2", @quoter.quote('1.2', Type::Float.new)
end
def test_quote_as_mb_chars_binary_column
string = ActiveSupport::Multibyte::Chars.new('lo\l')
assert_equal "'lo\\\\l'", @quoter.quote(string, FakeColumn.new(:binary))
assert_equal "'lo\\\\l'", @quoter.quote(string, Type::Binary.new)
end
def test_quote_binary_without_string_to_binary
assert_equal "'lo\\\\l'", @quoter.quote('lo\l', FakeColumn.new(:binary))
assert_equal "'lo\\\\l'", @quoter.quote('lo\l', Type::Binary.new)
end
def test_string_with_crazy_column
assert_equal "'lo\\\\l'", @quoter.quote('lo\l', FakeColumn.new(:foo))
assert_equal "'lo\\\\l'", @quoter.quote('lo\l')
end
def test_quote_duration
@ -193,7 +185,7 @@ def test_quote_duration
end
def test_quote_duration_int_column
assert_equal "7200", @quoter.quote(2.hours, FakeColumn.new(:integer))
assert_equal "7200", @quoter.quote(2.hours, Type::Integer.new)
end
end
end