From 752813016ed227ecfbe0bf69c92de2e2c3e7a988 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 1 Jan 2010 22:54:23 +0530 Subject: Add support for table aliasing Example : users = Table(:users, :as => :accounts) users.to_sql => SELECT `accounts`.`id`, `accounts`.`name` FROM `users` AS `accounts` --- spec/arel/engines/sql/unit/relations/join_spec.rb | 56 +++++++++++++++++++++- spec/arel/engines/sql/unit/relations/table_spec.rb | 20 ++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) (limited to 'spec') diff --git a/spec/arel/engines/sql/unit/relations/join_spec.rb b/spec/arel/engines/sql/unit/relations/join_spec.rb index 2820763a66..e2fc4bab3d 100644 --- a/spec/arel/engines/sql/unit/relations/join_spec.rb +++ b/spec/arel/engines/sql/unit/relations/join_spec.rb @@ -5,13 +5,20 @@ module Arel before do @relation1 = Table.new(:users) @relation2 = Table.new(:photos) - @predicate = @relation1[:id].eq(@relation2[:user_id]) + @predicate1 = @relation1[:id].eq(@relation2[:user_id]) + + @relation3 = Table.new(:users, :as => :super_users) + @relation4 = Table.new(:photos, :as => :super_photos) + + @predicate2 = @relation3[:id].eq(@relation2[:user_id]) + @predicate3 = @relation3[:id].eq(@relation4[:user_id]) end describe '#to_sql' do + describe 'when joining with another relation' do it 'manufactures sql joining the two tables on the predicate' do - sql = InnerJoin.new(@relation1, @relation2, @predicate).to_sql + sql = InnerJoin.new(@relation1, @relation2, @predicate1).to_sql adapter_is :mysql do sql.should be_like(%Q{ @@ -29,6 +36,51 @@ module Arel }) end end + + describe 'when joining with another relation with an aliased table' do + it 'manufactures sql joining the two tables on the predicate respecting table aliasing' do + sql = InnerJoin.new(@relation3, @relation2, @predicate2).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `super_users`.`id`, `super_users`.`name`, `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id` + FROM `users` AS `super_users` + INNER JOIN `photos` ON `super_users`.`id` = `photos`.`user_id` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "super_users"."id", "super_users"."name", "photos"."id", "photos"."user_id", "photos"."camera_id" + FROM "users" AS "super_users" + INNER JOIN "photos" ON "super_users"."id" = "photos"."user_id" + }) + end + end + end + + describe 'when joining with two relations with aliased tables' do + it 'manufactures sql joining the two tables on the predicate respecting table aliasing' do + sql = InnerJoin.new(@relation3, @relation4, @predicate3).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `super_users`.`id`, `super_users`.`name`, `super_photos`.`id`, `super_photos`.`user_id`, `super_photos`.`camera_id` + FROM `users` AS `super_users` + INNER JOIN `photos` AS `super_photos` ON `super_users`.`id` = `super_photos`.`user_id` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "super_users"."id", "super_users"."name", "super_photos"."id", "super_photos"."user_id", "super_photos"."camera_id" + FROM "users" AS "super_users" + INNER JOIN "photos" AS "super_photos" ON "super_users"."id" = "super_photos"."user_id" + }) + end + end + end + end describe 'when joining with a string' do diff --git a/spec/arel/engines/sql/unit/relations/table_spec.rb b/spec/arel/engines/sql/unit/relations/table_spec.rb index 92e4549028..977eb343e3 100644 --- a/spec/arel/engines/sql/unit/relations/table_spec.rb +++ b/spec/arel/engines/sql/unit/relations/table_spec.rb @@ -26,6 +26,26 @@ module Arel end end + describe '#as' do + it "manufactures a simple select query using aliases" do + sql = @relation.as(:super_users).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{ + SELECT `super_users`.`id`, `super_users`.`name` + FROM `users` AS `super_users` + }) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{ + SELECT "super_users"."id", "super_users"."name" + FROM "users" AS "super_users" + }) + end + end + end + describe '#column_for' do it "returns the column corresponding to the attribute" do @relation.column_for(@relation[:id]).should == @relation.columns.detect { |c| c.name == 'id' } -- cgit v1.2.3