Permalink
Browse files

Merge pull request #15197 from sgrif/sg-delegate-type-cast-sqlite3

Delegate `#type_cast` to injected type objects on SQLite3
  • Loading branch information...
2 parents f9860fc + 36fde2b commit d17b05657153f4305e4112532c485239d49b77b8 @senny senny committed May 20, 2014
@@ -42,13 +42,21 @@ def sqlite3_connection(config)
module ConnectionAdapters #:nodoc:
class SQLite3Column < Column #:nodoc:
- class << self
- def binary_to_string(value)
- if value.encoding != Encoding::ASCII_8BIT
- value = value.force_encoding(Encoding::ASCII_8BIT)
- end
- value
+ def type_cast(value)
+ if encoded?
+ super
+ else
+ cast_type.type_cast(value)
+ end
+ end
+ end
+
+ class SQLite3Binary < Type::Binary # :nodoc:
+ def cast_value(value)
+ if value.encoding != Encoding::ASCII_8BIT
+ value = value.force_encoding(Encoding::ASCII_8BIT)
end
+ value
end
end
@@ -502,6 +510,12 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
end
protected
+
+ def initialize_type_map(m)
+ super
+ m.register_type(/binary/i, SQLite3Binary.new)
+ end
+
def select(sql, name = nil, binds = []) #:nodoc:
exec_query(sql, name, binds)
end
@@ -5,6 +5,12 @@ class Boolean < Value # :nodoc:
def type
:boolean
end
+
+ private
+
+ def cast_value(value)
+ Column.value_to_boolean(value)
+ end
end
end
end
@@ -5,6 +5,12 @@ class Date < Value # :nodoc:
def type
:date
end
+
+ private
+
+ def cast_value(value)
+ Column.value_to_date(value)
+ end
end
end
end
@@ -5,6 +5,12 @@ class DateTime < Value # :nodoc:
def type
:datetime
end
+
+ private
+
+ def cast_value(string)
+ Column.string_to_time(string)
+ end
end
end
end
@@ -5,6 +5,12 @@ class Decimal < Value # :nodoc:
def type
:decimal
end
+
+ private
+
+ def cast_value(value)
+ Column.value_to_decimal(value)
+ end
end
end
end
@@ -5,6 +5,12 @@ class Float < Value # :nodoc:
def type
:float
end
+
+ private
+
+ def cast_value(value)
+ value.to_f
+ end
end
end
end
@@ -5,6 +5,12 @@ class Integer < Value # :nodoc:
def type
:integer
end
+
+ private
+
+ def cast_value(value)
+ Column.value_to_integer(value)
+ end
end
end
end
@@ -5,6 +5,16 @@ class String < Value # :nodoc:
def type
:string
end
+
+ private
+
+ def cast_value(value)
+ case value
+ when true then "1"
+ when false then "0"
+ else value.to_s
+ end
+ end
end
end
end
@@ -5,6 +5,12 @@ class Time < Value # :nodoc:
def type
:time
end
+
+ private
+
+ def cast_value(value)
+ Column.string_to_dummy_time(value)
+ end
end
end
end
@@ -3,6 +3,16 @@ module ConnectionAdapters
module Type
class Value # :nodoc:
def type; end
+
+ def type_cast(value)
+ cast_value(value) unless value.nil?
+ end
+
+ private
+
+ def cast_value(value)
+ value
+ end
end
end
end
@@ -146,7 +146,7 @@ def test_string_to_time_with_timezone
if current_adapter?(:SQLite3Adapter)
def test_binary_encoding
- column = SQLite3Column.new("field", nil, Type::Binary.new)
+ column = SQLite3Column.new("field", nil, SQLite3Binary.new)
utf8_string = "a string".encode(Encoding::UTF_8)
type_cast = column.type_cast(utf8_string)

0 comments on commit d17b056

Please sign in to comment.