rails-sqlserver-2000-2005-adapter 2.2.14 → 2.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -4,6 +4,14 @@ MASTER
4
4
  *
5
5
 
6
6
 
7
+ * 2.2.15 * (March 23rd, 2009)
8
+
9
+ * Better add_lock! method that can add the lock to just about all the elements in the statement. This
10
+ could be eager loaded associations, joins, etc. Done so that paginated results can easily add lock
11
+ options for performance. Note, the tally count in add_limit_offset! use "WITH (NOLOCK)" explicitly
12
+ as it can not hurt and is needed. [Ken Collins]
13
+
14
+
7
15
  * 2.2.14 * (March 17th, 2009)
8
16
 
9
17
  * Rails2.3 - Back passing tests on 2.2 work. Includes: (1) Created new test helpers that check ActiveRecord
@@ -150,7 +150,7 @@ module ActiveRecord
150
150
  class SQLServerAdapter < AbstractAdapter
151
151
 
152
152
  ADAPTER_NAME = 'SQLServer'.freeze
153
- VERSION = '2.2.14'.freeze
153
+ VERSION = '2.2.15'.freeze
154
154
  DATABASE_VERSION_REGEXP = /Microsoft SQL Server\s+(\d{4})/
155
155
  SUPPORTED_VERSIONS = [2000,2005].freeze
156
156
  LIMITABLE_TYPES = ['string','integer','float','char','nchar','varchar','nvarchar'].freeze
@@ -394,7 +394,9 @@ module ActiveRecord
394
394
  end
395
395
  # The business of adding limit/offset
396
396
  if options[:limit] and options[:offset]
397
- total_rows = select_value("SELECT count(*) as TotalRows from (#{sql.sub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT#{$1} TOP 1000000000")}) tally").to_i
397
+ tally_sql = "SELECT count(*) as TotalRows from (#{sql.sub(/\bSELECT(\s+DISTINCT)?\b/i, "SELECT#{$1} TOP 1000000000")}) tally"
398
+ add_lock! tally_sql, :lock => 'WITH (NOLOCK)'
399
+ total_rows = select_value(tally_sql).to_i
398
400
  if (options[:limit] + options[:offset]) >= total_rows
399
401
  options[:limit] = (total_rows - options[:offset] >= 0) ? (total_rows - options[:offset]) : 0
400
402
  end
@@ -439,8 +441,8 @@ module ActiveRecord
439
441
  # http://blog.sqlauthority.com/2007/04/27/sql-server-2005-locking-hints-and-examples/
440
442
  return unless options[:lock]
441
443
  lock_type = options[:lock] == true ? 'WITH(HOLDLOCK, ROWLOCK)' : options[:lock]
442
- from_table = sql.match(/FROM(.*)WHERE/im)[1]
443
- sql.sub! from_table, "#{from_table}#{lock_type} "
444
+ sql.gsub! %r|LEFT OUTER JOIN\s+(.*?)\s+ON|im, "LEFT OUTER JOIN \\1 #{lock_type} ON"
445
+ sql.gsub! %r{FROM\s([\w\[\]\.]+)}im, "FROM \\1 #{lock_type}"
444
446
  end
445
447
 
446
448
  def empty_insert_statement(table_name)
@@ -49,9 +49,34 @@ class PessimisticLockingTestSqlserver < ActiveRecord::TestCase
49
49
  end
50
50
  end
51
51
  end
52
+
53
+ should 'simply add lock to find all' do
54
+ assert_sql %r|SELECT \* FROM \[people\] WITH \(NOLOCK\)| do
55
+ Person.all(:lock => 'WITH (NOLOCK)')
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ context 'For paginated finds' do
62
+
63
+ setup do
64
+ 20.times { |n| Person.create!(:first_name => "Thing_#{n}") }
65
+ end
66
+
67
+ should 'cope with un-locked paginated results' do
68
+ tally_not_locked = %r|SELECT count\(\*\) as TotalRows from \(SELECT TOP 1000000000 \* FROM \[people\]\s+WITH \(NOLOCK\) \) tally|
69
+ inner_tmp_not_locked = %r|SELECT TOP 15 \* FROM \[people\] WITH \(NOLOCK\)|
70
+ # Currently association limiting is not locked like the parent.
71
+ association_limiting_not_locked = %r|SELECT \[readers\]\.\* FROM \[readers\] WITH \(NOLOCK\) WHERE \(\[readers\]\.person_id IN \(1,2,3,4,5\)\)|
72
+ assert_sql(tally_not_locked,inner_tmp_not_locked) do
73
+ Person.all(:include => :readers, :lock => 'WITH (NOLOCK)', :limit => 5, :offset => 10)
74
+ end
75
+ end
52
76
 
53
77
  end
54
78
 
79
+
55
80
  context 'For dueling concurrent connections' do
56
81
 
57
82
  use_concurrent_connections
@@ -78,7 +78,8 @@ end
78
78
  # Our changes/additions to ActiveRecord test helpers specific for SQL Server.
79
79
 
80
80
  ActiveRecord::Base.connection.class.class_eval do
81
- IGNORED_SQL << /SELECT SCOPE_IDENTITY/ << /INFORMATION_SCHEMA.TABLES/ << /INFORMATION_SCHEMA.COLUMNS/ << /^SELECT @@TRANCOUNT/
81
+ IGNORED_SQL << %r|SELECT SCOPE_IDENTITY| << %r{INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS)}
82
+ IGNORED_SQL << %r|SELECT @@IDENTITY| << %r|SELECT @@ROWCOUNT| << %r|SELECT @@version| << %r|SELECT @@TRANCOUNT|
82
83
  end
83
84
 
84
85
  ActiveRecord::ConnectionAdapters::SQLServerAdapter.class_eval do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-sqlserver-2000-2005-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.14
4
+ version: 2.2.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2009-03-17 00:00:00 -07:00
16
+ date: 2009-03-23 00:00:00 -07:00
17
17
  default_executable:
18
18
  dependencies: []
19
19