lib/arel/engines/sql/relations/table.rb in arel-0.2.1 vs lib/arel/engines/sql/relations/table.rb in arel-0.3.0
- old
+ new
@@ -1,10 +1,10 @@
module Arel
class Table < Relation
include Recursion::BaseCase
- cattr_accessor :engine
+ cattr_accessor :engine, :tables
attr_reader :name, :engine, :table_alias, :options
def initialize(name, options = {})
@name = name.to_s
@@ -13,18 +13,38 @@
@engine = options[:engine] || Table.engine
@table_alias = options[:as].to_s if options[:as].present? && options[:as].to_s != @name
else
@engine = options # Table.new('foo', engine)
end
+
+ if @engine.connection
+ begin
+ require "arel/engines/sql/compilers/#{@engine.adapter_name.downcase}_compiler"
+ @@tables ||= engine.tables
+ rescue LoadError
+ raise "#{@engine.adapter_name} is not supported by Arel."
+ end
+ end
end
def as(table_alias)
Table.new(name, options.merge(:as => table_alias))
end
+ def table_exists?
+ if @table_exists
+ true
+ else
+ @table_exists = @@tables.include?(name) || engine.table_exists?(name)
+ end
+ end
+
def attributes
- @attributes ||= columns.collect do |column|
- Attribute.new(self, column.name.to_sym)
+ return @attributes if defined?(@attributes)
+ if table_exists?
+ @attributes = columns.collect { |column| Attribute.new(self, column.name.to_sym) }
+ else
+ []
end
end
def eql?(other)
self == other