diff options
Diffstat (limited to 'lib/arel/algebra/relations')
-rw-r--r-- | lib/arel/algebra/relations/operations/from.rb | 14 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/group.rb | 14 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/having.rb | 14 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/join.rb | 103 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/lock.rb | 10 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/order.rb | 23 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/project.rb | 20 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/skip.rb | 14 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/take.rb | 18 | ||||
-rw-r--r-- | lib/arel/algebra/relations/operations/where.rb | 23 | ||||
-rw-r--r-- | lib/arel/algebra/relations/relation.rb | 205 | ||||
-rw-r--r-- | lib/arel/algebra/relations/row.rb | 29 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/compound.rb | 55 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/externalization.rb | 26 | ||||
-rw-r--r-- | lib/arel/algebra/relations/utilities/nil.rb | 7 | ||||
-rw-r--r-- | lib/arel/algebra/relations/writes.rb | 47 |
16 files changed, 0 insertions, 622 deletions
diff --git a/lib/arel/algebra/relations/operations/from.rb b/lib/arel/algebra/relations/operations/from.rb deleted file mode 100644 index 3ebfb10cb2..0000000000 --- a/lib/arel/algebra/relations/operations/from.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Arel - class From < Compound - attr_reader :sources - - def initialize relation, sources - super(relation) - @sources = sources - end - - def eval - unoperated_rows[sources..-1] - end - end -end diff --git a/lib/arel/algebra/relations/operations/group.rb b/lib/arel/algebra/relations/operations/group.rb deleted file mode 100644 index 16a2963c93..0000000000 --- a/lib/arel/algebra/relations/operations/group.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Arel - class Group < Compound - attr_reader :groupings - - def initialize(relation, groupings) - super(relation) - @groupings = groupings.collect { |g| g.bind(relation) } - end - - def eval - raise NotImplementedError - end - end -end diff --git a/lib/arel/algebra/relations/operations/having.rb b/lib/arel/algebra/relations/operations/having.rb deleted file mode 100644 index 3d8115c2f6..0000000000 --- a/lib/arel/algebra/relations/operations/having.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Arel - class Having < Compound - attr_reader :predicates - - def initialize(relation, predicates) - super(relation) - @predicates = predicates.map { |p| p.bind(relation) } - end - - def havings - @havings ||= relation.havings + predicates - end - end -end diff --git a/lib/arel/algebra/relations/operations/join.rb b/lib/arel/algebra/relations/operations/join.rb deleted file mode 100644 index 35374972be..0000000000 --- a/lib/arel/algebra/relations/operations/join.rb +++ /dev/null @@ -1,103 +0,0 @@ -module Arel - class Join - include Relation - - attr_reader :relation1, :relation2, :predicates - - def initialize(relation1, relation2 = Nil.instance, *predicates) - @relation1 = relation1 - @relation2 = relation2 - @predicates = predicates - @attributes = nil - end - - def name - relation1.name - end - - def attributes - @attributes ||= (relation1.externalize.attributes | relation2.externalize.attributes).bind(self) - end - - def wheres - # TESTME bind to self? - relation1.externalize.wheres - end - - def ons - @ons ||= @predicates.collect { |p| p.bind(self) } - end - - # TESTME - def externalizable? - relation1.externalizable? or relation2.externalizable? - end - - def join? - true - end - - def engine - relation1.engine != relation2.engine ? Memory::Engine.new : relation1.engine - end - - def table_sql(formatter = Sql::TableReference.new(self)) - relation1.externalize.table_sql(formatter) - end - - def joins(environment, formatter = Sql::TableReference.new(environment)) - @joins ||= begin - this_join = [ - join_sql, - relation2.externalize.table_sql(formatter), - ("ON" unless predicates.blank?), - (ons + relation2.externalize.wheres).collect { |p| p.bind(environment.relation).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') - ].compact.join(" ") - [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") - end - end - - def eval - result = [] - 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 - end - end - end - result - end - - def to_sql(formatter = nil) - compiler.select_sql - end - end - - class InnerJoin < Join - def join_sql; "INNER JOIN" end - end - - class OuterJoin < Join - def join_sql; "LEFT OUTER JOIN" end - end - - class StringJoin < Join - def joins(environment, formatter = Sql::TableReference.new(environment)) - [relation1.joins(environment), relation2].compact.join(" ") - end - - def externalizable? - relation1.externalizable? - end - - def attributes - relation1.externalize.attributes - end - - def engine - relation1.engine - end - end -end diff --git a/lib/arel/algebra/relations/operations/lock.rb b/lib/arel/algebra/relations/operations/lock.rb deleted file mode 100644 index 394ad6f0ec..0000000000 --- a/lib/arel/algebra/relations/operations/lock.rb +++ /dev/null @@ -1,10 +0,0 @@ -module Arel - class Lock < Compound - attr_reader :locked - - def initialize(relation, locked) - super(relation) - @locked = true == locked ? " FOR UPDATE" : locked - end - end -end diff --git a/lib/arel/algebra/relations/operations/order.rb b/lib/arel/algebra/relations/operations/order.rb deleted file mode 100644 index e7306fe384..0000000000 --- a/lib/arel/algebra/relations/operations/order.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Arel - class Order < Compound - attr_reader :orderings - - def initialize(relation, orderings) - super(relation) - @orderings = orderings.collect { |o| o.bind(relation) } - end - - # TESTME - def orders - # QUESTION - do we still need relation.orders ? - (orderings + relation.orders).collect { |o| o.bind(self) }.collect { |o| o.to_ordering } - end - - def eval - unoperated_rows.sort do |row1, row2| - ordering = orders.detect { |o| o.eval(row1, row2) != 0 } || orders.last - ordering.eval(row1, row2) - end - end - end -end diff --git a/lib/arel/algebra/relations/operations/project.rb b/lib/arel/algebra/relations/operations/project.rb deleted file mode 100644 index 1c24ae6caf..0000000000 --- a/lib/arel/algebra/relations/operations/project.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Arel - class Project < Compound - attr_reader :projections, :attributes, :christener - - def initialize(relation, projections) - super(relation) - @projections = projections.map { |p| p.bind(relation) } - @christener = Sql::Christener.new - @attributes = Header.new(projections.map { |x| x.bind(self) }) - end - - def externalizable? - attributes.any? { |a| a.respond_to?(:aggregation?) && a.aggregation? } || relation.externalizable? - end - - def eval - unoperated_rows.collect { |r| r.slice(*projections) } - end - end -end diff --git a/lib/arel/algebra/relations/operations/skip.rb b/lib/arel/algebra/relations/operations/skip.rb deleted file mode 100644 index a9157e914c..0000000000 --- a/lib/arel/algebra/relations/operations/skip.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Arel - class Skip < Compound - attr_reader :relation, :skipped - - def initialize relation, skipped - super(relation) - @skipped = skipped - end - - def eval - unoperated_rows[skipped..-1] - end - end -end diff --git a/lib/arel/algebra/relations/operations/take.rb b/lib/arel/algebra/relations/operations/take.rb deleted file mode 100644 index 299c29ab72..0000000000 --- a/lib/arel/algebra/relations/operations/take.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Arel - class Take < Compound - attr_reader :taken - - def initialize relation, taken - super(relation) - @taken = taken - end - - def externalizable? - true - end - - def eval - unoperated_rows[0, taken] - end - end -end diff --git a/lib/arel/algebra/relations/operations/where.rb b/lib/arel/algebra/relations/operations/where.rb deleted file mode 100644 index ed80fd93ba..0000000000 --- a/lib/arel/algebra/relations/operations/where.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Arel - class Where < Compound - attr_reader :predicates - - def initialize(relation, predicates) - super(relation) - @predicates = predicates.map { |p| p.bind(relation) } - @wheres = nil - end - - def wheres - @wheres ||= relation.wheres + predicates - end - - def eval - unoperated_rows.select { |row| predicates.all? { |p| p.eval(row) } } - end - - def to_sql(formatter = nil) - compiler.select_sql - end - end -end diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb deleted file mode 100644 index 765d6fe930..0000000000 --- a/lib/arel/algebra/relations/relation.rb +++ /dev/null @@ -1,205 +0,0 @@ -module Arel - module Relation - include Enumerable - - @@connection_tables_primary_keys = {} - - attr_reader :count - - def session - Session.instance - end - - def join? - false - end - - def call - engine.read(self) - end - - def bind(relation) - self - end - - def externalize - @externalized ||= externalizable?? Externalization.new(self) : self - end - - def externalizable? - false - end - - def compiler - @compiler ||= begin - Arel::SqlCompiler.const_get("#{engine.adapter_name}Compiler").new(self) - rescue - Arel::SqlCompiler::GenericCompiler.new(self) - end - end - - def to_sql(formatter = nil) - sql = compiler.select_sql - - return sql unless formatter - formatter.select sql, self - end - - def christener - @christener ||= Sql::Christener.new - end - - def inclusion_predicate_sql - "IN" - end - - def exclusion_predicate_sql - "NOT IN" - end - - def primary_key - connection_id = engine.connection.object_id - if @@connection_tables_primary_keys[connection_id] && @@connection_tables_primary_keys[connection_id].has_key?(table.name) - @@connection_tables_primary_keys[connection_id][table.name] - else - @@connection_tables_primary_keys[connection_id] ||= {} - @@connection_tables_primary_keys[connection_id][table.name] = engine.connection.primary_key(table.name) - end - end - - def select_clauses - attributes.map { |a| - case a - when Value - a.value - else - a.to_sql(Sql::SelectClause.new(self)) - end - } - end - - def from_clauses - sources.empty? ? table_sql : sources - end - - def where_clauses - wheres.map { |w| w.value } - end - - def group_clauses - groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) } - end - - def having_clauses - havings.collect { |g| g.to_sql(Sql::HavingClause.new(self)) } - end - - def order_clauses - orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) } - end - - def each - session.read(self).each { |e| yield e } - end - - def join(other_relation = nil, join_class = InnerJoin) - case other_relation - when String - StringJoin.new(self, other_relation) - when Relation - JoinOperation.new(join_class, self, other_relation) - else - self - end - end - - def outer_join(other_relation = nil) - join(other_relation, OuterJoin) - end - - %w{ - having group order - }.each do |op| - class_eval <<-OPERATION, __FILE__, __LINE__ - def #{op}(*args) - args.all? { |x| x.blank? } ? self : #{op.capitalize}.new(self, args) - end - OPERATION - end - - def project *args - args.empty? ? self : Project.new(self, args) - end - - def where clause = nil - clause ? Where.new(self, Array(clause)) : self - end - - def skip thing = nil - thing ? Skip.new(self, thing) : self - end - - def take count - Take.new self, count - end - - def from thing - From.new self, thing - end - - def lock(locking = true) - Lock.new(self, locking) - end - - def alias - Alias.new(self) - end - - def insert(record) - session.create Insert.new(self, record) - end - - def update(assignments) - session.update Update.new(self, assignments) - end - - def delete - session.delete Deletion.new(self) - end - - JoinOperation = Struct.new(:join_class, :relation1, :relation2) do - def on(*predicates) - join_class.new(relation1, relation2, *predicates) - end - end - - def [](index) - attributes[index] - end - - def find_attribute_matching_name(name) - attributes.detect { |a| a.named?(name) } || Attribute.new(self, name) - end - - def position_of(attribute) - @position_of ||= {} - - return @position_of[attribute] if @position_of.key? attribute - - @position_of[attribute] = attributes.index(attributes[attribute]) - end - - def attributes; Header.new end - def projections; [] end - def wheres; [] end - def orders; [] end - def inserts; [] end - def groupings; [] end - def havings; [] end - def joins(formatter = nil); nil end # FIXME - def taken; nil end - def skipped; nil end - def sources; [] end - def locked; [] end - end -end diff --git a/lib/arel/algebra/relations/row.rb b/lib/arel/algebra/relations/row.rb deleted file mode 100644 index 84f19e372f..0000000000 --- a/lib/arel/algebra/relations/row.rb +++ /dev/null @@ -1,29 +0,0 @@ -module Arel - class Row - attr_reader :tuple, :relation - - def initialize relation, tuple - @relation = relation - @tuple = tuple - end - - def [](attribute) - attribute.type_cast(tuple[relation.position_of(attribute)]) - end - - def slice(*attributes) - Row.new(relation, attributes.map do |attribute| - # FIXME TESTME method chaining - tuple[relation.relation.position_of(attribute)] - end) - end - - def bind(relation) - Row.new(relation, tuple) - end - - def combine(other, relation) - Row.new(relation, tuple + other.tuple) - end - end -end diff --git a/lib/arel/algebra/relations/utilities/compound.rb b/lib/arel/algebra/relations/utilities/compound.rb deleted file mode 100644 index 0a270f78a1..0000000000 --- a/lib/arel/algebra/relations/utilities/compound.rb +++ /dev/null @@ -1,55 +0,0 @@ -module Arel - class Compound - include Relation - - attr_reader :relation, :engine - - def initialize relation - @relation = relation - @engine = relation.engine - @attributes = nil - @wheres = nil - @groupings = nil - @orders = nil - @havings = nil - @projections = nil - end - - def join?; @relation.join? end - def name; @relation.name end - def table_alias; @relation.table_alias end - def skipped; @relation.skipped end - def taken; @relation.taken end - def joins env; @relation.joins env end - def column_for attr; @relation.column_for attr end - def externalizable?; @relation.externalizable? end - - def sources - @relation.sources - end - - def table - @relation.table - end - - def table_sql(formatter = Sql::TableReference.new(self)) - @relation.table_sql formatter - end - - [:wheres, :groupings, :orders, :havings, :projections].each do |operation_name| - class_eval <<-OPERATION, __FILE__, __LINE__ - def #{operation_name} - @#{operation_name} ||= relation.#{operation_name}.collect { |o| o.bind(self) } - end - OPERATION - end - - def attributes - @attributes ||= relation.attributes.bind(self) - end - - def unoperated_rows - relation.call.collect { |row| row.bind(self) } - end - end -end diff --git a/lib/arel/algebra/relations/utilities/externalization.rb b/lib/arel/algebra/relations/utilities/externalization.rb deleted file mode 100644 index 8e97573f68..0000000000 --- a/lib/arel/algebra/relations/utilities/externalization.rb +++ /dev/null @@ -1,26 +0,0 @@ -module Arel - class Externalization < Compound - include Recursion::BaseCase - - def == other - super || Externalization === other && relation == other.relation - end - - def wheres - [] - end - - def attributes - @attributes ||= Header.new(relation.attributes.map { |a| a.to_attribute(self) }) - end - - def table_sql(formatter = Sql::TableReference.new(relation)) - formatter.select relation.compiler.select_sql, self - end - - # REMOVEME - def name - relation.name + '_external' - end - end -end diff --git a/lib/arel/algebra/relations/utilities/nil.rb b/lib/arel/algebra/relations/utilities/nil.rb deleted file mode 100644 index 04055d0ddb..0000000000 --- a/lib/arel/algebra/relations/utilities/nil.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'singleton' - -module Arel - class Nil - include Relation, Singleton - end -end diff --git a/lib/arel/algebra/relations/writes.rb b/lib/arel/algebra/relations/writes.rb deleted file mode 100644 index 7df6d861a8..0000000000 --- a/lib/arel/algebra/relations/writes.rb +++ /dev/null @@ -1,47 +0,0 @@ -module Arel - class Action < Compound - end - - class Deletion < Action - def call - engine.delete(self) - end - - def to_sql - compiler.delete_sql - end - end - - class Insert < Action - attr_reader :record - - def initialize(relation, record) - super(relation) - @record = record.bind(relation) - end - - def call - engine.create(self) - end - - def eval - unoperated_rows + [Row.new(self, record.values.collect(&:value))] - end - - def to_sql(include_returning = true) - compiler.insert_sql(include_returning) - end - end - - class Update < Insert - alias :assignments :record - - def call - engine.update(self) - end - - def to_sql - compiler.update_sql - end - end -end |