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-12-07 14:44:32 -05:00
|
|
|
include Arel::FactoryMethods
|
2020-05-11 11:03:15 -04:00
|
|
|
include Arel::AliasPredication
|
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
|
|
|
|
|
Move Arel attribute normalization into `arel_table`
In Active Record internal, `arel_table` is not directly used but
`arel_attribute` is used, since `arel_table` doesn't normalize an
attribute name as a string, and doesn't resolve attribute aliases.
For the above reason, `arel_attribute` should be used rather than
`arel_table`, but most people directly use `arel_table`, both
`arel_table` and `arel_attribute` are private API though.
Although I'd not recommend using private API, `arel_table` is actually
widely used, and it is also problematic for unscopeable queries and
hash-like relation merging friendly, as I explained at #39863.
To resolve the issue, this change moves Arel attribute normalization
(attribute name as a string, and attribute alias resolution) into
`arel_table`.
2020-07-19 07:00:42 -04:00
|
|
|
def initialize(name, as: nil, klass: nil, type_caster: klass&.type_caster)
|
2018-02-24 01:45:50 -05:00
|
|
|
@name = name.to_s
|
Move Arel attribute normalization into `arel_table`
In Active Record internal, `arel_table` is not directly used but
`arel_attribute` is used, since `arel_table` doesn't normalize an
attribute name as a string, and doesn't resolve attribute aliases.
For the above reason, `arel_attribute` should be used rather than
`arel_table`, but most people directly use `arel_table`, both
`arel_table` and `arel_attribute` are private API though.
Although I'd not recommend using private API, `arel_table` is actually
widely used, and it is also problematic for unscopeable queries and
hash-like relation merging friendly, as I explained at #39863.
To resolve the issue, this change moves Arel attribute normalization
(attribute name as a string, and attribute alias resolution) into
`arel_table`.
2020-07-19 07:00:42 -04:00
|
|
|
@klass = klass
|
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
|
|
|
|
|
Move Arel attribute normalization into `arel_table`
In Active Record internal, `arel_table` is not directly used but
`arel_attribute` is used, since `arel_table` doesn't normalize an
attribute name as a string, and doesn't resolve attribute aliases.
For the above reason, `arel_attribute` should be used rather than
`arel_table`, but most people directly use `arel_table`, both
`arel_table` and `arel_attribute` are private API though.
Although I'd not recommend using private API, `arel_table` is actually
widely used, and it is also problematic for unscopeable queries and
hash-like relation merging friendly, as I explained at #39863.
To resolve the issue, this change moves Arel attribute normalization
(attribute name as a string, and attribute alias resolution) into
`arel_table`.
2020-07-19 07:00:42 -04:00
|
|
|
def [](name, table = self)
|
|
|
|
name = name.to_s if name.is_a?(Symbol)
|
|
|
|
name = @klass.attribute_aliases[name] || name if @klass
|
|
|
|
Attribute.new(table, 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?
|
|
|
|
|
2020-06-02 14:17:03 -04:00
|
|
|
def type_cast_for_database(attr_name, value)
|
|
|
|
type_caster.type_cast_for_database(attr_name, value)
|
2014-12-29 13:24:44 -05:00
|
|
|
end
|
|
|
|
|
2020-01-14 16:14:39 -05:00
|
|
|
def type_for_attribute(name)
|
|
|
|
type_caster.type_for_attribute(name)
|
|
|
|
end
|
|
|
|
|
2014-12-29 13:24:44 -05:00
|
|
|
def able_to_type_cast?
|
|
|
|
!type_caster.nil?
|
|
|
|
end
|
|
|
|
|
2018-09-30 02:24:17 -04:00
|
|
|
private
|
2018-02-24 01:45:50 -05:00
|
|
|
attr_reader :type_caster
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
|
|
|
end
|