diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-13 14:03:35 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-04-14 13:37:38 -0700 |
commit | 4893170da20eee28c016408a0f72f1996343a048 (patch) | |
tree | cc834b2f684912496ea3b50ed5964bc0d19c2e03 /activerecord/lib/active_record/connection_adapters/abstract/quoting.rb | |
parent | 6b6ecbefad648f39b507dbb59c9d22ff9031f7a8 (diff) | |
download | rails-4893170da20eee28c016408a0f72f1996343a048.tar.gz rails-4893170da20eee28c016408a0f72f1996343a048.tar.bz2 rails-4893170da20eee28c016408a0f72f1996343a048.zip |
adding a type cast method for prepared statements
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract/quoting.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/quoting.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 325665dd87..5634e53e80 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -39,6 +39,42 @@ module ActiveRecord end end + # Cast a +value+ to a type that the database understands. For example, + # SQLite does not understand dates, so this method will convert a Date + # to a String. + def type_cast(value, column) + return value.id if value.respond_to?(:quoted_id) + + case value + when String, ActiveSupport::Multibyte::Chars + value = value.to_s + return value unless column + + case column.type + when :binary then value + when :integer then value.to_i + when :float then value.to_f + else + value + end + + when true, false + if column && column.type == :integer + value ? 1 : 0 + else + value ? 't' : 'f' + end + # BigDecimals need to be put in a non-normalized form and quoted. + when nil then nil + when BigDecimal then value.to_f + when Numeric then value + when Date, Time then quoted_date(value) + when Symbol then value.to_s + else + YAML.dump(value) + end + end + # Quotes a string, escaping any ' (single quote) and \ (backslash) # characters. def quote_string(s) |