mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added "from" method, allowing to specify custom from clauses.
This commit is contained in:
parent
a5e3ac2c2d
commit
49a8c7bd51
8 changed files with 76 additions and 4 deletions
|
@ -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'
|
||||
|
|
6
lib/arel/algebra/relations/operations/from.rb
Normal file
6
lib/arel/algebra/relations/operations/from.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
module Arel
|
||||
class From < Compound
|
||||
attributes :relation, :sources
|
||||
deriving :initialize, :==
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
50
spec/arel/engines/sql/unit/relations/from_spec.rb
Normal file
50
spec/arel/engines/sql/unit/relations/from_spec.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
require 'spec_helper'
|
||||
|
||||
module Arel
|
||||
describe Table do
|
||||
before do
|
||||
@relation = Table.new(:users)
|
||||
end
|
||||
|
||||
describe '#to_sql' do
|
||||
it "manufactures a simple select query" do
|
||||
sql = @relation.from("workers").to_sql
|
||||
|
||||
adapter_is :mysql do
|
||||
sql.should be_like(%Q{
|
||||
SELECT `users`.`id`, `users`.`name`
|
||||
FROM workers
|
||||
})
|
||||
end
|
||||
|
||||
adapter_is_not :mysql do
|
||||
sql.should be_like(%Q{
|
||||
SELECT "users"."id", "users"."name"
|
||||
FROM workers
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#to_sql' do
|
||||
it "overrides and use last from clause given " do
|
||||
sql = @relation.from("workers").from("users").to_sql
|
||||
|
||||
adapter_is :mysql do
|
||||
sql.should be_like(%Q{
|
||||
SELECT `users`.`id`, `users`.`name`
|
||||
FROM users
|
||||
})
|
||||
end
|
||||
|
||||
adapter_is_not :mysql do
|
||||
sql.should be_like(%Q{
|
||||
SELECT "users"."id", "users"."name"
|
||||
FROM users
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue