Skip to content

Commit 94dd35a

Browse files
committed
Properly quote table names when reflecting on views. Issue #73
1 parent e8d6e5d commit 94dd35a

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
* master *
33

4+
* Properly quote table names when reflecting on views.
5+
46
* Add "dead or not enabled" to :dblib's lost connection messages.
57

68

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def view_information(table_name)
305305
if view_info
306306
view_info = view_info.with_indifferent_access
307307
if view_info[:VIEW_DEFINITION].blank? || view_info[:VIEW_DEFINITION].length == 4000
308-
view_info[:VIEW_DEFINITION] = info_schema_query { select_values("EXEC sp_helptext #{table_name}").join }
308+
view_info[:VIEW_DEFINITION] = info_schema_query { select_values("EXEC sp_helptext #{quote_table_name(table_name)}").join }
309309
end
310310
end
311311
view_info

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ def set_new_id
1313

1414
class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
1515

16+
should 'quote table names properly even when they are views' do
17+
obj = SqlServerQuotedTable.create!
18+
assert_nothing_raised { SqlServerQuotedTable.first }
19+
obj = SqlServerQuotedView1.create!
20+
assert_nothing_raised { SqlServerQuotedView1.first }
21+
obj = SqlServerQuotedView2.create!
22+
assert_nothing_raised { SqlServerQuotedView2.first }
23+
end
24+
1625
should 'cope with multi line defaults' do
1726
default = StringDefault.new
1827
assert_equal "Some long default with a\nnew line.", default.string_with_multiline_default

test/cases/sqlserver_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class NumericData < ActiveRecord::Base ; self.table_name = 'numeric_data' ; end
3333
class CustomersView < ActiveRecord::Base ; self.table_name = 'customers_view' ; end
3434
class StringDefaultsView < ActiveRecord::Base ; self.table_name = 'string_defaults_view' ; end
3535
class StringDefaultsBigView < ActiveRecord::Base ; self.table_name = 'string_defaults_big_view' ; end
36+
class SqlServerQuotedTable < ActiveRecord::Base ; self.table_name = 'quoted-table' ; end
37+
class SqlServerQuotedView1 < ActiveRecord::Base ; self.table_name = 'quoted-view1' ; end
38+
class SqlServerQuotedView2 < ActiveRecord::Base ; self.table_name = 'quoted-view2' ; end
3639
class SqlServerUnicode < ActiveRecord::Base ; end
3740
class SqlServerString < ActiveRecord::Base ; end
3841
class SqlServerChronic < ActiveRecord::Base

test/schema/sqlserver_specific_schema.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@
7070
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newid] uniqueidentifier DEFAULT NEWID();|
7171
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newseqid] uniqueidentifier DEFAULT NEWSEQUENTIALID();|
7272

73+
create_table 'quoted-table', :force => true do |t|
74+
end
75+
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'quoted-view1') DROP VIEW [quoted-view1]"
76+
execute "CREATE VIEW [quoted-view1] AS SELECT * FROM [quoted-table]"
77+
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'quoted-view2') DROP VIEW [quoted-view2]"
78+
execute "CREATE VIEW [quoted-view2] AS \n /*#{'x'*4000}}*/ \n SELECT * FROM [quoted-table]"
79+
7380
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'customers_view') DROP VIEW customers_view"
7481
execute <<-CUSTOMERSVIEW
7582
CREATE VIEW customers_view AS

0 commit comments

Comments
 (0)