aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/arel/table.rb5
-rw-r--r--lib/arel/visitors/to_sql.rb6
-rw-r--r--spec/arel/select_manager_spec.rb9
3 files changed, 19 insertions, 1 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index efdc34ed94..1519efa63b 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -14,6 +14,11 @@ module Arel
@columns = nil
@aliases = []
@table_alias = 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
end
def alias
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index be9a62325a..391d2ff1c4 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -139,7 +139,11 @@ module Arel
end
def visit_Arel_Table o
- quote_table_name o.name
+ if o.table_alias
+ "#{quote_table_name o.name} #{quote_table_name o.table_alias}"
+ else
+ quote_table_name o.name
+ end
end
def visit_Arel_Nodes_In o
diff --git a/spec/arel/select_manager_spec.rb b/spec/arel/select_manager_spec.rb
index 86a4daee44..2ed1a851e5 100644
--- a/spec/arel/select_manager_spec.rb
+++ b/spec/arel/select_manager_spec.rb
@@ -49,6 +49,15 @@ module Arel
end
end
+ describe 'initialize' do
+ it 'uses alias in sql' do
+ table = Table.new :users, :engine => Table.engine, :as => 'foo'
+ mgr = table.from table
+ mgr.skip 10
+ mgr.to_sql.should be_like %{ SELECT FROM "users" "foo" OFFSET 10 }
+ end
+ end
+
describe 'skip' do
it 'should add an offset' do
table = Table.new :users