Permalink
Browse files

Properly quote table names when reflecting on views. Issue #73

  • Loading branch information...
1 parent e8d6e5d commit 94dd35aab551df6fca1142bc4f37c636b4ec9efe @metaskills metaskills committed Jan 1, 2011
View
@@ -1,6 +1,8 @@
* master *
+* Properly quote table names when reflecting on views.
+
* Add "dead or not enabled" to :dblib's lost connection messages.
@@ -305,7 +305,7 @@ def view_information(table_name)
if view_info
view_info = view_info.with_indifferent_access
if view_info[:VIEW_DEFINITION].blank? || view_info[:VIEW_DEFINITION].length == 4000
- view_info[:VIEW_DEFINITION] = info_schema_query { select_values("EXEC sp_helptext #{table_name}").join }
+ view_info[:VIEW_DEFINITION] = info_schema_query { select_values("EXEC sp_helptext #{quote_table_name(table_name)}").join }
end
end
view_info
@@ -13,6 +13,15 @@ def set_new_id
class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
+ should 'quote table names properly even when they are views' do
+ obj = SqlServerQuotedTable.create!
+ assert_nothing_raised { SqlServerQuotedTable.first }
+ obj = SqlServerQuotedView1.create!
+ assert_nothing_raised { SqlServerQuotedView1.first }
+ obj = SqlServerQuotedView2.create!
+ assert_nothing_raised { SqlServerQuotedView2.first }
+ end
+
should 'cope with multi line defaults' do
default = StringDefault.new
assert_equal "Some long default with a\nnew line.", default.string_with_multiline_default
@@ -33,6 +33,9 @@ class NumericData < ActiveRecord::Base ; self.table_name = 'numeric_data' ; end
class CustomersView < ActiveRecord::Base ; self.table_name = 'customers_view' ; end
class StringDefaultsView < ActiveRecord::Base ; self.table_name = 'string_defaults_view' ; end
class StringDefaultsBigView < ActiveRecord::Base ; self.table_name = 'string_defaults_big_view' ; end
+class SqlServerQuotedTable < ActiveRecord::Base ; self.table_name = 'quoted-table' ; end
+class SqlServerQuotedView1 < ActiveRecord::Base ; self.table_name = 'quoted-view1' ; end
+class SqlServerQuotedView2 < ActiveRecord::Base ; self.table_name = 'quoted-view2' ; end
class SqlServerUnicode < ActiveRecord::Base ; end
class SqlServerString < ActiveRecord::Base ; end
class SqlServerChronic < ActiveRecord::Base
@@ -70,6 +70,13 @@
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newid] uniqueidentifier DEFAULT NEWID();|
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newseqid] uniqueidentifier DEFAULT NEWSEQUENTIALID();|
+ create_table 'quoted-table', :force => true do |t|
+ end
+ execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'quoted-view1') DROP VIEW [quoted-view1]"
+ execute "CREATE VIEW [quoted-view1] AS SELECT * FROM [quoted-table]"
+ execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'quoted-view2') DROP VIEW [quoted-view2]"
+ execute "CREATE VIEW [quoted-view2] AS \n /*#{'x'*4000}}*/ \n SELECT * FROM [quoted-table]"
+
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'customers_view') DROP VIEW customers_view"
execute <<-CUSTOMERSVIEW
CREATE VIEW customers_view AS

0 comments on commit 94dd35a

Please sign in to comment.