mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #15203 from sgrif/sg-delegate-type-cast
Replace `type_cast` case statement with delegation
This commit is contained in:
commit
59ee23f721
7 changed files with 7 additions and 75 deletions
|
@ -249,10 +249,9 @@ module ActiveRecord
|
|||
raise NotImplementedError
|
||||
end
|
||||
|
||||
# Overridden by the adapters to instantiate their specific Column type.
|
||||
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
|
||||
cast_type = lookup_cast_type(sql_type)
|
||||
Column.new(field, default, cast_type, sql_type, null, collation, extra)
|
||||
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
|
||||
end
|
||||
|
||||
# Must return the Mysql error number from the exception, if the exception has an
|
||||
|
|
|
@ -94,28 +94,10 @@ module ActiveRecord
|
|||
|
||||
# Casts value to an appropriate instance.
|
||||
def type_cast(value)
|
||||
return nil if value.nil?
|
||||
return coder.load(value) if encoded?
|
||||
|
||||
klass = self.class
|
||||
|
||||
case type
|
||||
when :string, :text
|
||||
case value
|
||||
when TrueClass; "1"
|
||||
when FalseClass; "0"
|
||||
else
|
||||
value.to_s
|
||||
end
|
||||
when :integer then klass.value_to_integer(value)
|
||||
when :float then value.to_f
|
||||
when :decimal then klass.value_to_decimal(value)
|
||||
when :datetime then klass.string_to_time(value)
|
||||
when :time then klass.string_to_dummy_time(value)
|
||||
when :date then klass.value_to_date(value)
|
||||
when :binary then klass.binary_to_string(value)
|
||||
when :boolean then klass.value_to_boolean(value)
|
||||
else value
|
||||
if encoded?
|
||||
coder.load(value)
|
||||
else
|
||||
cast_type.type_cast(value)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -29,13 +29,6 @@ module ActiveRecord
|
|||
|
||||
module ConnectionAdapters
|
||||
class Mysql2Adapter < AbstractMysqlAdapter
|
||||
|
||||
class Column < AbstractMysqlAdapter::Column # :nodoc:
|
||||
def adapter
|
||||
Mysql2Adapter
|
||||
end
|
||||
end
|
||||
|
||||
ADAPTER_NAME = 'Mysql2'
|
||||
|
||||
def initialize(connection, logger, connection_options, config)
|
||||
|
@ -69,11 +62,6 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
|
||||
cast_type = lookup_cast_type(sql_type)
|
||||
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
|
||||
end
|
||||
|
||||
def error_number(exception)
|
||||
exception.error_number if exception.respond_to?(:error_number)
|
||||
end
|
||||
|
|
|
@ -66,21 +66,6 @@ module ActiveRecord
|
|||
# * <tt>:sslcipher</tt> - Necessary to use MySQL with an SSL connection.
|
||||
#
|
||||
class MysqlAdapter < AbstractMysqlAdapter
|
||||
|
||||
class Column < AbstractMysqlAdapter::Column #:nodoc:
|
||||
def type_cast(value)
|
||||
if encoded?
|
||||
super
|
||||
else
|
||||
cast_type.type_cast(value)
|
||||
end
|
||||
end
|
||||
|
||||
def adapter
|
||||
MysqlAdapter
|
||||
end
|
||||
end
|
||||
|
||||
ADAPTER_NAME = 'MySQL'
|
||||
|
||||
class StatementPool < ConnectionAdapters::StatementPool
|
||||
|
@ -142,11 +127,6 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
|
||||
cast_type = lookup_cast_type(sql_type)
|
||||
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
|
||||
end
|
||||
|
||||
def error_number(exception) # :nodoc:
|
||||
exception.errno if exception.respond_to?(:errno)
|
||||
end
|
||||
|
|
|
@ -123,13 +123,6 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def type_cast(value)
|
||||
return if value.nil?
|
||||
return super if encoded?
|
||||
|
||||
@oid_type.type_cast value
|
||||
end
|
||||
|
||||
def accessor
|
||||
@oid_type.accessor
|
||||
end
|
||||
|
|
|
@ -41,16 +41,6 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
module ConnectionAdapters #:nodoc:
|
||||
class SQLite3Column < Column #:nodoc:
|
||||
def type_cast(value)
|
||||
if encoded?
|
||||
super
|
||||
else
|
||||
cast_type.type_cast(value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class SQLite3Binary < Type::Binary # :nodoc:
|
||||
def cast_value(value)
|
||||
if value.encoding != Encoding::ASCII_8BIT
|
||||
|
@ -403,7 +393,7 @@ module ActiveRecord
|
|||
|
||||
sql_type = field['type']
|
||||
cast_type = lookup_cast_type(sql_type)
|
||||
SQLite3Column.new(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
|
||||
Column.new(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -146,7 +146,7 @@ module ActiveRecord
|
|||
|
||||
if current_adapter?(:SQLite3Adapter)
|
||||
def test_binary_encoding
|
||||
column = SQLite3Column.new("field", nil, SQLite3Binary.new)
|
||||
column = Column.new("field", nil, SQLite3Binary.new)
|
||||
utf8_string = "a string".encode(Encoding::UTF_8)
|
||||
type_cast = column.type_cast(utf8_string)
|
||||
|
||||
|
|
Loading…
Reference in a new issue