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

96 lines
2 KiB
Ruby
Raw Normal View History

2010-08-12 14:24:16 -04:00
module Arel
class Table
include Arel::Crud
2010-08-12 14:24:16 -04:00
@engine = nil
class << self; attr_accessor :engine; end
attr_reader :name, :engine, :aliases, :table_alias
2010-08-12 18:40:58 -04:00
2010-08-12 17:55:31 -04:00
def initialize name, engine = Table.engine
@name = name
@engine = engine
@engine = engine[:engine] if Hash === engine
@columns = nil
2010-08-18 19:32:32 -04:00
@aliases = []
@table_alias = nil
@primary_key = nil
# 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 = engine[:as] if Hash === engine
2010-08-18 19:32:32 -04:00
end
def primary_key
@primary_key ||= self[@engine.connection.primary_key(name)]
end
2010-08-18 19:32:32 -04:00
def alias
Nodes::TableAlias.new("#{name}_2", self).tap do |node|
@aliases << node
end
2010-08-12 14:24:16 -04:00
end
2010-08-13 11:11:08 -04:00
def tm
SelectManager.new(@engine).from(self)
2010-08-13 00:21:20 -04:00
end
2010-09-08 20:32:44 -04:00
def from table
SelectManager.new(@engine).from table
end
2010-09-07 17:02:23 -04:00
def joins manager
nil
end
2010-09-07 17:49:06 -04:00
def join relation, klass = Nodes::InnerJoin
return tm unless relation
sm = SelectManager.new(@engine)
case relation
when String, Nodes::SqlLiteral
raise if relation.blank?
sm.from Nodes::StringJoin.new(self, relation)
else
2010-09-07 17:49:06 -04:00
sm.from klass.new(self, relation, nil)
end
2010-08-18 19:32:32 -04:00
end
2010-09-07 19:07:37 -04:00
def group *columns
tm.group(*columns)
end
2010-09-06 20:17:49 -04:00
def order *expr
2010-09-06 20:18:10 -04:00
tm.order(*expr)
2010-09-06 20:17:49 -04:00
end
def where condition
tm.where condition
end
def project *things
2010-09-08 16:44:34 -04:00
tm.project(*things)
2010-08-13 17:13:38 -04:00
end
2010-08-16 18:02:37 -04:00
def take amount
tm.take amount
end
2010-09-08 18:29:22 -04:00
def having expr
tm.having expr
end
2010-08-12 17:55:31 -04:00
def columns
@columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
Attributes.for(column).new self, column.name.to_sym, column
2010-08-12 17:55:31 -04:00
end
end
def [] name
name = name.to_sym
2010-08-12 17:55:31 -04:00
columns.find { |column| column.name == name }
2010-08-12 14:24:16 -04:00
end
end
end