@@ -98,7 +98,7 @@ class ClientTest < TinyTds::TestCase
98
98
99
99
it 'raises TinyTds exception with long query past :timeout option' do
100
100
client = new_connection :timeout => 1
101
- action = lambda { client . execute ( "WaitFor Delay '00:00:02'" ) . do }
101
+ action = lambda { client . do ( "WaitFor Delay '00:00:02'" ) }
102
102
assert_raise_tinytds_error ( action ) do |e |
103
103
assert_equal 20003 , e . db_error_number
104
104
assert_equal 6 , e . severity
@@ -111,21 +111,21 @@ class ClientTest < TinyTds::TestCase
111
111
112
112
it 'must not timeout per sql batch when not under transaction' do
113
113
client = new_connection :timeout => 2
114
- client . execute ( "WaitFor Delay '00:00:01'" ) . do
115
- client . execute ( "WaitFor Delay '00:00:01'" ) . do
116
- client . execute ( "WaitFor Delay '00:00:01'" ) . do
114
+ client . do ( "WaitFor Delay '00:00:01'" )
115
+ client . do ( "WaitFor Delay '00:00:01'" )
116
+ client . do ( "WaitFor Delay '00:00:01'" )
117
117
close_client ( client )
118
118
end
119
119
120
120
it 'must not timeout per sql batch when under transaction' do
121
121
client = new_connection :timeout => 2
122
122
begin
123
- client . execute ( "BEGIN TRANSACTION" ) . do
124
- client . execute ( "WaitFor Delay '00:00:01'" ) . do
125
- client . execute ( "WaitFor Delay '00:00:01'" ) . do
126
- client . execute ( "WaitFor Delay '00:00:01'" ) . do
123
+ client . do ( "BEGIN TRANSACTION" )
124
+ client . do ( "WaitFor Delay '00:00:01'" )
125
+ client . do ( "WaitFor Delay '00:00:01'" )
126
+ client . do ( "WaitFor Delay '00:00:01'" )
127
127
ensure
128
- client . execute ( "COMMIT TRANSACTION" ) . do
128
+ client . do ( "COMMIT TRANSACTION" )
129
129
close_client ( client )
130
130
end
131
131
end
@@ -134,7 +134,7 @@ class ClientTest < TinyTds::TestCase
134
134
begin
135
135
client = new_connection timeout : 2 , port : 1234 , host : ENV [ 'TOXIPROXY_HOST' ]
136
136
assert_client_works ( client )
137
- action = lambda { client . execute ( "waitfor delay '00:00:05'" ) . do }
137
+ action = lambda { client . do ( "waitfor delay '00:00:05'" ) }
138
138
139
139
# Use toxiproxy to close the TCP socket after 1 second.
140
140
# We want TinyTds to execute the statement, hit the timeout configured above, and then not be able to use the network to cancel
@@ -157,7 +157,7 @@ class ClientTest < TinyTds::TestCase
157
157
begin
158
158
client = new_connection timeout : 2 , port : 1234 , host : ENV [ 'TOXIPROXY_HOST' ]
159
159
assert_client_works ( client )
160
- action = lambda { client . execute ( "waitfor delay '00:00:05'" ) . do }
160
+ action = lambda { client . do ( "waitfor delay '00:00:05'" ) }
161
161
162
162
# Use toxiproxy to close the network connection after 1 second.
163
163
# We want TinyTds to execute the statement, hit the timeout configured above, and then not be able to use the network to cancel
@@ -278,8 +278,8 @@ class ClientTest < TinyTds::TestCase
278
278
it 'has an #insert method that cancels result rows and returns IDENTITY natively' do
279
279
rollback_transaction ( @client ) do
280
280
text = 'test scope identity rows native'
281
- @client . execute ( "DELETE FROM [datatypes] WHERE [varchar_50] = '#{ text } '" ) . do
282
- @client . execute ( "INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{ text } ')" ) . do
281
+ @client . do ( "DELETE FROM [datatypes] WHERE [varchar_50] = '#{ text } '" )
282
+ @client . do ( "INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{ text } ')" )
283
283
sql_identity = @client . execute ( @client . identity_sql ) . each . first [ 'Ident' ]
284
284
native_identity = @client . insert ( "INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{ text } ')" )
285
285
@@ -295,12 +295,12 @@ class ClientTest < TinyTds::TestCase
295
295
296
296
rollback_transaction ( @client ) do
297
297
seed = 9223372036854775805
298
- @client . execute ( "DELETE FROM [datatypes]" ) . do
298
+ @client . do ( "DELETE FROM [datatypes]" )
299
299
id_constraint_name = @client . execute ( "EXEC sp_helpindex [datatypes]" ) . detect { |row | row [ 'index_keys' ] == 'id' } [ 'index_name' ]
300
- @client . execute ( "ALTER TABLE [datatypes] DROP CONSTRAINT [#{ id_constraint_name } ]" ) . do
301
- @client . execute ( "ALTER TABLE [datatypes] DROP COLUMN [id]" ) . do
302
- @client . execute ( "ALTER TABLE [datatypes] ADD [id] [bigint] NOT NULL IDENTITY(1,1) PRIMARY KEY" ) . do
303
- @client . execute ( "DBCC CHECKIDENT ('datatypes', RESEED, #{ seed } )" ) . do
300
+ @client . do ( "ALTER TABLE [datatypes] DROP CONSTRAINT [#{ id_constraint_name } ]" )
301
+ @client . do ( "ALTER TABLE [datatypes] DROP COLUMN [id]" )
302
+ @client . do ( "ALTER TABLE [datatypes] ADD [id] [bigint] NOT NULL IDENTITY(1,1) PRIMARY KEY" )
303
+ @client . do ( "DBCC CHECKIDENT ('datatypes', RESEED, #{ seed } )" )
304
304
identity = @client . insert ( "INSERT INTO [datatypes] ([varchar_50]) VALUES ('something')" )
305
305
306
306
assert_equal ( seed , identity )
@@ -318,4 +318,35 @@ class ClientTest < TinyTds::TestCase
318
318
end
319
319
end
320
320
end
321
+
322
+ describe "#do" do
323
+ before do
324
+ @client = new_connection
325
+ end
326
+
327
+ it 'has a #do method that cancels result rows and returns affected rows natively' do
328
+ rollback_transaction ( @client ) do
329
+ text = 'test affected rows native'
330
+ count = @client . execute ( "SELECT COUNT(*) AS [count] FROM [datatypes]" ) . each . first [ 'count' ]
331
+ deleted_rows = @client . do ( "DELETE FROM [datatypes]" )
332
+ assert_equal count , deleted_rows , 'should have deleted rows equal to count'
333
+ inserted_rows = @client . do ( "INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{ text } ')" )
334
+ assert_equal 1 , inserted_rows , 'should have inserted row for one above'
335
+ updated_rows = @client . do ( "UPDATE [datatypes] SET [varchar_50] = NULL WHERE [varchar_50] = '#{ text } '" )
336
+ assert_equal 1 , updated_rows , 'should have updated row for one above'
337
+ end
338
+ end
339
+
340
+ it 'allows native affected rows using #do to work under transaction' do
341
+ rollback_transaction ( @client ) do
342
+ text = 'test affected rows native in transaction'
343
+ @client . do ( "BEGIN TRANSACTION" )
344
+ @client . do ( "DELETE FROM [datatypes]" )
345
+ inserted_rows = @client . do ( "INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{ text } ')" )
346
+ assert_equal 1 , inserted_rows , 'should have inserted row for one above'
347
+ updated_rows = @client . do ( "UPDATE [datatypes] SET [varchar_50] = NULL WHERE [varchar_50] = '#{ text } '" )
348
+ assert_equal 1 , updated_rows , 'should have updated row for one above'
349
+ end
350
+ end
351
+ end
321
352
end
0 commit comments