aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-12-27 16:38:32 -0300
committerEmilio Tagua <miloops@gmail.com>2009-12-27 16:38:32 -0300
commit49a8c7bd5168ca84897166527f5c0d90126dc402 (patch)
tree73d803eddb1dffa6c0f31bdd3a203e607ee00686 /lib
parenta5e3ac2c2d20ea1335a1aa48e6813272a39bc3d3 (diff)
downloadrails-49a8c7bd5168ca84897166527f5c0d90126dc402.tar.gz
rails-49a8c7bd5168ca84897166527f5c0d90126dc402.tar.bz2
rails-49a8c7bd5168ca84897166527f5c0d90126dc402.zip
Added "from" method, allowing to specify custom from clauses.
Diffstat (limited to 'lib')
-rw-r--r--lib/arel/algebra/relations.rb1
-rw-r--r--lib/arel/algebra/relations/operations/from.rb6
-rw-r--r--lib/arel/algebra/relations/relation.rb3
-rw-r--r--lib/arel/algebra/relations/utilities/compound.rb2
-rw-r--r--lib/arel/engines/memory/relations/operations.rb6
-rw-r--r--lib/arel/engines/sql/formatters.rb4
-rw-r--r--lib/arel/engines/sql/relations/relation.rb8
7 files changed, 26 insertions, 4 deletions
diff --git a/lib/arel/algebra/relations.rb b/lib/arel/algebra/relations.rb
index f9fa24ba25..cee308299b 100644
--- a/lib/arel/algebra/relations.rb
+++ b/lib/arel/algebra/relations.rb
@@ -5,6 +5,7 @@ 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/from'
require 'arel/algebra/relations/operations/group'
require 'arel/algebra/relations/operations/join'
require 'arel/algebra/relations/operations/order'
diff --git a/lib/arel/algebra/relations/operations/from.rb b/lib/arel/algebra/relations/operations/from.rb
new file mode 100644
index 0000000000..6bfd68dfc9
--- /dev/null
+++ b/lib/arel/algebra/relations/operations/from.rb
@@ -0,0 +1,6 @@
+module Arel
+ class From < Compound
+ attributes :relation, :sources
+ deriving :initialize, :==
+ end
+end
diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb
index 5403e40fae..2ce3fdcce8 100644
--- a/lib/arel/algebra/relations/relation.rb
+++ b/lib/arel/algebra/relations/relation.rb
@@ -43,7 +43,7 @@ module Arel
join(other_relation, OuterJoin)
end
- [:where, :project, :order, :take, :skip, :group].each do |operation_name|
+ [:where, :project, :order, :take, :skip, :group, :from].each do |operation_name|
class_eval <<-OPERATION, __FILE__, __LINE__
def #{operation_name}(*arguments, &block)
arguments.all?(&:blank?) && !block_given?? self : #{operation_name.to_s.classify}.new(self, *arguments, &block)
@@ -130,6 +130,7 @@ module Arel
def joins(formatter = nil); nil end # FIXME
def taken; nil end
def skipped; nil end
+ def sources; [] end
end
include DefaultOperations
end
diff --git a/lib/arel/algebra/relations/utilities/compound.rb b/lib/arel/algebra/relations/utilities/compound.rb
index 5e775618f1..06bfce4ac0 100644
--- a/lib/arel/algebra/relations/utilities/compound.rb
+++ b/lib/arel/algebra/relations/utilities/compound.rb
@@ -2,7 +2,7 @@ module Arel
class Compound < Relation
attr_reader :relation
delegate :joins, :join?, :inserts, :taken, :skipped, :name, :externalizable?,
- :column_for, :engine,
+ :column_for, :engine, :sources,
:to => :relation
[:attributes, :wheres, :groupings, :orders].each do |operation_name|
diff --git a/lib/arel/engines/memory/relations/operations.rb b/lib/arel/engines/memory/relations/operations.rb
index 8e01938360..a5082715a1 100644
--- a/lib/arel/engines/memory/relations/operations.rb
+++ b/lib/arel/engines/memory/relations/operations.rb
@@ -32,6 +32,12 @@ module Arel
end
end
+ class From < Compound
+ def eval
+ unoperated_rows[sources..-1]
+ end
+ end
+
class Group < Compound
def eval
raise NotImplementedError
diff --git a/lib/arel/engines/sql/formatters.rb b/lib/arel/engines/sql/formatters.rb
index 626803a887..a13e11f24f 100644
--- a/lib/arel/engines/sql/formatters.rb
+++ b/lib/arel/engines/sql/formatters.rb
@@ -98,6 +98,10 @@ module Arel
(table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '')
end
end
+
+ def attribute(attribute)
+ attribute
+ end
end
class Attribute < WhereCondition
diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb
index deca9b19cb..448cf7951d 100644
--- a/lib/arel/engines/sql/relations/relation.rb
+++ b/lib/arel/engines/sql/relations/relation.rb
@@ -13,7 +13,7 @@ module Arel
query = build_query \
"SELECT #{select_clauses.kind_of?(::Array) ? select_clauses.join("") : select_clauses.to_s}",
- "FROM #{table_sql(Sql::TableReference.new(self))}",
+ "FROM #{from_clauses}",
(joins(self) unless joins(self).blank? ),
("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ),
("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? )
@@ -27,7 +27,7 @@ module Arel
else
build_query \
"SELECT #{select_clauses.join(', ')}",
- "FROM #{table_sql(Sql::TableReference.new(self))}",
+ "FROM #{from_clauses}",
(joins(self) unless joins(self).blank? ),
("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ),
("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ),
@@ -51,6 +51,10 @@ module Arel
parts.compact.join(" ")
end
+ def from_clauses
+ sources.blank? ? table_sql(Sql::TableReference.new(self)) : sources
+ end
+
def select_clauses
attributes.collect { |a| a.to_sql(Sql::SelectClause.new(self)) }
end