Skip to content

Commit 4e7a921

Browse files
committed
Implement #quote_table_name that first checks if table name is quoted already. Add tests which was removed from ActiveRecord a short while ago that caught this.
1 parent 3d1edab commit 4e7a921

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

2000-2005-adapter.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Gem::Specification.new do |s|
4040
"test/connections/native_sqlserver/connection.rb",
4141
"test/connections/native_sqlserver_odbc/connection.rb",
4242
"test/migrations/transaction_table/1_table_will_never_be_created.rb",
43-
"test/schema/sqlserver_specific_schema.rb" ]
43+
"test/schema/sqlserver_specific_schema.rb",
44+
"test/schema/table_name_test_sqlserver.rb" ]
4445
s.rdoc_options = ["--line-numbers", "--inline-source", "--main", "README.rdoc"]
4546
s.extra_rdoc_files = ["README.rdoc"]
4647
end

README.rdoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ First, you will need Ruby DBI and Ruby ODBC. To my knowledge the ADO DBD for DBI
4848

4949
It should be noted that this version of the adapter was developed using both the ancient 0.0.23 version of DBI up to the current stable release of 0.4.0. Because later versions of DBI will be changing many things, IT IS HIGHLY RECOMMENDED that you max your install to version 0.4.0 which the examples below show. For the time being we are not supporting DBI versions higher than 0.4.0. The good news is that if you were using a very old DBI with ADO, technically this adapter will still work for you, but be warned your path is getting old and may not be supported for long.
5050

51-
$ sudo gem install dbi --version 0.4.0
52-
$ sudo gem install dbd-odbc --version 0.2.4
53-
$ sudo gem install rails-sqlserver-2000-2005-adapter
51+
$ gem install dbi --version 0.4.0
52+
$ gem install dbd-odbc --version 0.2.4
53+
$ gem install rails-sqlserver-2000-2005-adapter
5454

5555
Optionally configure your gem dependencies in your rails environment.rb file.
5656

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ def quote_column_name(column_name)
223223
column_name.to_s.split('.').map{ |name| "[#{name}]" }.join('.')
224224
end
225225

226+
def quote_table_name(table_name)
227+
return table_name if table_name =~ /^\[.*\]$/
228+
quote_column_name(table_name)
229+
end
230+
226231
def quoted_true
227232
'1'
228233
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'cases/sqlserver_helper'
2+
require 'models/order'
3+
4+
class TableNameTestSqlserver < ActiveRecord::TestCase
5+
6+
self.use_transactional_fixtures = false
7+
8+
def setup
9+
Order.table_name = '[orders]'
10+
Order.reset_column_information
11+
end
12+
13+
should 'load columns with escaped table name for model' do
14+
assert_equal 4, Order.columns.length
15+
16+
end
17+
18+
should 'not re-escape table name if it is escaped already for SQL queries' do
19+
assert_sql(/SELECT \* FROM \[orders\]/) { Order.all }
20+
end
21+
22+
23+
end

0 commit comments

Comments
 (0)