Permalink
Browse files

initial implementation of cross-engine join

Conflicts:

	lib/arel/engines/memory/relations/array.rb
	lib/arel/engines/sql/primitives.rb
  • Loading branch information...
1 parent 07833d3 commit 7a51983efc50c8f9092785b1b586f8884dedc01a @brynary brynary committed May 17, 2009
View
@@ -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?
@@ -4,12 +4,13 @@ class Row
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
@@ -14,6 +14,10 @@ def attributes
end
end
+ def format(attribute, value)
+ value
+ end
+
def eval
@array.collect { |r| Row.new(self, r) }
end
@@ -3,7 +3,7 @@ class Compound < Relation
delegate :array, :to => :relation
def unoperated_rows
- relation.eval.collect { |row| row.bind(self) }
+ relation.call.collect { |row| row.bind(self) }
end
end
end
@@ -47,8 +47,8 @@ def eval
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
@@ -20,7 +20,9 @@ def create(relation)
def read(relation)
# FIXME
- class << rows = connection.execute(relation.to_sql)
+ rows = connection.select_rows(relation.to_sql)
+
+ class << rows
include Enumerable
end
@@ -4,6 +4,10 @@ def column
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
@@ -16,6 +16,10 @@ def attributes
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
@@ -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

0 comments on commit 7a51983

Please sign in to comment.