From b706f690b611d647aae15a85caa19d167814ae3c Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 10 Mar 2010 18:16:07 -0300 Subject: Arel doesn't has to know if a table or column exists. --- lib/arel/algebra/relations/relation.rb | 5 +++-- lib/arel/engines/sql/relations/table.rb | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index e848c8aa1c..4cd3f8e109 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -84,7 +84,8 @@ module Arel module AttributeAccessable def [](index) - case index + @cached_attributes ||= {} + @cached_attributes[index] ||= case index when Symbol, String find_attribute_matching_name(index) when Attribute, Expression @@ -96,7 +97,7 @@ module Arel end def find_attribute_matching_name(name) - attributes.detect { |a| a.named?(name) } + attributes.detect { |a| a.named?(name) } || Attribute.new(self, name) end def find_attribute_matching_attribute(attribute) diff --git a/lib/arel/engines/sql/relations/table.rb b/lib/arel/engines/sql/relations/table.rb index e4b75d6c3d..d10b761ea3 100644 --- a/lib/arel/engines/sql/relations/table.rb +++ b/lib/arel/engines/sql/relations/table.rb @@ -2,7 +2,7 @@ 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 = {}) @@ -19,6 +19,7 @@ module Arel 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 @@ -29,9 +30,20 @@ module Arel 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 -- cgit v1.2.3