Skip to content

Commit 4986e2f

Browse files
committed
Update internal helper method #orders_and_dirs_set to cope with an order clause like "description desc". This resolves ticket #26 http://rails-sqlserver.lighthouseapp.com/projects/20277/tickets/26-cant-have-a-description-column-and-sort-desc
1 parent 2ab7ca6 commit 4986e2f

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

CHANGELOG

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

22
MASTER
33

4+
* Update internal helper method #orders_and_dirs_set to cope with an order clause like "description desc". This
5+
resolves ticket #26 [Ken Collins]
6+
47
* Provide support for running queries at different isolation levels using #run_with_isolation_level method
58
that can take a block or not. Also implement a #user_options method that reflects on the current user
69
session values. Resolves #20 [Murray Steele]

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -901,12 +901,13 @@ def orders_and_dirs_set(order)
901901
orders = order.sub('ORDER BY','').split(',').map(&:strip).reject(&:blank?)
902902
orders_dirs = orders.map do |ord|
903903
dir = nil
904-
if match_data = ord.match(/\b(asc|desc)$/i)
905-
dir = match_data[1]
906-
ord.sub!(dir,'').strip!
907-
dir.upcase!
904+
ord.sub!(/\b(asc|desc)$/i) do |match|
905+
if match
906+
dir = match.upcase.strip
907+
''
908+
end
908909
end
909-
[ord,dir]
910+
[ord.strip, dir]
910911
end
911912
end
912913

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'cases/sqlserver_helper'
22

33
class StringDefault < ActiveRecord::Base; end;
4+
class SqlServerEdgeSchema < ActiveRecord::Base; end;
45

56
class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
67

@@ -23,4 +24,34 @@ class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
2324
assert_equal '(NULL)', default.string_with_pretend_null_four
2425
end
2526

27+
context 'Testing edge case schemas' do
28+
29+
setup do
30+
@edge_class = SqlServerEdgeSchema
31+
end
32+
33+
context 'with description column' do
34+
35+
setup do
36+
@da = @edge_class.create! :description => 'A'
37+
@db = @edge_class.create! :description => 'B'
38+
@dc = @edge_class.create! :description => 'C'
39+
end
40+
41+
teardown { @edge_class.delete_all }
42+
43+
should 'allow all sorts of ordering without adapter munging it up' do
44+
assert_equal ['A','B','C'], @edge_class.all(:order => 'description').map(&:description)
45+
assert_equal ['A','B','C'], @edge_class.all(:order => 'description asc').map(&:description)
46+
assert_equal ['A','B','C'], @edge_class.all(:order => 'description ASC').map(&:description)
47+
assert_equal ['C','B','A'], @edge_class.all(:order => 'description desc').map(&:description)
48+
assert_equal ['C','B','A'], @edge_class.all(:order => 'description DESC').map(&:description)
49+
end
50+
51+
end
52+
53+
54+
end
55+
56+
2657
end

test/schema/sqlserver_specific_schema.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
# TODO: Add some different native binary types and test.
6464
end
6565

66+
create_table :sql_server_edge_schemas, :force => true do |t|
67+
t.string :description
68+
end
69+
6670
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'customers_view') DROP VIEW customers_view"
6771
execute <<-CUSTOMERSVIEW
6872
CREATE VIEW customers_view AS

0 commit comments

Comments
 (0)