2010-10-18 18:41:21 -04:00
|
|
|
module Arel
|
2010-08-12 14:24:16 -04:00
|
|
|
class Table
|
2010-08-16 17:56:56 -04:00
|
|
|
include Arel::Crud
|
|
|
|
|
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
|
|
|
|
2010-08-12 17:55:31 -04:00
|
|
|
def initialize name, engine = Table.engine
|
2010-11-16 19:26:48 -05:00
|
|
|
@name = name.to_s
|
2010-08-13 14:53:36 -04:00
|
|
|
@engine = engine
|
|
|
|
@columns = nil
|
2010-08-18 19:32:32 -04:00
|
|
|
@aliases = []
|
2010-08-23 17:24:00 -04:00
|
|
|
@table_alias = nil
|
2010-09-14 16:39:33 -04:00
|
|
|
@primary_key = nil
|
2010-09-12 21:15:21 -04:00
|
|
|
|
2010-09-21 16:50:53 -04:00
|
|
|
if Hash === engine
|
|
|
|
@engine = engine[:engine] || Table.engine
|
|
|
|
@columns = attributes_for engine[:columns]
|
|
|
|
|
|
|
|
# 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?
|
2010-11-16 19:47:34 -05:00
|
|
|
@table_alias = engine[:as] unless engine[:as].to_s == @name
|
2010-09-21 16:50:53 -04:00
|
|
|
end
|
2010-08-18 19:32:32 -04:00
|
|
|
end
|
|
|
|
|
2010-09-14 16:39:33 -04:00
|
|
|
def primary_key
|
2010-09-28 09:15:35 -04:00
|
|
|
@primary_key ||= begin
|
|
|
|
primary_key_name = @engine.connection.primary_key(name)
|
|
|
|
# some tables might be without primary key
|
|
|
|
primary_key_name && self[primary_key_name]
|
|
|
|
end
|
2010-09-14 16:39:33 -04:00
|
|
|
end
|
|
|
|
|
2010-11-24 15:01:05 -05:00
|
|
|
def alias name = "#{self.name}_2"
|
|
|
|
Nodes::TableAlias.new(name, self).tap do |node|
|
2010-08-18 19:32:32 -04:00
|
|
|
@aliases << node
|
|
|
|
end
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
|
|
|
|
2010-09-08 20:32:44 -04:00
|
|
|
def from table
|
2010-09-23 18:26:08 -04:00
|
|
|
SelectManager.new(@engine, table)
|
2010-09-08 20:32:44 -04:00
|
|
|
end
|
|
|
|
|
2010-09-07 17:02:23 -04:00
|
|
|
def joins manager
|
2010-11-30 19:57:55 -05:00
|
|
|
if $VERBOSE
|
|
|
|
warn "joins is deprecated and will be removed in 2.2"
|
|
|
|
warn "please remove your call to joins from #{caller.first}"
|
|
|
|
end
|
2010-09-07 17:02:23 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2010-09-07 17:49:06 -04:00
|
|
|
def join relation, klass = Nodes::InnerJoin
|
2010-09-28 19:57:33 -04:00
|
|
|
return from(self) unless relation
|
2010-09-09 12:27:29 -04:00
|
|
|
|
2010-08-24 20:59:03 -04:00
|
|
|
case relation
|
|
|
|
when String, Nodes::SqlLiteral
|
|
|
|
raise if relation.blank?
|
2010-09-28 19:57:33 -04:00
|
|
|
from Nodes::StringJoin.new(self, relation)
|
2010-08-24 20:59:03 -04:00
|
|
|
else
|
2010-09-28 19:57:33 -04:00
|
|
|
from klass.new(self, relation, nil)
|
2010-08-24 20:59:03 -04:00
|
|
|
end
|
2010-08-18 19:32:32 -04:00
|
|
|
end
|
|
|
|
|
2010-09-07 19:07:37 -04:00
|
|
|
def group *columns
|
2010-09-28 19:57:33 -04:00
|
|
|
from(self).group(*columns)
|
2010-09-07 19:07:37 -04:00
|
|
|
end
|
|
|
|
|
2010-09-06 20:17:49 -04:00
|
|
|
def order *expr
|
2010-09-28 19:57:33 -04:00
|
|
|
from(self).order(*expr)
|
2010-09-06 20:17:49 -04:00
|
|
|
end
|
|
|
|
|
2010-08-13 14:30:24 -04:00
|
|
|
def where condition
|
2010-09-28 19:57:33 -04:00
|
|
|
from(self).where condition
|
2010-08-13 14:30:24 -04:00
|
|
|
end
|
|
|
|
|
2010-09-08 16:25:42 -04:00
|
|
|
def project *things
|
2010-09-28 19:57:33 -04:00
|
|
|
from(self).project(*things)
|
2010-08-13 17:13:38 -04:00
|
|
|
end
|
|
|
|
|
2010-08-16 18:02:37 -04:00
|
|
|
def take amount
|
2010-09-28 19:57:33 -04:00
|
|
|
from(self).take amount
|
2010-08-16 18:02:37 -04:00
|
|
|
end
|
|
|
|
|
2010-10-20 20:16:18 -04:00
|
|
|
def skip amount
|
|
|
|
from(self).skip amount
|
|
|
|
end
|
|
|
|
|
2010-09-08 18:29:22 -04:00
|
|
|
def having expr
|
2010-09-28 19:57:33 -04:00
|
|
|
from(self).having expr
|
2010-09-08 18:29:22 -04:00
|
|
|
end
|
|
|
|
|
2010-08-12 17:55:31 -04:00
|
|
|
def columns
|
2010-09-21 16:50:53 -04:00
|
|
|
@columns ||=
|
|
|
|
attributes_for @engine.connection.columns(@name, "#{@name} Columns")
|
2010-08-12 17:55:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def [] name
|
2010-09-18 14:33:07 -04:00
|
|
|
return nil unless table_exists?
|
|
|
|
|
2010-09-10 19:58:19 -04:00
|
|
|
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
|
2010-09-18 14:33:07 -04:00
|
|
|
|
2010-09-27 16:52:50 -04:00
|
|
|
def select_manager
|
2010-09-28 19:57:33 -04:00
|
|
|
SelectManager.new(@engine)
|
2010-09-27 16:52:50 -04:00
|
|
|
end
|
|
|
|
|
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|
|
|
|
|
Attributes.for(column).new self, column.name.to_sym, column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-18 14:33:07 -04:00
|
|
|
def table_exists?
|
|
|
|
@table_exists ||= tables.key?(@name) || engine.connection.table_exists?(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tables
|
|
|
|
self.class.table_cache(@engine)
|
|
|
|
end
|
|
|
|
|
|
|
|
@@table_cache = nil
|
|
|
|
def self.table_cache engine # :nodoc:
|
|
|
|
@@table_cache ||= Hash[engine.connection.tables.map { |x| [x,true] }]
|
|
|
|
end
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
|
|
|
end
|