2017-02-13 13:58:58 -05:00
|
|
|
# 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
|
2010-08-16 17:56:56 -04:00
|
|
|
include Arel::Crud
|
2010-12-07 14:44:32 -05:00
|
|
|
include Arel::FactoryMethods
|
2010-08-16 17:56:56 -04:00
|
|
|
|
2010-08-12 14:24:16 -04:00
|
|
|
@engine = nil
|
|
|
|
class << self; attr_accessor :engine; end
|
|
|
|
|
2016-09-13 14:06:17 -04:00
|
|
|
attr_accessor :name, :table_alias
|
2010-08-12 18:40:58 -04:00
|
|
|
|
2011-03-03 17:13:22 -05:00
|
|
|
# TableAlias and Table both have a #table_name which is the name of the underlying table
|
|
|
|
alias :table_name :name
|
|
|
|
|
2014-12-29 13:24:44 -05:00
|
|
|
def initialize(name, as: nil, type_caster: nil)
|
2018-02-24 01:45:50 -05:00
|
|
|
@name = name.to_s
|
2014-12-29 13:24:44 -05:00
|
|
|
@type_caster = type_caster
|
2014-11-26 15:45:31 -05:00
|
|
|
|
|
|
|
# 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?
|
2014-12-29 13:24:44 -05:00
|
|
|
if as.to_s == @name
|
|
|
|
as = nil
|
2010-09-21 16:50:53 -04:00
|
|
|
end
|
2014-12-29 13:24:44 -05:00
|
|
|
@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")
|
2016-09-13 14:06:17 -04:00
|
|
|
Nodes::TableAlias.new(self, name)
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
|
|
|
|
2014-11-29 19:22:17 -05:00
|
|
|
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)
|
2014-11-26 15:45:31 -05:00
|
|
|
return from unless relation
|
2010-09-09 12:27:29 -04:00
|
|
|
|
2010-08-24 20:59:03 -04:00
|
|
|
case relation
|
|
|
|
when String, Nodes::SqlLiteral
|
2017-01-17 23:13:37 -05:00
|
|
|
raise EmptyJoinError if relation.empty?
|
2010-12-07 14:44:32 -05:00
|
|
|
klass = Nodes::StringJoin
|
2010-08-24 20:59:03 -04:00
|
|
|
end
|
2010-12-07 14:44:32 -05:00
|
|
|
|
2014-11-26 15:45:31 -05:00
|
|
|
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)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.group(*columns)
|
2010-09-07 19:07:37 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def order(*expr)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.order(*expr)
|
2010-09-06 20:17:49 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def where(condition)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.where condition
|
2010-08-13 14:30:24 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def project(*things)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.project(*things)
|
2010-08-13 17:13:38 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def take(amount)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.take amount
|
2010-08-16 18:02:37 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def skip(amount)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.skip amount
|
2010-10-20 20:16:18 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def having(expr)
|
2014-11-26 15:45:31 -05:00
|
|
|
from.having expr
|
2010-09-08 18:29:22 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def [](name)
|
2010-12-22 21:02:56 -05:00
|
|
|
::Arel::Attribute.new self, name
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
2010-09-18 14:33:07 -04:00
|
|
|
|
2012-08-18 22:33:25 -04:00
|
|
|
def hash
|
2014-11-26 15:45:31 -05:00
|
|
|
# Perf note: aliases and table alias is excluded from the hash
|
2013-08-30 01:44:10 -04:00
|
|
|
# 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
|
2012-08-18 22:33:25 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def eql?(other)
|
2012-08-18 22:33:25 -04:00
|
|
|
self.class == other.class &&
|
|
|
|
self.name == other.name &&
|
|
|
|
self.table_alias == other.table_alias
|
|
|
|
end
|
|
|
|
alias :== :eql?
|
|
|
|
|
2014-12-29 13:24:44 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
attr_reader :type_caster
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
|
|
|
end
|