mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
type_cast_for_database
-> serialize
This commit is contained in:
parent
4a3cb840b0
commit
1455c4c22f
40 changed files with 79 additions and 79 deletions
|
@ -43,7 +43,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def value_for_database
|
def value_for_database
|
||||||
type.type_cast_for_database(value)
|
type.serialize(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def changed_from?(old_value)
|
def changed_from?(old_value)
|
||||||
|
|
|
@ -133,7 +133,7 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
# When ActiveRecord::QueryMethods#where is called, it will
|
# When ActiveRecord::QueryMethods#where is called, it will
|
||||||
# use the type defined by the model class to convert the value to SQL,
|
# use the type defined by the model class to convert the value to SQL,
|
||||||
# calling +type_cast_for_database+ on your type object. For example:
|
# calling +serialize+ on your type object. For example:
|
||||||
#
|
#
|
||||||
# class Money < Struct.new(:amount, :currency)
|
# class Money < Struct.new(:amount, :currency)
|
||||||
# end
|
# end
|
||||||
|
@ -146,7 +146,7 @@ module ActiveRecord
|
||||||
# # value will be the result of +deserialize+ or
|
# # value will be the result of +deserialize+ or
|
||||||
# # +type_cast_from_user+. Assumed to be in instance of +Money+ in
|
# # +type_cast_from_user+. Assumed to be in instance of +Money+ in
|
||||||
# # this case.
|
# # this case.
|
||||||
# def type_cast_for_database(value)
|
# def serialize(value)
|
||||||
# value_in_bitcoins = @currency_converter.convert_to_bitcoins(value)
|
# value_in_bitcoins = @currency_converter.convert_to_bitcoins(value)
|
||||||
# value_in_bitcoins.amount
|
# value_in_bitcoins.amount
|
||||||
# end
|
# end
|
||||||
|
|
|
@ -52,7 +52,7 @@ module ActiveRecord
|
||||||
def type_cast_from_column(column, value) # :nodoc:
|
def type_cast_from_column(column, value) # :nodoc:
|
||||||
if column
|
if column
|
||||||
type = lookup_cast_type_from_column(column)
|
type = lookup_cast_type_from_column(column)
|
||||||
type.type_cast_for_database(value)
|
type.serialize(value)
|
||||||
else
|
else
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
@ -103,7 +103,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def quote_default_expression(value, column) #:nodoc:
|
def quote_default_expression(value, column) #:nodoc:
|
||||||
value = lookup_cast_type(column.sql_type).type_cast_for_database(value)
|
value = lookup_cast_type(column.sql_type).serialize(value)
|
||||||
quote(value)
|
quote(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -926,7 +926,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
class MysqlString < Type::String # :nodoc:
|
class MysqlString < Type::String # :nodoc:
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
case value
|
case value
|
||||||
when true then "1"
|
when true then "1"
|
||||||
when false then "0"
|
when false then "0"
|
||||||
|
|
|
@ -40,7 +40,7 @@ module ActiveRecord
|
||||||
type_cast_array(value, :type_cast_from_user)
|
type_cast_array(value, :type_cast_from_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if value.is_a?(::Array)
|
if value.is_a?(::Array)
|
||||||
cast_value_for_database(value)
|
cast_value_for_database(value)
|
||||||
else
|
else
|
||||||
|
@ -69,7 +69,7 @@ module ActiveRecord
|
||||||
casted_values = value.map { |item| cast_value_for_database(item) }
|
casted_values = value.map { |item| cast_value_for_database(item) }
|
||||||
"{#{casted_values.join(delimiter)}}"
|
"{#{casted_values.join(delimiter)}}"
|
||||||
else
|
else
|
||||||
quote_and_escape(subtype.type_cast_for_database(value))
|
quote_and_escape(subtype.serialize(value))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
Data.new(super) if value
|
Data.new(super) if value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if IPAddr === value
|
if IPAddr === value
|
||||||
"#{value}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
|
"#{value}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
|
||||||
else
|
else
|
||||||
|
|
|
@ -21,7 +21,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if value.is_a?(::Hash)
|
if value.is_a?(::Hash)
|
||||||
value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(', ')
|
value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(', ')
|
||||||
else
|
else
|
||||||
|
|
|
@ -17,7 +17,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if value.is_a?(::Array) || value.is_a?(::Hash)
|
if value.is_a?(::Array) || value.is_a?(::Hash)
|
||||||
::ActiveSupport::JSON.encode(value)
|
::ActiveSupport::JSON.encode(value)
|
||||||
else
|
else
|
||||||
|
|
|
@ -13,7 +13,7 @@ module ActiveRecord
|
||||||
# the comparison here. Therefore, we need to parse and re-dump the
|
# the comparison here. Therefore, we need to parse and re-dump the
|
||||||
# raw value here to ensure the insignificant whitespaces are
|
# raw value here to ensure the insignificant whitespaces are
|
||||||
# consistent with our encoder's output.
|
# consistent with our encoder's output.
|
||||||
raw_old_value = type_cast_for_database(deserialize(raw_old_value))
|
raw_old_value = serialize(deserialize(raw_old_value))
|
||||||
super(raw_old_value, new_value)
|
super(raw_old_value, new_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if value.is_a?(::Array)
|
if value.is_a?(::Array)
|
||||||
"(#{number_for_point(value[0])},#{number_for_point(value[1])})"
|
"(#{number_for_point(value[0])},#{number_for_point(value[1])})"
|
||||||
else
|
else
|
||||||
|
|
|
@ -30,7 +30,7 @@ module ActiveRecord
|
||||||
::Range.new(from, to, extracted[:exclude_end])
|
::Range.new(from, to, extracted[:exclude_end])
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if value.is_a?(::Range)
|
if value.is_a?(::Range)
|
||||||
from = type_cast_single_for_database(value.begin)
|
from = type_cast_single_for_database(value.begin)
|
||||||
to = type_cast_single_for_database(value.end)
|
to = type_cast_single_for_database(value.end)
|
||||||
|
@ -53,7 +53,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_single_for_database(value)
|
def type_cast_single_for_database(value)
|
||||||
infinity?(value) ? '' : @subtype.type_cast_for_database(value)
|
infinity?(value) ? '' : @subtype.serialize(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def extract_bounds(value)
|
def extract_bounds(value)
|
||||||
|
|
|
@ -5,7 +5,7 @@ module ActiveRecord
|
||||||
class Uuid < Type::Value # :nodoc:
|
class Uuid < Type::Value # :nodoc:
|
||||||
ACCEPTABLE_UUID = %r{\A\{?([a-fA-F0-9]{4}-?){8}\}?\z}x
|
ACCEPTABLE_UUID = %r{\A\{?([a-fA-F0-9]{4}-?){8}\}?\z}x
|
||||||
|
|
||||||
alias_method :type_cast_for_database, :deserialize
|
alias_method :serialize, :deserialize
|
||||||
|
|
||||||
def type
|
def type
|
||||||
:uuid
|
:uuid
|
||||||
|
|
|
@ -7,7 +7,7 @@ module ActiveRecord
|
||||||
:xml
|
:xml
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
return unless value
|
return unless value
|
||||||
Data.new(super)
|
Data.new(super)
|
||||||
end
|
end
|
||||||
|
|
|
@ -110,7 +110,7 @@ module ActiveRecord
|
||||||
mapping.key(value.to_i)
|
mapping.key(value.to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
mapping.fetch(value, value)
|
mapping.fetch(value, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ module ActiveRecord
|
||||||
def sanitize_sql_hash_for_assignment(attrs, table)
|
def sanitize_sql_hash_for_assignment(attrs, table)
|
||||||
c = connection
|
c = connection
|
||||||
attrs.map do |attr, value|
|
attrs.map do |attr, value|
|
||||||
value = type_for_attribute(attr.to_s).type_cast_for_database(value)
|
value = type_for_attribute(attr.to_s).serialize(value)
|
||||||
"#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
|
"#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
|
||||||
end.join(', ')
|
end.join(', ')
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
return if value.nil?
|
return if value.nil?
|
||||||
Data.new(super)
|
Data.new(super)
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@ module ActiveRecord
|
||||||
:datetime
|
:datetime
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
if precision && value.respond_to?(:usec)
|
if precision && value.respond_to?(:usec)
|
||||||
number_of_insignificant_digits = 6 - precision
|
number_of_insignificant_digits = 6 - precision
|
||||||
round_power = 10 ** number_of_insignificant_digits
|
round_power = 10 ** number_of_insignificant_digits
|
||||||
|
|
|
@ -7,7 +7,7 @@ module ActiveRecord
|
||||||
:float
|
:float
|
||||||
end
|
end
|
||||||
|
|
||||||
alias type_cast_for_database type_cast
|
alias serialize type_cast
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,14 @@ module ActiveRecord
|
||||||
module Helpers
|
module Helpers
|
||||||
module Mutable # :nodoc:
|
module Mutable # :nodoc:
|
||||||
def type_cast_from_user(value)
|
def type_cast_from_user(value)
|
||||||
deserialize(type_cast_for_database(value))
|
deserialize(serialize(value))
|
||||||
end
|
end
|
||||||
|
|
||||||
# +raw_old_value+ will be the `_before_type_cast` version of the
|
# +raw_old_value+ will be the `_before_type_cast` version of the
|
||||||
# value (likely a string). +new_value+ will be the current, type
|
# value (likely a string). +new_value+ will be the current, type
|
||||||
# cast value.
|
# cast value.
|
||||||
def changed_in_place?(raw_old_value, new_value)
|
def changed_in_place?(raw_old_value, new_value)
|
||||||
raw_old_value != type_cast_for_database(new_value)
|
raw_old_value != serialize(new_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@ module ActiveRecord
|
||||||
value.to_i
|
value.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
result = type_cast(value)
|
result = type_cast(value)
|
||||||
if result
|
if result
|
||||||
ensure_in_range(result)
|
ensure_in_range(result)
|
||||||
|
|
|
@ -19,7 +19,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
return if value.nil?
|
return if value.nil?
|
||||||
unless default_value?(value)
|
unless default_value?(value)
|
||||||
super coder.dump(value)
|
super coder.dump(value)
|
||||||
|
@ -28,7 +28,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def changed_in_place?(raw_old_value, value)
|
def changed_in_place?(raw_old_value, value)
|
||||||
return false if value.nil?
|
return false if value.nil?
|
||||||
subtype.changed_in_place?(raw_old_value, type_cast_for_database(value))
|
subtype.changed_in_place?(raw_old_value, serialize(value))
|
||||||
end
|
end
|
||||||
|
|
||||||
def accessor
|
def accessor
|
||||||
|
|
|
@ -11,7 +11,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
case value
|
case value
|
||||||
when ::Numeric, ActiveSupport::Duration then value.to_s
|
when ::Numeric, ActiveSupport::Duration then value.to_s
|
||||||
when ::String then ::String.new(value)
|
when ::String then ::String.new(value)
|
||||||
|
|
|
@ -40,7 +40,7 @@ module ActiveRecord
|
||||||
# to understand. The returned value from this method should be a
|
# to understand. The returned value from this method should be a
|
||||||
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
|
# +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or
|
||||||
# +nil+.
|
# +nil+.
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ module ActiveRecord
|
||||||
# which could be mutated, you should override this method. You will need
|
# which could be mutated, you should override this method. You will need
|
||||||
# to either:
|
# to either:
|
||||||
#
|
#
|
||||||
# - pass +new_value+ to Value#type_cast_for_database and compare it to
|
# - pass +new_value+ to Value#serialize and compare it to
|
||||||
# +raw_old_value+
|
# +raw_old_value+
|
||||||
#
|
#
|
||||||
# or
|
# or
|
||||||
|
|
|
@ -8,7 +8,7 @@ module ActiveRecord
|
||||||
def type_cast_for_database(attr_name, value)
|
def type_cast_for_database(attr_name, value)
|
||||||
return value if value.is_a?(Arel::Nodes::BindParam)
|
return value if value.is_a?(Arel::Nodes::BindParam)
|
||||||
type = types.type_for_attribute(attr_name.to_s)
|
type = types.type_for_attribute(attr_name.to_s)
|
||||||
type.type_cast_for_database(value)
|
type.serialize(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -61,7 +61,7 @@ module ActiveRecord
|
||||||
|
|
||||||
column = klass.columns_hash[attribute_name]
|
column = klass.columns_hash[attribute_name]
|
||||||
cast_type = klass.type_for_attribute(attribute_name)
|
cast_type = klass.type_for_attribute(attribute_name)
|
||||||
value = cast_type.type_cast_for_database(value)
|
value = cast_type.serialize(value)
|
||||||
value = klass.connection.type_cast(value)
|
value = klass.connection.type_cast(value)
|
||||||
if value.is_a?(String) && column.limit
|
if value.is_a?(String) && column.limit
|
||||||
value = value.to_s[0, column.limit]
|
value = value.to_s[0, column.limit]
|
||||||
|
|
|
@ -206,7 +206,7 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
|
||||||
x = PgArray.create!(tags: tags)
|
x = PgArray.create!(tags: tags)
|
||||||
x.reload
|
x.reload
|
||||||
|
|
||||||
assert_equal x.tags_before_type_cast, PgArray.type_for_attribute('tags').type_cast_for_database(tags)
|
assert_equal x.tags_before_type_cast, PgArray.type_for_attribute('tags').serialize(tags)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_quoting_non_standard_delimiters
|
def test_quoting_non_standard_delimiters
|
||||||
|
@ -214,8 +214,8 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
|
||||||
comma_delim = OID::Array.new(ActiveRecord::Type::String.new, ',')
|
comma_delim = OID::Array.new(ActiveRecord::Type::String.new, ',')
|
||||||
semicolon_delim = OID::Array.new(ActiveRecord::Type::String.new, ';')
|
semicolon_delim = OID::Array.new(ActiveRecord::Type::String.new, ';')
|
||||||
|
|
||||||
assert_equal %({"hello,",world;}), comma_delim.type_cast_for_database(strings)
|
assert_equal %({"hello,",world;}), comma_delim.serialize(strings)
|
||||||
assert_equal %({hello,;"world;"}), semicolon_delim.type_cast_for_database(strings)
|
assert_equal %({hello,;"world;"}), semicolon_delim.serialize(strings)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mutate_array
|
def test_mutate_array
|
||||||
|
|
|
@ -10,14 +10,14 @@ module ActiveRecord
|
||||||
ip = IPAddr.new("255.0.0.0/8")
|
ip = IPAddr.new("255.0.0.0/8")
|
||||||
ip2 = IPAddr.new("127.0.0.1")
|
ip2 = IPAddr.new("127.0.0.1")
|
||||||
|
|
||||||
assert_equal "255.0.0.0/8", type.type_cast_for_database(ip)
|
assert_equal "255.0.0.0/8", type.serialize(ip)
|
||||||
assert_equal "127.0.0.1/32", type.type_cast_for_database(ip2)
|
assert_equal "127.0.0.1/32", type.serialize(ip2)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "casting does nothing with non-IPAddr objects" do
|
test "casting does nothing with non-IPAddr objects" do
|
||||||
type = OID::Cidr.new
|
type = OID::Cidr.new
|
||||||
|
|
||||||
assert_equal "foo", type.type_cast_for_database("foo")
|
assert_equal "foo", type.serialize("foo")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -93,7 +93,7 @@ class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
return if value.nil?
|
return if value.nil?
|
||||||
"(#{value.city},#{value.street})"
|
"(#{value.city},#{value.street})"
|
||||||
end
|
end
|
||||||
|
|
|
@ -165,19 +165,19 @@ if ActiveRecord::Base.connection.supports_extensions?
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_gen1
|
def test_gen1
|
||||||
assert_equal(%q(" "=>""), @type.type_cast_for_database({' '=>''}))
|
assert_equal(%q(" "=>""), @type.serialize({' '=>''}))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_gen2
|
def test_gen2
|
||||||
assert_equal(%q(","=>""), @type.type_cast_for_database({','=>''}))
|
assert_equal(%q(","=>""), @type.serialize({','=>''}))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_gen3
|
def test_gen3
|
||||||
assert_equal(%q("="=>""), @type.type_cast_for_database({'='=>''}))
|
assert_equal(%q("="=>""), @type.serialize({'='=>''}))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_gen4
|
def test_gen4
|
||||||
assert_equal(%q(">"=>""), @type.type_cast_for_database({'>'=>''}))
|
assert_equal(%q(">"=>""), @type.serialize({'>'=>''}))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_parse1
|
def test_parse1
|
||||||
|
|
|
@ -30,13 +30,13 @@ module ActiveRecord
|
||||||
def test_quote_range
|
def test_quote_range
|
||||||
range = "1,2]'; SELECT * FROM users; --".."a"
|
range = "1,2]'; SELECT * FROM users; --".."a"
|
||||||
type = OID::Range.new(Type::Integer.new, :int8range)
|
type = OID::Range.new(Type::Integer.new, :int8range)
|
||||||
assert_equal "'[1,0]'", @conn.quote(type.type_cast_for_database(range))
|
assert_equal "'[1,0]'", @conn.quote(type.serialize(range))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_quote_bit_string
|
def test_quote_bit_string
|
||||||
value = "'); SELECT * FROM users; /*\n01\n*/--"
|
value = "'); SELECT * FROM users; /*\n01\n*/--"
|
||||||
type = OID::Bit.new
|
type = OID::Bit.new
|
||||||
assert_equal nil, @conn.quote(type.type_cast_for_database(value))
|
assert_equal nil, @conn.quote(type.serialize(value))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,8 +18,8 @@ class PostgresqlTypeLookupTest < ActiveRecord::TestCase
|
||||||
bigint_array = @connection.type_map.lookup(1016, -1, "bigint[]")
|
bigint_array = @connection.type_map.lookup(1016, -1, "bigint[]")
|
||||||
big_array = [123456789123456789]
|
big_array = [123456789123456789]
|
||||||
|
|
||||||
assert_raises(RangeError) { int_array.type_cast_for_database(big_array) }
|
assert_raises(RangeError) { int_array.serialize(big_array) }
|
||||||
assert_equal "{123456789123456789}", bigint_array.type_cast_for_database(big_array)
|
assert_equal "{123456789123456789}", bigint_array.serialize(big_array)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "range types correctly respect registration of subtypes" do
|
test "range types correctly respect registration of subtypes" do
|
||||||
|
@ -27,7 +27,7 @@ class PostgresqlTypeLookupTest < ActiveRecord::TestCase
|
||||||
bigint_range = @connection.type_map.lookup(3926, -1, "int8range")
|
bigint_range = @connection.type_map.lookup(3926, -1, "int8range")
|
||||||
big_range = 0..123456789123456789
|
big_range = 0..123456789123456789
|
||||||
|
|
||||||
assert_raises(RangeError) { int_range.type_cast_for_database(big_range) }
|
assert_raises(RangeError) { int_range.serialize(big_range) }
|
||||||
assert_equal "[0,123456789123456789]", bigint_range.type_cast_for_database(big_range)
|
assert_equal "[0,123456789123456789]", bigint_range.serialize(big_range)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,7 +87,7 @@ module ActiveRecord
|
||||||
value = "hello".encode('ascii-8bit')
|
value = "hello".encode('ascii-8bit')
|
||||||
type = Type::String.new
|
type = Type::String.new
|
||||||
|
|
||||||
assert_equal "'hello'", @conn.quote(type.type_cast_for_database(value))
|
assert_equal "'hello'", @conn.quote(type.serialize(value))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,22 +59,22 @@ module ActiveRecord
|
||||||
|
|
||||||
test "from_database + read_for_database type casts to and from database" do
|
test "from_database + read_for_database type casts to and from database" do
|
||||||
@type.expect(:deserialize, 'read from database', ['whatever'])
|
@type.expect(:deserialize, 'read from database', ['whatever'])
|
||||||
@type.expect(:type_cast_for_database, 'ready for database', ['read from database'])
|
@type.expect(:serialize, 'ready for database', ['read from database'])
|
||||||
attribute = Attribute.from_database(nil, 'whatever', @type)
|
attribute = Attribute.from_database(nil, 'whatever', @type)
|
||||||
|
|
||||||
type_cast_for_database = attribute.value_for_database
|
serialize = attribute.value_for_database
|
||||||
|
|
||||||
assert_equal 'ready for database', type_cast_for_database
|
assert_equal 'ready for database', serialize
|
||||||
end
|
end
|
||||||
|
|
||||||
test "from_user + read_for_database type casts from the user to the database" do
|
test "from_user + read_for_database type casts from the user to the database" do
|
||||||
@type.expect(:type_cast_from_user, 'read from user', ['whatever'])
|
@type.expect(:type_cast_from_user, 'read from user', ['whatever'])
|
||||||
@type.expect(:type_cast_for_database, 'ready for database', ['read from user'])
|
@type.expect(:serialize, 'ready for database', ['read from user'])
|
||||||
attribute = Attribute.from_user(nil, 'whatever', @type)
|
attribute = Attribute.from_user(nil, 'whatever', @type)
|
||||||
|
|
||||||
type_cast_for_database = attribute.value_for_database
|
serialize = attribute.value_for_database
|
||||||
|
|
||||||
assert_equal 'ready for database', type_cast_for_database
|
assert_equal 'ready for database', serialize
|
||||||
end
|
end
|
||||||
|
|
||||||
test "duping dups the value" do
|
test "duping dups the value" do
|
||||||
|
|
|
@ -623,13 +623,13 @@ class DirtyTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "defaults with type that implements `type_cast_for_database`" do
|
test "defaults with type that implements `serialize`" do
|
||||||
type = Class.new(ActiveRecord::Type::Value) do
|
type = Class.new(ActiveRecord::Type::Value) do
|
||||||
def type_cast(value)
|
def type_cast(value)
|
||||||
value.to_i
|
value.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
value.to_s
|
value.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -94,7 +94,7 @@ class ReflectionTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
assert_equal object, type.deserialize(object)
|
assert_equal object, type.deserialize(object)
|
||||||
assert_equal object, type.type_cast_from_user(object)
|
assert_equal object, type.type_cast_from_user(object)
|
||||||
assert_equal object, type.type_cast_for_database(object)
|
assert_equal object, type.serialize(object)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_reflection_klass_for_nested_class_name
|
def test_reflection_klass_for_nested_class_name
|
||||||
|
|
|
@ -259,7 +259,7 @@ module ActiveRecord
|
||||||
"type cast from database"
|
"type cast from database"
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def serialize(value)
|
||||||
raise value unless value == "value from user"
|
raise value unless value == "value from user"
|
||||||
"type cast for database"
|
"type cast for database"
|
||||||
end
|
end
|
||||||
|
|
|
@ -43,8 +43,8 @@ module ActiveRecord
|
||||||
|
|
||||||
test "casting booleans for database" do
|
test "casting booleans for database" do
|
||||||
type = Type::Integer.new
|
type = Type::Integer.new
|
||||||
assert_equal 1, type.type_cast_for_database(true)
|
assert_equal 1, type.serialize(true)
|
||||||
assert_equal 0, type.type_cast_for_database(false)
|
assert_equal 0, type.serialize(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "changed?" do
|
test "changed?" do
|
||||||
|
@ -60,53 +60,53 @@ module ActiveRecord
|
||||||
|
|
||||||
test "values below int min value are out of range" do
|
test "values below int min value are out of range" do
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
Integer.new.type_cast_for_database(-2147483649)
|
Integer.new.serialize(-2147483649)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "values above int max value are out of range" do
|
test "values above int max value are out of range" do
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
Integer.new.type_cast_for_database(2147483648)
|
Integer.new.serialize(2147483648)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "very small numbers are out of range" do
|
test "very small numbers are out of range" do
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
Integer.new.type_cast_for_database(-9999999999999999999999999999999)
|
Integer.new.serialize(-9999999999999999999999999999999)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "very large numbers are out of range" do
|
test "very large numbers are out of range" do
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
Integer.new.type_cast_for_database(9999999999999999999999999999999)
|
Integer.new.serialize(9999999999999999999999999999999)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "normal numbers are in range" do
|
test "normal numbers are in range" do
|
||||||
type = Integer.new
|
type = Integer.new
|
||||||
assert_equal(0, type.type_cast_for_database(0))
|
assert_equal(0, type.serialize(0))
|
||||||
assert_equal(-1, type.type_cast_for_database(-1))
|
assert_equal(-1, type.serialize(-1))
|
||||||
assert_equal(1, type.type_cast_for_database(1))
|
assert_equal(1, type.serialize(1))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "int max value is in range" do
|
test "int max value is in range" do
|
||||||
assert_equal(2147483647, Integer.new.type_cast_for_database(2147483647))
|
assert_equal(2147483647, Integer.new.serialize(2147483647))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "int min value is in range" do
|
test "int min value is in range" do
|
||||||
assert_equal(-2147483648, Integer.new.type_cast_for_database(-2147483648))
|
assert_equal(-2147483648, Integer.new.serialize(-2147483648))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "columns with a larger limit have larger ranges" do
|
test "columns with a larger limit have larger ranges" do
|
||||||
type = Integer.new(limit: 8)
|
type = Integer.new(limit: 8)
|
||||||
|
|
||||||
assert_equal(9223372036854775807, type.type_cast_for_database(9223372036854775807))
|
assert_equal(9223372036854775807, type.serialize(9223372036854775807))
|
||||||
assert_equal(-9223372036854775808, type.type_cast_for_database(-9223372036854775808))
|
assert_equal(-9223372036854775808, type.serialize(-9223372036854775808))
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
type.type_cast_for_database(-9999999999999999999999999999999)
|
type.serialize(-9999999999999999999999999999999)
|
||||||
end
|
end
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
type.type_cast_for_database(9999999999999999999999999999999)
|
type.serialize(9999999999999999999999999999999)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,12 @@ module ActiveRecord
|
||||||
module Type
|
module Type
|
||||||
class UnsignedIntegerTest < ActiveRecord::TestCase
|
class UnsignedIntegerTest < ActiveRecord::TestCase
|
||||||
test "unsigned int max value is in range" do
|
test "unsigned int max value is in range" do
|
||||||
assert_equal(4294967295, UnsignedInteger.new.type_cast_for_database(4294967295))
|
assert_equal(4294967295, UnsignedInteger.new.serialize(4294967295))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "minus value is out of range" do
|
test "minus value is out of range" do
|
||||||
assert_raises(::RangeError) do
|
assert_raises(::RangeError) do
|
||||||
UnsignedInteger.new.type_cast_for_database(-1)
|
UnsignedInteger.new.serialize(-1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -110,7 +110,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def test_attributes_which_are_invalid_for_database_can_still_be_reassigned
|
def test_attributes_which_are_invalid_for_database_can_still_be_reassigned
|
||||||
type_which_cannot_go_to_the_database = Type::Value.new
|
type_which_cannot_go_to_the_database = Type::Value.new
|
||||||
def type_which_cannot_go_to_the_database.type_cast_for_database(*)
|
def type_which_cannot_go_to_the_database.serialize(*)
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
klass = Class.new(ActiveRecord::Base) do
|
klass = Class.new(ActiveRecord::Base) do
|
||||||
|
|
Loading…
Reference in a new issue