Skip to content

Commit 31924f1

Browse files
author
Jippe Holwerda
committed
Fixed linked server compatability for SELECT queries.
1 parent 016cdc6 commit 31924f1

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

lib/arel/visitors/sqlserver.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ class SQLServer < Arel::Visitors::ToSql
1313

1414
# SQLServer ToSql/Visitor (Overides)
1515

16+
def visit_Arel_Attributes_Attribute o, collector
17+
join_name = o.relation.table_alias ||
18+
ActiveRecord::ConnectionAdapters::SQLServer::Utils.extract_identifiers(
19+
o.relation.name
20+
).object_quoted
21+
collector << "#{quote_table_name join_name}.#{quote_column_name o.name}"
22+
end
23+
1624
def visit_Arel_Nodes_BindParam o, collector
1725
collector.add_bind(o) { |i| "@#{i-1}" }
1826
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'cases/helper_sqlserver'
2+
3+
class FullyQualifiedIdentifierTestSQLServer < ActiveRecord::TestCase
4+
it 'should use fully qualified table name in select from clause' do
5+
table = Arel::Table.new("[my.server].db.schema.table")
6+
expected_sql = "SELECT * FROM [my.server].[db].[schema].[table]"
7+
assert_equal expected_sql, table.project(Arel.star).to_sql
8+
end
9+
10+
it 'should not use fully qualified table name in select projections' do
11+
table = Arel::Table.new("[my.server].db.schema.table")
12+
expected_sql = "SELECT [table].[name] FROM [my.server].[db].[schema].[table]"
13+
assert_equal expected_sql, table.project(table[:name]).to_sql
14+
end
15+
16+
it 'should not use fully qualified table name in where clause' do
17+
table = Arel::Table.new("[my.server].db.schema.table")
18+
expected_sql = "SELECT * FROM [my.server].[db].[schema].[table] WHERE [table].[id] = 42"
19+
assert_equal expected_sql, table.project(Arel.star).where(table[:id].eq(42)).to_sql
20+
end
21+
22+
it 'should not use fully qualified table name in order clause' do
23+
table = Arel::Table.new("[my.server].db.schema.table")
24+
expected_sql = "SELECT * FROM [my.server].[db].[schema].[table] ORDER BY [table].[name]"
25+
assert_equal expected_sql, table.project(Arel.star).order(table[:name]).to_sql
26+
end
27+
28+
it 'should use table name in select projections' do
29+
table = Arel::Table.new(:table)
30+
expected_sql = "SELECT [table].[name] FROM [table]"
31+
assert_equal expected_sql, table.project(table[:name]).to_sql
32+
end
33+
34+
it 'should use fully qualified table name in insert statement' do
35+
manager = Arel::InsertManager.new(Arel::Table.engine)
36+
manager.into Arel::Table.new("[my.server].db.schema.table")
37+
manager.values = manager.create_values [Arel.sql('*')], %w{ a }
38+
expected_sql = "INSERT INTO [my.server].[db].[schema].[table] VALUES (*)"
39+
assert_equal expected_sql, manager.to_sql
40+
end
41+
42+
it 'should use fully qualified table name in update statement' do
43+
table = Arel::Table.new("[my.server].db.schema.table")
44+
manager = Arel::UpdateManager.new(Arel::Table.engine)
45+
manager.table(table).where(table[:id].eq(42))
46+
manager.set([[table[:name], "Bob"]])
47+
expected_sql = "UPDATE [my.server].[db].[schema].[table] SET [name] = N'Bob' WHERE [table].[id] = 42"
48+
assert_equal expected_sql, manager.to_sql
49+
end
50+
51+
it 'should use fully qualified table name in delete statement' do
52+
table = Arel::Table.new("[my.server].db.schema.table")
53+
manager = Arel::DeleteManager.new(Arel::Table.engine)
54+
manager.from(table).where(table[:id].eq(42))
55+
expected_sql = "DELETE FROM [my.server].[db].[schema].[table] WHERE [table].[id] = 42"
56+
assert_equal expected_sql, manager.to_sql
57+
end
58+
end

0 commit comments

Comments
 (0)