aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-04-13 14:03:35 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-04-14 13:37:38 -0700
commit4893170da20eee28c016408a0f72f1996343a048 (patch)
treecc834b2f684912496ea3b50ed5964bc0d19c2e03 /activerecord/lib/active_record/connection_adapters
parent6b6ecbefad648f39b507dbb59c9d22ff9031f7a8 (diff)
downloadrails-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')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/quoting.rb36
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb2
2 files changed, 37 insertions, 1 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)
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 8ef286b473..14c4fd689b 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -165,7 +165,7 @@ module ActiveRecord
cols = cache[:cols] ||= stmt.columns
stmt.reset!
stmt.bind_params binds.map { |col, val|
- col ? col.type_cast(val) : val
+ type_cast(val, col)
}
end