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

111 lines
2.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-02-24 01:45:50 -05:00
2018-02-24 02:41:47 -05:00
module Arel # :nodoc: all
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
attr_accessor :name, :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, as: nil, type_caster: nil)
2018-02-24 01:45:50 -05:00
@name = name.to_s
@type_caster = type_caster
# 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?
if as.to_s == @name
as = nil
2010-09-21 16:50:53 -04:00
end
@table_alias = as
2010-08-18 19:32:32 -04:00
end
2018-02-24 01:45:50 -05:00
def alias(name = "#{self.name}_2")
Nodes::TableAlias.new(self, name)
2010-08-12 14:24:16 -04:00
end
def from
SelectManager.new(self)
2010-09-08 20:32:44 -04:00
end
2018-02-24 01:45:50 -05:00
def join(relation, klass = Nodes::InnerJoin)
return from unless relation
case relation
when String, Nodes::SqlLiteral
2017-01-17 23:13:37 -05:00
raise EmptyJoinError if relation.empty?
klass = Nodes::StringJoin
end
from.join(relation, klass)
2010-08-18 19:32:32 -04:00
end
2018-02-24 01:45:50 -05:00
def outer_join(relation)
2013-05-12 13:09:07 -04:00
join(relation, Nodes::OuterJoin)
end
2018-02-24 01:45:50 -05:00
def group(*columns)
from.group(*columns)
2010-09-07 19:07:37 -04:00
end
2018-02-24 01:45:50 -05:00
def order(*expr)
from.order(*expr)
2010-09-06 20:17:49 -04:00
end
2018-02-24 01:45:50 -05:00
def where(condition)
from.where condition
end
2018-02-24 01:45:50 -05:00
def project(*things)
from.project(*things)
2010-08-13 17:13:38 -04:00
end
2018-02-24 01:45:50 -05:00
def take(amount)
from.take amount
2010-08-16 18:02:37 -04:00
end
2018-02-24 01:45:50 -05:00
def skip(amount)
from.skip amount
end
2018-02-24 01:45:50 -05:00
def having(expr)
from.having expr
2010-09-08 18:29:22 -04:00
end
2018-02-24 01:45:50 -05: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
2018-02-24 01:45:50 -05:00
def eql?(other)
self.class == other.class &&
self.name == other.name &&
self.table_alias == other.table_alias
end
alias :== :eql?
def type_cast_for_database(attribute_name, value)
type_caster.type_cast_for_database(attribute_name, value)
end
def able_to_type_cast?
!type_caster.nil?
end
private
2018-02-24 01:45:50 -05:00
attr_reader :type_caster
2010-08-12 14:24:16 -04:00
end
end