Permalink
Browse files

SQLServer: handle [quoted] table names. Closes #6635.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5891 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 8d3ff3a commit 9073cef8669b9f43dbeb83e306be3445d62566f8 @jeremy jeremy committed Jan 12, 2007
View
@@ -1,5 +1,7 @@
*SVN*
+* SQLServer: handle [quoted] table names. #6635 [rrich]
+
* acts_as_nested_set works with single-table inheritance. #6030 [Josh Susser]
* PostgreSQL: use a subselect to correctly perform eager finds with :limit and :order. #4668 [eventualbuddha]
@@ -257,6 +257,7 @@ def columns(table_name, name = nil)
return [] if table_name.blank?
table_name = table_name.to_s if table_name.is_a?(Symbol)
table_name = table_name.split('.')[-1] unless table_name.nil?
+ table_name = table_name.gsub(/[\[\]]/, '')
sql = %Q{
SELECT
cols.COLUMN_NAME as ColName,
@@ -30,3 +30,4 @@ DROP TABLE fk_test_has_pk;
DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
+DROP TABLE [order];
@@ -228,3 +228,11 @@ CREATE TABLE numeric_data (
my_house_population decimal(2),
decimal_number_with_default decimal(3,2) DEFAULT 2.78
);
+
+CREATE TABLE [order] (
+ id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
+ color varchar(255),
+ fruit_size varchar(255),
+ texture varchar(255),
+ flavor varchar(255)
+);
@@ -0,0 +1,23 @@
+require 'abstract_unit'
+require "#{File.dirname(__FILE__)}/../lib/active_record/schema"
+
+if ActiveRecord::Base.connection.supports_migrations?
+ class Order < ActiveRecord::Base
+ self.table_name = '[order]'
+ end
+
+ class TableNameTest < Test::Unit::TestCase
+ self.use_transactional_fixtures = false
+
+ # Ensures Model.columns works when using SQLServer escape characters.
+ # Enables legacy schemas using SQL reserved words as table names.
+ # Should work with table names with spaces as well ('table name').
+ def test_escaped_table_name
+ assert_nothing_raised do
+ ActiveRecord::Base.connection.select_all 'SELECT * FROM [order]'
+ end
+ assert_equal '[order]', Order.table_name
+ assert_equal 5, Order.columns.length
+ end
+ end
+end

0 comments on commit 9073cef

Please sign in to comment.