2010-08-12 14:24:16 -04:00
|
|
|
module Arel
|
|
|
|
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-08-23 17:24:00 -04:00
|
|
|
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
|
2010-08-13 14:53:36 -04:00
|
|
|
@name = name
|
|
|
|
@engine = engine
|
|
|
|
@engine = engine[:engine] if Hash === engine
|
|
|
|
@columns = nil
|
2010-08-18 19:32:32 -04:00
|
|
|
@aliases = []
|
2010-08-23 17:24:00 -04:00
|
|
|
@table_alias = nil
|
2010-08-18 19:32:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2010-08-13 18:30:22 -04:00
|
|
|
SelectManager.new(@engine).from(self)
|
2010-08-13 00:21:20 -04:00
|
|
|
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
|
2010-08-24 20:59:03 -04:00
|
|
|
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)
|
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
|
|
|
|
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
|
|
|
|
|
2010-08-13 14:30:24 -04:00
|
|
|
def where condition
|
|
|
|
tm.where condition
|
|
|
|
end
|
|
|
|
|
2010-08-13 17:13:38 -04:00
|
|
|
def project thing
|
|
|
|
tm.project thing
|
|
|
|
end
|
|
|
|
|
2010-08-16 18:02:37 -04:00
|
|
|
def take amount
|
|
|
|
tm.take amount
|
|
|
|
end
|
|
|
|
|
2010-08-12 17:55:31 -04:00
|
|
|
def columns
|
2010-08-13 14:30:24 -04:00
|
|
|
@columns ||= @engine.connection.columns(@name, "#{@name} Columns").map do |column|
|
2010-08-12 17:55:31 -04:00
|
|
|
Attributes.for(column).new self, column.name, column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def [] name
|
|
|
|
name = name.to_s
|
|
|
|
columns.find { |column| column.name == name }
|
2010-08-12 14:24:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|