提交 155b1b7f 编写于 作者: S Sean Griffin

Remove most uses of `Column#cast_type`

The goal is to remove the type object from the column, and remove
columns from the type casting process entirely. The primary motivation
for this is clarity. The connection adapter does not have sufficient
type information, since the type we want to work with might have been
overriden at the class level. By taking this object from the column,
it is easy to mistakenly think that the column object which exists on
the connection adapter is sufficient. It isn't.

A concrete example of this is `serialize`. In 4.2 and earlier, `where`
worked in a very inconsistent and confusing manner. If you passed a
single value to `where`, it would serialize it before querying, and do
the right thing. However, passing it as part of an array, hash, or range
would cause it to not work. This is because it would stop using prepared
statements, so the type casting would come from arel. Arel would have no
choice but to get the column from the connection adapter, which would
treat it as any other string column, and query for the wrong value.

There are a handful of cases where using the column object to find the
cast type is appropriate. These are cases where there is not actually a
class involved, such as the migration DSL, or fixtures. For all other
cases, the API should be designed as such that the type is provided
before we get to the connection adapter. (For an example of this, see
the work done to decorate the arel table object with a type caster, or
the introduction of `QueryAttribute` to `Relation`).

There are times that it is appropriate to use information from the
column to change behavior in the connection adapter. These cases are
when the primitive used to represent that type before it goes to the
database does not sufficiently express what needs to happen. An example
of this that affects every adapter is binary vs varchar, where the
primitive used for both is a string. In this case it is appropriate to
look at the column object to determine which quoting method to use, as
this is something schema dependent.

An example of something which would not be appropriate is to look at the
type and see that it is a datetime, and performing string parsing when
given a string instead of a date.  This is the type of logic that should
live entirely on the type. The value which comes out of the type should
be a sufficiently generic primitive that the adapter can be expected to
know how to work with it.

The one place that is still using the column for type information which
should not be necessary is the connection adapter type caster which is
sometimes given to the arel table when we can't find the associated
table. This will hopefully go away in the near future.
上级 dedb946b
......@@ -286,7 +286,8 @@ def insert_fixture(fixture, table_name)
columns = schema_cache.columns_hash(table_name)
binds = fixture.map do |name, value|
Relation::QueryAttribute.new(name, value, columns[name].cast_type)
type = lookup_cast_type_from_column(columns[name])
Relation::QueryAttribute.new(name, value, type)
end
key_list = fixture.keys.map { |name| quote_column_name(name) }
value_list = prepare_binds_for_database(binds).map do |value|
......
......@@ -16,7 +16,7 @@ def quote(value, column = nil)
https://github.com/rails/arel/commit/6160bfbda1d1781c3b08a33ec4955f170e95be11
for more information.
MSG
value = column.cast_type.type_cast_for_database(value)
value = type_cast_from_column(column, value)
end
_quote(value)
......@@ -31,7 +31,7 @@ def type_cast(value, column = nil)
end
if column
value = column.cast_type.type_cast_for_database(value)
value = type_cast_from_column(column, value)
end
_type_cast(value)
......@@ -40,6 +40,29 @@ def type_cast(value, column = nil)
raise TypeError, "can't cast #{value.class}#{to_type}"
end
# If you are having to call this function, you are likely doing something
# wrong. The column does not have sufficient type information if the user
# provided a custom type on the class level either explicitly (via
# `attribute`) or implicitly (via `serialize`,
# `time_zone_aware_attributes`). In almost all cases, the sql type should
# only be used to change quoting behavior, when the primitive to
# represent the type doesn't sufficiently reflect the differences
# (varchar vs binary) for example. The type used to get this primitive
# should have been provided before reaching the connection adapter.
def type_cast_from_column(column, value) # :nodoc:
if column
type = lookup_cast_type_from_column(column)
type.type_cast_for_database(value)
else
value
end
end
# See docs for +type_cast_from_column+
def lookup_cast_type_from_column(column) # :nodoc:
lookup_cast_type(column.sql_type)
end
# Quotes a string, escaping any ' (single quote) and \ (backslash)
# characters.
def quote_string(s)
......
......@@ -2,17 +2,19 @@ module ActiveRecord
module ConnectionAdapters
# PostgreSQL-specific extensions to column definitions in a table.
class PostgreSQLColumn < Column #:nodoc:
attr_reader :array
attr_reader :array, :oid, :fmod
alias :array? :array
def initialize(name, default, cast_type, sql_type = nil, null = true, default_function = nil)
def initialize(name, default, cast_type, sql_type = nil, null = true, default_function = nil, oid = nil, fmod = nil)
if sql_type =~ /\[\]$/
@array = true
sql_type = sql_type[0..sql_type.length - 3]
else
@array = false
end
super
@oid = oid
@fmod = fmod
super(name, default, cast_type, sql_type, null, default_function)
end
def serial?
......
......@@ -56,11 +56,15 @@ def quote_default_value(value, column) #:nodoc:
if column.type == :uuid && value =~ /\(\)/
value
else
value = column.cast_type.type_cast_for_database(value)
value = type_cast_from_column(column, value)
quote(value)
end
end
def lookup_cast_type_from_column(column) # :nodoc:
type_map.lookup(column.oid, column.fmod, column.sql_type)
end
private
def _quote(value)
......
......@@ -183,15 +183,17 @@ def indexes(table_name, name = nil)
def columns(table_name)
# Limit, precision, and scale are all handled by the superclass.
column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod|
oid = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
default_value = extract_value_from_default(oid, default)
oid = oid.to_i
fmod = fmod.to_i
cast_type = get_oid_type(oid, fmod, column_name, type)
default_value = extract_value_from_default(cast_type, default)
default_function = extract_default_function(default_value, default)
new_column(column_name, default_value, oid, type, notnull == 'f', default_function)
new_column(column_name, default_value, cast_type, type, notnull == 'f', default_function, oid, fmod)
end
end
def new_column(name, default, cast_type, sql_type = nil, null = true, default_function = nil) # :nodoc:
PostgreSQLColumn.new(name, default, cast_type, sql_type, null, default_function)
def new_column(name, default, cast_type, sql_type = nil, null = true, default_function = nil, oid = nil, fmod = nil) # :nodoc:
PostgreSQLColumn.new(name, default, cast_type, sql_type, null, default_function, oid, fmod)
end
# Returns the current database name.
......
......@@ -8,8 +8,8 @@ def initialize(klass, table_name)
def type_cast_for_database(attribute_name, value)
return value if value.is_a?(Arel::Nodes::BindParam)
type = type_for(attribute_name)
type.type_cast_for_database(value)
column = column_for(attribute_name)
connection.type_cast_from_column(column, value)
end
protected
......@@ -19,17 +19,11 @@ def type_cast_for_database(attribute_name, value)
private
def type_for(attribute_name)
def column_for(attribute_name)
if connection.schema_cache.table_exists?(table_name)
column_for(attribute_name).cast_type
else
Type::Value.new
connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]
end
end
def column_for(attribute_name)
connection.schema_cache.columns_hash(table_name)[attribute_name.to_s]
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册