diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 16:02:10 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-05-17 16:12:06 -0400 |
commit | 7a51983efc50c8f9092785b1b586f8884dedc01a (patch) | |
tree | 5b915ee48c7aa454207312ba20bb13c4dadfdf70 | |
parent | 07833d39c2885a5cddf38eeb860d79353c0f447b (diff) | |
download | rails-7a51983efc50c8f9092785b1b586f8884dedc01a.tar.gz rails-7a51983efc50c8f9092785b1b586f8884dedc01a.tar.bz2 rails-7a51983efc50c8f9092785b1b586f8884dedc01a.zip |
initial implementation of cross-engine join
Conflicts:
lib/arel/engines/memory/relations/array.rb
lib/arel/engines/sql/primitives.rb
-rw-r--r-- | doc/TODO | 4 | ||||
-rw-r--r-- | lib/arel/algebra/relations/row.rb | 5 | ||||
-rw-r--r-- | lib/arel/engines/memory/relations/array.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/memory/relations/compound.rb | 2 | ||||
-rw-r--r-- | lib/arel/engines/memory/relations/operations.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/engine.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/primitives.rb | 4 | ||||
-rw-r--r-- | lib/arel/engines/sql/relations/table.rb | 4 | ||||
-rw-r--r-- | spec/arel/engines/memory/integration/joins/cross_engine_spec.rb | 31 |
9 files changed, 54 insertions, 8 deletions
@@ -1,8 +1,7 @@ todo: -- insertions for in memory - cross-engine joins - blocks for joins -- test sql insertions +- fix sql insertions - implement mnesia adapter - rename externalize to derived. @@ -94,6 +93,7 @@ done: - implement joins in memory - result sets should be array relations - fix AR +- insertions for in memory icebox: - #bind in Attribute and Expression should be doing a descend? diff --git a/lib/arel/algebra/relations/row.rb b/lib/arel/algebra/relations/row.rb index 3731dd9696..e8484944bd 100644 --- a/lib/arel/algebra/relations/row.rb +++ b/lib/arel/algebra/relations/row.rb @@ -4,12 +4,13 @@ module Arel deriving :==, :initialize def [](attribute) - tuple[relation.position_of(attribute)] + attribute.type_cast(tuple[relation.position_of(attribute)]) end def slice(*attributes) Row.new(relation, attributes.inject([]) do |cheese, attribute| - cheese << self[attribute] + # FIXME TESTME method chaining + cheese << tuple[relation.relation.position_of(attribute)] cheese end) end diff --git a/lib/arel/engines/memory/relations/array.rb b/lib/arel/engines/memory/relations/array.rb index 15a3e95e1b..6e2dc29252 100644 --- a/lib/arel/engines/memory/relations/array.rb +++ b/lib/arel/engines/memory/relations/array.rb @@ -14,6 +14,10 @@ module Arel end end + def format(attribute, value) + value + end + def eval @array.collect { |r| Row.new(self, r) } end diff --git a/lib/arel/engines/memory/relations/compound.rb b/lib/arel/engines/memory/relations/compound.rb index 3791fa4622..9e7827dfb3 100644 --- a/lib/arel/engines/memory/relations/compound.rb +++ b/lib/arel/engines/memory/relations/compound.rb @@ -3,7 +3,7 @@ module Arel delegate :array, :to => :relation def unoperated_rows - relation.eval.collect { |row| row.bind(self) } + relation.call.collect { |row| row.bind(self) } end end end diff --git a/lib/arel/engines/memory/relations/operations.rb b/lib/arel/engines/memory/relations/operations.rb index e35fbe3234..e0fd2824b3 100644 --- a/lib/arel/engines/memory/relations/operations.rb +++ b/lib/arel/engines/memory/relations/operations.rb @@ -47,8 +47,8 @@ module Arel class Join < Relation def eval result = [] - relation1.eval.each do |row1| - relation2.eval.each do |row2| + relation1.call.each do |row1| + relation2.call.each do |row2| combined_row = row1.combine(row2, self) if predicates.all? { |p| p.eval(combined_row) } result << combined_row diff --git a/lib/arel/engines/sql/engine.rb b/lib/arel/engines/sql/engine.rb index 0700ae9733..d27d93a5dc 100644 --- a/lib/arel/engines/sql/engine.rb +++ b/lib/arel/engines/sql/engine.rb @@ -20,7 +20,9 @@ module Arel def read(relation) # FIXME - class << rows = connection.execute(relation.to_sql) + rows = connection.select_rows(relation.to_sql) + + class << rows include Enumerable end diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb index 9e9143ac0f..22ee19dcf0 100644 --- a/lib/arel/engines/sql/primitives.rb +++ b/lib/arel/engines/sql/primitives.rb @@ -4,6 +4,10 @@ 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 2653744149..e842f85ed1 100644 --- a/lib/arel/engines/sql/relations/table.rb +++ b/lib/arel/engines/sql/relations/table.rb @@ -16,6 +16,10 @@ module Arel end 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 diff --git a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb new file mode 100644 index 0000000000..dd923ee6eb --- /dev/null +++ b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb @@ -0,0 +1,31 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') + +module Arel + describe Join do + before do + @users = Array.new([ + [1, 'bryan' ], + [2, 'emilio' ], + [3, 'nick'] + ], [:id, :name]) + @photos = Table.new(:photos) + @photos.delete + @photos \ + .insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) \ + .insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) + end + + it 'joins across engines' do + @users \ + .join(@photos) \ + .on(@users[:id].eq(@photos[:user_id])) \ + .project(@users[:name], @photos[:camera_id]) \ + .let do |relation| + relation.call.should == [ + Row.new(relation, ['bryan', '6']), + Row.new(relation, ['emilio', '42']) + ] + end + end + end +end
\ No newline at end of file |