From 8339f024c7663133a78c4d0a8824b5b6fafaf239 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 17 May 2009 15:42:42 -0400 Subject: recursive memory operations now possible Conflicts: lib/arel/algebra/relations/relation.rb --- lib/arel/algebra/extensions/hash.rb | 7 ------- lib/arel/algebra/relations.rb | 1 + lib/arel/algebra/relations/relation.rb | 4 ---- lib/arel/algebra/relations/row.rb | 21 +++++++++++++++++++++ lib/arel/engines/memory/predicates.rb | 6 ++++++ lib/arel/engines/memory/relations.rb | 1 + lib/arel/engines/memory/relations/array.rb | 2 +- lib/arel/engines/memory/relations/compound.rb | 4 ++++ lib/arel/engines/memory/relations/operations.rb | 10 +++++----- lib/arel/engines/memory/relations/relation.rb | 7 +++++++ 10 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 lib/arel/algebra/relations/row.rb create mode 100644 lib/arel/engines/memory/relations/relation.rb (limited to 'lib') diff --git a/lib/arel/algebra/extensions/hash.rb b/lib/arel/algebra/extensions/hash.rb index bc97785e62..7472b5aa73 100644 --- a/lib/arel/algebra/extensions/hash.rb +++ b/lib/arel/algebra/extensions/hash.rb @@ -4,11 +4,4 @@ class Hash bound.merge(key.bind(relation) => value.bind(relation)) end end - - def slice(*attributes) - inject({}) do |cheese, (key, value)| - cheese[key] = value if attributes.include?(key) - cheese - end - end end \ No newline at end of file diff --git a/lib/arel/algebra/relations.rb b/lib/arel/algebra/relations.rb index b75a31e5e3..94df5938fe 100644 --- a/lib/arel/algebra/relations.rb +++ b/lib/arel/algebra/relations.rb @@ -2,6 +2,7 @@ require 'arel/algebra/relations/relation' require 'arel/algebra/relations/utilities/compound' require 'arel/algebra/relations/utilities/nil' require 'arel/algebra/relations/utilities/externalization' +require 'arel/algebra/relations/row' require 'arel/algebra/relations/writes' require 'arel/algebra/relations/operations/alias' require 'arel/algebra/relations/operations/group' diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index 6d76e66638..fe8cab4b02 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -14,10 +14,6 @@ module Arel self end - def root - self - end - module Enumerable include ::Enumerable diff --git a/lib/arel/algebra/relations/row.rb b/lib/arel/algebra/relations/row.rb new file mode 100644 index 0000000000..2d63498452 --- /dev/null +++ b/lib/arel/algebra/relations/row.rb @@ -0,0 +1,21 @@ +module Arel + class Row + attributes :relation, :tuple + deriving :==, :initialize + + def [](attribute) + tuple[relation.position_of(attribute)] + end + + def slice(*attributes) + Row.new(relation, attributes.inject([]) do |cheese, attribute| + cheese << self[attribute] + cheese + end) + end + + def bind(relation) + Row.new(relation, tuple) + end + end +end \ No newline at end of file diff --git a/lib/arel/engines/memory/predicates.rb b/lib/arel/engines/memory/predicates.rb index bbf39ba794..e233f3ba39 100644 --- a/lib/arel/engines/memory/predicates.rb +++ b/lib/arel/engines/memory/predicates.rb @@ -21,15 +21,19 @@ module Arel end class Equality < Binary + def operator; :== end end class GreaterThanOrEqualTo < Binary + def operator; :>= end end class GreaterThan < Binary + def operator; :> end end class LessThanOrEqualTo < Binary + def operator; :<= end end class LessThan < Binary @@ -37,8 +41,10 @@ module Arel end class Match < Binary + def operator; :=~ end end class In < Binary + def operator; :include? end end end diff --git a/lib/arel/engines/memory/relations.rb b/lib/arel/engines/memory/relations.rb index 1b009537b9..820b0af4b2 100644 --- a/lib/arel/engines/memory/relations.rb +++ b/lib/arel/engines/memory/relations.rb @@ -1,3 +1,4 @@ +require 'arel/engines/memory/relations/relation' require 'arel/engines/memory/relations/array' require 'arel/engines/memory/relations/operations' require 'arel/engines/memory/relations/compound' diff --git a/lib/arel/engines/memory/relations/array.rb b/lib/arel/engines/memory/relations/array.rb index c02c62891b..ea0b5af5ba 100644 --- a/lib/arel/engines/memory/relations/array.rb +++ b/lib/arel/engines/memory/relations/array.rb @@ -15,7 +15,7 @@ module Arel end def eval - @array.collect { |row| attributes.zip(row).to_hash } + @array.collect { |r| Row.new(self, r) } end end end \ No newline at end of file diff --git a/lib/arel/engines/memory/relations/compound.rb b/lib/arel/engines/memory/relations/compound.rb index b029082d57..3791fa4622 100644 --- a/lib/arel/engines/memory/relations/compound.rb +++ b/lib/arel/engines/memory/relations/compound.rb @@ -1,5 +1,9 @@ module Arel class Compound < Relation delegate :array, :to => :relation + + def unoperated_rows + relation.eval.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 115df054df..8e03aca7b1 100644 --- a/lib/arel/engines/memory/relations/operations.rb +++ b/lib/arel/engines/memory/relations/operations.rb @@ -1,13 +1,13 @@ module Arel class Where < Compound def eval - relation.eval.select { |row| predicate.eval(row) } + unoperated_rows.select { |row| predicate.eval(row) } end end class Order < Compound def eval - relation.eval.sort do |row1, row2| + unoperated_rows.sort do |row1, row2| ordering = orderings.detect { |o| o.eval(row1, row2) != 0 } || orderings.last ordering.eval(row1, row2) end @@ -16,19 +16,19 @@ module Arel class Project < Compound def eval - relation.eval.collect { |r| r.slice(*projections) } + unoperated_rows.collect { |r| r.slice(*projections) } end end class Take < Compound def eval - relation.eval[0, taken] + unoperated_rows[0, taken] end end class Skip < Compound def eval - relation.eval[skipped..-1] + unoperated_rows[skipped..-1] end end diff --git a/lib/arel/engines/memory/relations/relation.rb b/lib/arel/engines/memory/relations/relation.rb new file mode 100644 index 0000000000..abfb8bb37f --- /dev/null +++ b/lib/arel/engines/memory/relations/relation.rb @@ -0,0 +1,7 @@ +module Arel + class Relation + def position_of(attribute) + attributes.index(self[attribute]) + end + end +end \ No newline at end of file -- cgit v1.2.3