Skip to content

Commit a72f8ca

Browse files
committed
Minor tweaks for Rails 3.1.0.beta1. Prepare to release gem.
1 parent 2b8e374 commit a72f8ca

File tree

9 files changed

+29
-53
lines changed

9 files changed

+29
-53
lines changed

CHANGELOG

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

2+
* 3.1.0.beta1 *
3+
4+
It just works! Uses "EXEC sp_executesql ..." for just about everything now!
5+
6+
27
* 3.0.15 *
38

49
* Way better schema support! Thanks to @ianic! Fixes #61

Gemfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
source :rubygems
33

44
gemspec :path => ENV['RAILS_SOURCE']
5-
# TODO: [Rails31] Remove this rack hack.
6-
gem 'rack', :git => 'git://github.com/rack/rack.git' # master e8563a6 2011-03-30
7-
gem 'arel', :path => ENV['AREL']
85

96
group :tinytds do
107
if ENV['TINYTDS_SOURCE']

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222

2323

2424
task :test => ['test:dblib']
25-
25+
task :default => [:test]
2626

2727
namespace :test do
2828

activerecord-sqlserver-adapter.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Gem::Specification.new do |s|
33
s.platform = Gem::Platform::RUBY
44
s.name = "activerecord-sqlserver-adapter"
5-
s.version = "3.0.15"
5+
s.version = "3.1.0.beta1"
66
s.summary = "SQL Server 2005 and 2008 Adapter For ActiveRecord."
77
s.description = "SQL Server 2005 and 2008 Adapter For ActiveRecord"
88

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,26 @@ def execute(sql, name = nil)
1515
end
1616
end
1717

18-
def exec_query(sql, name = 'SQL', binds = [])
19-
if id_insert_table_name = query_requires_identity_insert?(sql)
20-
with_identity_insert_enabled(id_insert_table_name) do
21-
binds.empty? ? raw_select(sql, name, binds, :ar_result => true) : do_exec_query(sql, name, binds)
22-
end
18+
def exec_query(sql, name = 'SQL', binds = [], sqlserver_options = {})
19+
if id_insert_table_name = sqlserver_options[:insert] ? query_requires_identity_insert?(sql) : nil
20+
with_identity_insert_enabled(id_insert_table_name) { do_exec_query(sql, name, binds) }
2321
else
24-
binds.empty? ? raw_select(sql, name, binds, :ar_result => true) : do_exec_query(sql, name, binds)
22+
do_exec_query(sql, name, binds)
2523
end
2624
end
2725

2826
def exec_insert(sql, name, binds)
29-
exec_query(sql, name, binds)
27+
exec_query sql, name, binds, :insert => true
28+
end
29+
30+
def exec_delete(sql, name, binds)
31+
sql << "; SELECT @@ROWCOUNT AS AffectedRows"
32+
super.rows.first.first
33+
end
34+
35+
def exec_update(sql, name, binds)
36+
sql << "; SELECT @@ROWCOUNT AS AffectedRows"
37+
super.rows.first.first
3038
end
3139

3240
def outside_transaction?
@@ -71,13 +79,6 @@ def empty_insert_statement_value
7179
def case_sensitive_modifier(node)
7280
node.acts_like?(:string) ? Arel::Nodes::Bin.new(node) : node
7381
end
74-
75-
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
76-
match_data = where_sql.match(/^(.*?[\]\) ])WHERE[\[\( ]/)
77-
limit = match_data[1]
78-
where_sql.sub!(limit,'')
79-
"WHERE #{quoted_primary_key} IN (SELECT #{limit} #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"
80-
end
8182

8283
# === SQLServer Specific ======================================== #
8384

@@ -207,23 +208,8 @@ def select(sql, name = nil, binds = [])
207208
exec_query(sql, name, binds).to_a
208209
end
209210

210-
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
211-
super || select_value("SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident")
212-
end
213-
214-
def update_sql(sql, name = nil)
215-
@update_sql = true
216-
case @connection_options[:mode]
217-
when :dblib
218-
execute(sql, name)
219-
else
220-
execute(sql, name)
221-
select_value('SELECT @@ROWCOUNT AS AffectedRows')
222-
end
223-
end
224-
225211
def sql_for_insert(sql, pk, id_value, sequence_name, binds)
226-
sql = "#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident" unless binds.empty?
212+
sql = "#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"# unless binds.empty?
227213
super
228214
end
229215

@@ -268,7 +254,8 @@ def do_exec_query(sql, name, binds)
268254
quoted_value = ar_column ? quote(v,column) : quote(v,nil)
269255
params << "@#{index} = #{quoted_value}"
270256
end
271-
sql = "EXEC sp_executesql #{statement}, #{quote(names_and_types.join(', '))}, #{params.join(', ')}"
257+
sql = "EXEC sp_executesql #{statement}"
258+
sql << ", #{quote(names_and_types.join(', '))}, #{params.join(', ')}" unless binds.empty?
272259
raw_select sql, name, binds, :ar_result => true
273260
end
274261

@@ -341,7 +328,6 @@ def handle_to_names_and_values_dblib(handle, options={})
341328
options[:ar_result] ? ActiveRecord::Result.new(handle.fields, results) : results
342329
end
343330

344-
# TODO [Rails31] Use options[:ar_result]
345331
def handle_to_names_and_values_odbc(handle, options={})
346332
@connection.use_utc = ActiveRecord::Base.default_timezone == :utc
347333
if options[:ar_result]
@@ -358,7 +344,6 @@ def handle_to_names_and_values_odbc(handle, options={})
358344
end
359345
end
360346

361-
# TODO [Rails31] Use options[:ar_result]
362347
def handle_to_names_and_values_adonet(handle, options={})
363348
if handle.has_rows
364349
names = []

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class SQLServerAdapter < AbstractAdapter
161161
include Sqlserver::Errors
162162

163163
ADAPTER_NAME = 'SQLServer'.freeze
164-
VERSION = '3.0.15'.freeze
164+
VERSION = '3.1.0.beta1'.freeze
165165
DATABASE_VERSION_REGEXP = /Microsoft SQL Server\s+"?(\d{4}|\w+)"?/
166166
SUPPORTED_VERSIONS = [2005,2008,2010,2011].freeze
167167

test/cases/adapter_test_sqlserver.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,6 @@ def setup
120120
assert !@connection.send(:insert_sql?,'SELECT...')
121121
end
122122

123-
context 'for #limited_update_conditions' do
124-
125-
should 'only match up to the first WHERE' do
126-
where_sql = "TOP 1 WHERE ([posts].author_id = 1 and [posts].columnWHEREname = 2) ORDER BY posts.id"
127-
assert_equal "WHERE bar IN (SELECT TOP 1 bar FROM foo WHERE ([posts].author_id = 1 and [posts].columnWHEREname = 2) ORDER BY posts.id)", @connection.limited_update_conditions(where_sql, 'foo', 'bar')
128-
end
129-
130-
end
131-
132123
context 'for #get_table_name' do
133124

134125
should 'return quoted table name from basic INSERT, UPDATE and SELECT statements' do

test/cases/offset_and_limit_test_sqlserver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
3737
end
3838

3939
should 'not convert strings which look like integers to integers' do
40-
assert_sql(/WHERE \[__rnt\]\.\[__rn\] > \(N'5'\)/) { Book.limit(10).offset('5').all }
40+
assert_sql(/WHERE \[__rnt\]\.\[__rn\] > \(N''5''\)/) { Book.limit(10).offset('5').all }
4141
end
4242

4343
should 'alter SQL to limit number of records returned offset by specified amount' do
44-
sql = %|SELECT TOP (3) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [books].[id] ASC) AS [__rn], [books].* FROM [books] ) AS [__rnt] WHERE [__rnt].[__rn] > (5)|
44+
sql = %|EXEC sp_executesql N'SELECT TOP (3) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [books].[id] ASC) AS [__rn], [books].* FROM [books] ) AS [__rnt] WHERE [__rnt].[__rn] > (5)'|
4545
assert_sql(sql) { Book.limit(3).offset(5).all }
4646
end
4747

test/cases/sqlserver_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ def method_added(method)
8282
module ActiveRecord
8383
class SQLCounter
8484
IGNORED_SQL << %r|SELECT SCOPE_IDENTITY| << %r{INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS)} <<
85-
%r|SELECT @@IDENTITY| << %r|SELECT @@ROWCOUNT| << %r|SELECT @@version| << %r|SELECT @@TRANCOUNT| <<
86-
%r{(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION}
87-
85+
%r|SELECT @@version| << %r|SELECT @@TRANCOUNT| << %r{(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION}
8886
end
8987
end
9088

0 commit comments

Comments
 (0)