1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/arel/table.rb

114 lines
2.4 KiB
Ruby
Raw Normal View History

module Arel
2010-08-12 14:24:16 -04:00
class Table
include Arel::Crud
include Arel::FactoryMethods
2010-08-12 14:24:16 -04:00
@engine = nil
class << self; attr_accessor :engine; end
2010-09-27 16:59:50 -04:00
attr_accessor :name, :engine, :aliases, :table_alias
2010-08-12 18:40:58 -04:00
# TableAlias and Table both have a #table_name which is the name of the underlying table
alias :table_name :name
def initialize name, options = {}
@name = name.to_s
@columns = nil
2010-08-18 19:32:32 -04:00
@aliases = []
@engine = Table.engine
# Sometime AR sends an :as parameter to table, to let the table know
# that it is an Alias. We may want to override new, and return a
# TableAlias node?
@table_alias = options[:as]
if @table_alias.to_s == @name
@table_alias = nil
2010-09-21 16:50:53 -04:00
end
2010-08-18 19:32:32 -04:00
end
2010-11-24 15:01:05 -05:00
def alias name = "#{self.name}_2"
Nodes::TableAlias.new(self, name).tap do |node|
2010-08-18 19:32:32 -04:00
@aliases << node
end
2010-08-12 14:24:16 -04:00
end
def from engine = Table.engine
SelectManager.new(engine, self)
2010-09-08 20:32:44 -04:00
end
2010-09-07 17:49:06 -04:00
def join relation, klass = Nodes::InnerJoin
return from unless relation
case relation
when String, Nodes::SqlLiteral
raise if relation.empty?
klass = Nodes::StringJoin
end
from.join(relation, klass)
2010-08-18 19:32:32 -04:00
end
2013-05-12 13:09:07 -04:00
def outer_join relation
join(relation, Nodes::OuterJoin)
end
2010-09-07 19:07:37 -04:00
def group *columns
from.group(*columns)
2010-09-07 19:07:37 -04:00
end
2010-09-06 20:17:49 -04:00
def order *expr
from.order(*expr)
2010-09-06 20:17:49 -04:00
end
def where condition
from.where condition
end
def project *things
from.project(*things)
2010-08-13 17:13:38 -04:00
end
2010-08-16 18:02:37 -04:00
def take amount
from.take amount
2010-08-16 18:02:37 -04:00
end
def skip amount
from.skip amount
end
2010-09-08 18:29:22 -04:00
def having expr
from.having expr
2010-09-08 18:29:22 -04:00
end
2010-08-12 17:55:31 -04:00
def [] name
::Arel::Attribute.new self, name
2010-08-12 14:24:16 -04:00
end
def hash
# Perf note: aliases and table alias is excluded from the hash
# aliases can have a loop back to this table breaking hashes in parent
# relations, for the vast majority of cases @name is unique to a query
@name.hash
end
def eql? other
self.class == other.class &&
self.name == other.name &&
self.aliases == other.aliases &&
self.table_alias == other.table_alias
end
alias :== :eql?
2010-09-28 19:46:06 -04:00
private
2010-09-21 16:50:53 -04:00
def attributes_for columns
return nil unless columns
columns.map do |column|
2010-12-03 18:24:30 -05:00
Attributes.for(column).new self, column.name.to_sym
2010-09-21 16:50:53 -04:00
end
end
2010-08-12 14:24:16 -04:00
end
end