Refactor the type casting of booleans in MySQL

This commit is contained in:
Sean Griffin 2014-05-26 07:01:53 -07:00
parent 7a5fbaf02c
commit fbdd58081e
2 changed files with 18 additions and 13 deletions

@ -55,8 +55,8 @@ def type_cast(value, column)
case value case value
when String, ActiveSupport::Multibyte::Chars when String, ActiveSupport::Multibyte::Chars
value.to_s value.to_s
when true then 't' when true then unquoted_true
when false then 'f' when false then unquoted_false
when nil then nil when nil then nil
# BigDecimals need to be put in a non-normalized form and quoted. # BigDecimals need to be put in a non-normalized form and quoted.
when BigDecimal then value.to_s('F') when BigDecimal then value.to_s('F')
@ -101,10 +101,18 @@ def quoted_true
"'t'" "'t'"
end end
def unquoted_true
't'
end
def quoted_false def quoted_false
"'f'" "'f'"
end end
def unquoted_false
'f'
end
def quoted_date(value) def quoted_date(value)
if value.acts_like?(:time) if value.acts_like?(:time)
zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal

@ -178,17 +178,6 @@ def supports_index_sort_order?
true true
end end
def type_cast(value, column)
case value
when TrueClass
1
when FalseClass
0
else
super
end
end
# MySQL 4 technically support transaction isolation, but it is affected by a bug # MySQL 4 technically support transaction isolation, but it is affected by a bug
# where the transaction level gets persisted for the whole session: # where the transaction level gets persisted for the whole session:
# #
@ -253,10 +242,18 @@ def quoted_true
QUOTED_TRUE QUOTED_TRUE
end end
def unquoted_true
1
end
def quoted_false def quoted_false
QUOTED_FALSE QUOTED_FALSE
end end
def unquoted_false
0
end
# REFERENTIAL INTEGRITY ==================================== # REFERENTIAL INTEGRITY ====================================
def disable_referential_integrity #:nodoc: def disable_referential_integrity #:nodoc: