aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/engines
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2010-03-12 12:51:20 -0800
committerCarl Lerche <carllerche@mac.com>2010-03-12 12:52:07 -0800
commit83c27c0b5e2e341307b7a160d831fb930a9552b4 (patch)
treefc41004d425cbc9827e82f550959ed40e8c12978 /lib/arel/engines
parent602722922c8365afcb3e9bed3721d61756322353 (diff)
downloadrails-83c27c0b5e2e341307b7a160d831fb930a9552b4.tar.gz
rails-83c27c0b5e2e341307b7a160d831fb930a9552b4.tar.bz2
rails-83c27c0b5e2e341307b7a160d831fb930a9552b4.zip
Attributes should be typed
Diffstat (limited to 'lib/arel/engines')
-rw-r--r--lib/arel/engines/memory/relations/array.rb11
-rw-r--r--lib/arel/engines/sql.rb1
-rw-r--r--lib/arel/engines/sql/attributes.rb40
-rw-r--r--lib/arel/engines/sql/primitives.rb4
-rw-r--r--lib/arel/engines/sql/relations/table.rb8
5 files changed, 52 insertions, 12 deletions
diff --git a/lib/arel/engines/memory/relations/array.rb b/lib/arel/engines/memory/relations/array.rb
index 5e7c0a4ab1..577e327b19 100644
--- a/lib/arel/engines/memory/relations/array.rb
+++ b/lib/arel/engines/memory/relations/array.rb
@@ -1,16 +1,21 @@
module Arel
class Array < Relation
- attributes :array, :attribute_names
+ attributes :array, :attribute_names_and_types
include Recursion::BaseCase
deriving :==, :initialize
+ def initialize(array, attribute_names_and_types)
+ @array, @attribute_names_and_types = array, attribute_names_and_types
+ end
+
def engine
@engine ||= Memory::Engine.new
end
def attributes
- @attributes ||= @attribute_names.collect do |name|
- name.to_attribute(self)
+ @attributes ||= @attribute_names_and_types.collect do |attribute, type|
+ attribute = type.new(self, attribute) if Symbol === attribute
+ attribute
end
end
diff --git a/lib/arel/engines/sql.rb b/lib/arel/engines/sql.rb
index dc40428b77..a7721eb909 100644
--- a/lib/arel/engines/sql.rb
+++ b/lib/arel/engines/sql.rb
@@ -1,3 +1,4 @@
+require 'arel/engines/sql/attributes'
require 'arel/engines/sql/engine'
require 'arel/engines/sql/relations'
require 'arel/engines/sql/primitives'
diff --git a/lib/arel/engines/sql/attributes.rb b/lib/arel/engines/sql/attributes.rb
new file mode 100644
index 0000000000..2d315d53fc
--- /dev/null
+++ b/lib/arel/engines/sql/attributes.rb
@@ -0,0 +1,40 @@
+module Arel
+ module Sql
+ module Attributes
+ def self.for(column)
+ case column.type
+ when :string then String
+ when :text then String
+ when :integer then Integer
+ when :float then Float
+ when :decimal then Decimal
+ when :date then Time
+ when :datetime then Time
+ when :timestamp then Time
+ when :time then Time
+ when :binary then String
+ when :boolean then Boolean
+ else
+ raise NotImplementedError, "Column type `#{column.type}` is not currently handled"
+ end
+ end
+
+ def initialize(column, *args)
+ @column = column
+ super(*args)
+ end
+
+ def type_cast(value)
+ @column.type_cast(value)
+ end
+
+ %w(Boolean Decimal Float Integer String Time).each do |klass|
+ class_eval <<-R
+ class #{klass} < Arel::Attributes::#{klass}
+ include Attributes
+ end
+ R
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb
index 6cce46a441..666579331a 100644
--- a/lib/arel/engines/sql/primitives.rb
+++ b/lib/arel/engines/sql/primitives.rb
@@ -16,10 +16,6 @@ module Arel
original_relation.column_for(self)
end
- def type_cast(value)
- root.relation.format(self, value)
- end
-
def format(object)
object.to_sql(Sql::Attribute.new(self))
end
diff --git a/lib/arel/engines/sql/relations/table.rb b/lib/arel/engines/sql/relations/table.rb
index d10b761ea3..c0d3386463 100644
--- a/lib/arel/engines/sql/relations/table.rb
+++ b/lib/arel/engines/sql/relations/table.rb
@@ -41,7 +41,9 @@ module Arel
def attributes
return @attributes if defined?(@attributes)
if table_exists?
- @attributes = columns.collect { |column| Attribute.new(self, column.name.to_sym) }
+ @attributes = columns.collect do |column|
+ Sql::Attributes.for(column).new(column, self, column.name.to_sym)
+ end
else
[]
end
@@ -55,10 +57,6 @@ module Arel
@hash ||= :name.hash
end
- def format(attribute, value)
- attribute.column.type_cast(value)
- end
-
def column_for(attribute)
has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s }
end