aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-20 07:56:04 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-20 09:04:09 -0700
commitca703bbc06c353acc41851578e264e4459d481c2 (patch)
tree433ccd3ccf664e649bae51af408bacb7c9881df5 /activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
parent89ca6806a72006ef493e5e6ceb50f8ed02dc2da3 (diff)
downloadrails-ca703bbc06c353acc41851578e264e4459d481c2.tar.gz
rails-ca703bbc06c353acc41851578e264e4459d481c2.tar.bz2
rails-ca703bbc06c353acc41851578e264e4459d481c2.zip
Use general types for mysql fields
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb80
1 files changed, 11 insertions, 69 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index fc27e1dd66..fc81a56fcb 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -283,39 +283,7 @@ module ActiveRecord
end
module Fields
- class Type
- def type; end
-
- def type_cast_for_write(value)
- value
- end
- end
-
- class Identity < Type
- def type_cast(value); value; end
- end
-
- class Integer < Type
- def type_cast(value)
- return if value.nil?
-
- value.to_i rescue value ? 1 : 0
- end
- end
-
- class Date < Type
- def type; :date; end
-
- def type_cast(value)
- return if value.nil?
-
- # FIXME: probably we can improve this since we know it is mysql
- # specific
- ConnectionAdapters::Column.value_to_date value
- end
- end
-
- class DateTime < ConnectionAdapters::Type::DateTime
+ class DateTime < Type::DateTime
def cast_value(value)
if Mysql::Time === value
new_time(
@@ -332,7 +300,7 @@ module ActiveRecord
end
end
- class Time < ConnectionAdapters::Type::Time
+ class Time < Type::Time
def cast_value(value)
if Mysql::Time === value
new_time(
@@ -349,32 +317,6 @@ module ActiveRecord
end
end
- class Float < Type
- def type; :float; end
-
- def type_cast(value)
- return if value.nil?
-
- value.to_f
- end
- end
-
- class Decimal < Type
- def type_cast(value)
- return if value.nil?
-
- ConnectionAdapters::Column.value_to_decimal value
- end
- end
-
- class Boolean < Type
- def type_cast(value)
- return if value.nil?
-
- ConnectionAdapters::Column.value_to_boolean value
- end
- end
-
TYPES = {}
# Register an MySQL +type_id+ with a typecasting object in
@@ -391,26 +333,26 @@ module ActiveRecord
if field.type == Mysql::Field::TYPE_TINY && field.length > 1
TYPES[Mysql::Field::TYPE_LONG]
else
- TYPES.fetch(field.type) { Fields::Identity.new }
+ TYPES.fetch(field.type) { Type::Value.new }
end
end
- register_type Mysql::Field::TYPE_TINY, Fields::Boolean.new
- register_type Mysql::Field::TYPE_LONG, Fields::Integer.new
+ register_type Mysql::Field::TYPE_TINY, Type::Boolean.new
+ register_type Mysql::Field::TYPE_LONG, Type::Integer.new
alias_type Mysql::Field::TYPE_LONGLONG, Mysql::Field::TYPE_LONG
alias_type Mysql::Field::TYPE_NEWDECIMAL, Mysql::Field::TYPE_LONG
- register_type Mysql::Field::TYPE_VAR_STRING, Fields::Identity.new
- register_type Mysql::Field::TYPE_BLOB, Fields::Identity.new
- register_type Mysql::Field::TYPE_DATE, Fields::Date.new
+ register_type Mysql::Field::TYPE_VAR_STRING, Type::Value.new
+ register_type Mysql::Field::TYPE_BLOB, Type::Value.new
+ register_type Mysql::Field::TYPE_DATE, Type::Date.new
register_type Mysql::Field::TYPE_DATETIME, Fields::DateTime.new
register_type Mysql::Field::TYPE_TIME, Fields::Time.new
- register_type Mysql::Field::TYPE_FLOAT, Fields::Float.new
+ register_type Mysql::Field::TYPE_FLOAT, Type::Float.new
Mysql::Field.constants.grep(/TYPE/).map { |class_name|
Mysql::Field.const_get class_name
}.reject { |const| TYPES.key? const }.each do |const|
- register_type const, Fields::Identity.new
+ register_type const, Type::Value.new
end
end
@@ -435,7 +377,7 @@ module ActiveRecord
fields << field_name
if field.decimals > 0
- types[field_name] = Fields::Decimal.new
+ types[field_name] = Type::Decimal.new
else
types[field_name] = Fields.find_type field
end