tiny_tds 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +187 -74
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -1
- data/README.md +59 -50
- data/Rakefile +46 -37
- data/VERSION +1 -1
- data/ext/tiny_tds/extconf.rb +169 -70
- data/ext/tiny_tds/extconsts.rb +3 -4
- data/lib/tiny_tds/bin.rb +12 -26
- data/lib/tiny_tds/client.rb +38 -42
- data/lib/tiny_tds/error.rb +0 -2
- data/lib/tiny_tds/gem.rb +5 -9
- data/lib/tiny_tds/result.rb +0 -2
- data/lib/tiny_tds/version.rb +1 -1
- data/lib/tiny_tds.rb +28 -47
- data/tasks/native_gem.rake +11 -18
- data/tasks/package.rake +1 -3
- data/tasks/ports.rake +7 -91
- data/tasks/test.rake +3 -5
- data/test/bin/install-freetds.sh +2 -4
- data/test/bin/restore-from-native-gem.ps1 +10 -0
- data/test/client_test.rb +106 -112
- data/test/gem_test.rb +31 -107
- data/test/result_test.rb +208 -221
- data/test/schema_test.rb +177 -181
- data/test/test_helper.rb +58 -63
- data/test/thread_test.rb +22 -31
- data/tiny_tds.gemspec +28 -29
- metadata +21 -13
- data/tasks/ports/freetds.rb +0 -32
- data/tasks/ports/libiconv.rb +0 -26
- data/tasks/ports/openssl.rb +0 -62
- data/tasks/ports/recipe.rb +0 -64
- data/test/benchmark/query.rb +0 -77
- data/test/benchmark/query_odbc.rb +0 -106
- data/test/benchmark/query_tinytds.rb +0 -126
data/test/result_test.rb
CHANGED
@@ -1,43 +1,40 @@
|
|
1
|
-
|
2
|
-
require 'test_helper'
|
1
|
+
require "test_helper"
|
3
2
|
|
4
3
|
class ResultTest < TinyTds::TestCase
|
5
|
-
|
6
|
-
describe 'Basic query and result' do
|
7
|
-
|
4
|
+
describe "Basic query and result" do
|
8
5
|
before do
|
9
6
|
@@current_schema_loaded ||= load_current_schema
|
10
7
|
@client = new_connection
|
11
|
-
@query1 =
|
8
|
+
@query1 = "SELECT 1 AS [one]"
|
12
9
|
end
|
13
10
|
|
14
|
-
it
|
11
|
+
it "has included Enumerable" do
|
15
12
|
assert TinyTds::Result.ancestors.include?(Enumerable)
|
16
13
|
end
|
17
14
|
|
18
|
-
it
|
15
|
+
it "responds to #each" do
|
19
16
|
result = @client.execute(@query1)
|
20
17
|
assert result.respond_to?(:each)
|
21
18
|
end
|
22
19
|
|
23
|
-
it
|
20
|
+
it "returns all results for #each with no block" do
|
24
21
|
result = @client.execute(@query1)
|
25
22
|
data = result.each
|
26
23
|
row = data.first
|
27
24
|
assert_instance_of Array, data
|
28
25
|
assert_equal 1, data.size
|
29
|
-
assert_instance_of Hash, row,
|
26
|
+
assert_instance_of Hash, row, "hash is the default query option"
|
30
27
|
end
|
31
28
|
|
32
|
-
it
|
29
|
+
it "returns all results for #each with a block yielding a row at a time" do
|
33
30
|
result = @client.execute(@query1)
|
34
31
|
data = result.each do |row|
|
35
|
-
assert_instance_of Hash, row,
|
32
|
+
assert_instance_of Hash, row, "hash is the default query option"
|
36
33
|
end
|
37
34
|
assert_instance_of Array, data
|
38
35
|
end
|
39
36
|
|
40
|
-
it
|
37
|
+
it "allows successive calls to each returning the same data" do
|
41
38
|
result = @client.execute(@query1)
|
42
39
|
data = result.each
|
43
40
|
result.each
|
@@ -45,150 +42,150 @@ class ResultTest < TinyTds::TestCase
|
|
45
42
|
assert_equal data.first.object_id, result.each.first.object_id
|
46
43
|
end
|
47
44
|
|
48
|
-
it
|
45
|
+
it "returns hashes with string keys" do
|
49
46
|
result = @client.execute(@query1)
|
50
|
-
row = result.each(:
|
47
|
+
row = result.each(as: :hash, symbolize_keys: false).first
|
51
48
|
assert_instance_of Hash, row
|
52
|
-
assert_equal [
|
53
|
-
assert_equal [
|
49
|
+
assert_equal ["one"], row.keys
|
50
|
+
assert_equal ["one"], result.fields
|
54
51
|
end
|
55
52
|
|
56
|
-
it
|
53
|
+
it "returns hashes with symbol keys" do
|
57
54
|
result = @client.execute(@query1)
|
58
|
-
row = result.each(:
|
55
|
+
row = result.each(as: :hash, symbolize_keys: true).first
|
59
56
|
assert_instance_of Hash, row
|
60
57
|
assert_equal [:one], row.keys
|
61
58
|
assert_equal [:one], result.fields
|
62
59
|
end
|
63
60
|
|
64
|
-
it
|
61
|
+
it "returns arrays with string fields" do
|
65
62
|
result = @client.execute(@query1)
|
66
|
-
row = result.each(:
|
63
|
+
row = result.each(as: :array, symbolize_keys: false).first
|
67
64
|
assert_instance_of Array, row
|
68
|
-
assert_equal [
|
65
|
+
assert_equal ["one"], result.fields
|
69
66
|
end
|
70
67
|
|
71
|
-
it
|
68
|
+
it "returns arrays with symbol fields" do
|
72
69
|
result = @client.execute(@query1)
|
73
|
-
row = result.each(:
|
70
|
+
row = result.each(as: :array, symbolize_keys: true).first
|
74
71
|
assert_instance_of Array, row
|
75
72
|
assert_equal [:one], result.fields
|
76
73
|
end
|
77
74
|
|
78
|
-
it
|
75
|
+
it "allows sql concat + to work" do
|
79
76
|
rollback_transaction(@client) do
|
80
77
|
@client.execute("DELETE FROM [datatypes]").do
|
81
78
|
@client.execute("INSERT INTO [datatypes] ([char_10], [varchar_50]) VALUES ('1', '2')").do
|
82
|
-
result = @client.execute("SELECT TOP (1) [char_10] + 'test' + [varchar_50] AS [test] FROM [datatypes]").each.first[
|
79
|
+
result = @client.execute("SELECT TOP (1) [char_10] + 'test' + [varchar_50] AS [test] FROM [datatypes]").each.first["test"]
|
83
80
|
_(result).must_equal "1 test2"
|
84
81
|
end
|
85
82
|
end
|
86
83
|
|
87
|
-
it
|
84
|
+
it "must be able to turn :cache_rows option off" do
|
88
85
|
result = @client.execute(@query1)
|
89
86
|
local = []
|
90
|
-
result.each(:
|
87
|
+
result.each(cache_rows: false) do |row|
|
91
88
|
local << row
|
92
89
|
end
|
93
|
-
assert local.first,
|
94
|
-
assert_equal [], result.each,
|
95
|
-
assert_equal [
|
90
|
+
assert local.first, "should have iterated over each row"
|
91
|
+
assert_equal [], result.each, "should not have been cached"
|
92
|
+
assert_equal ["one"], result.fields, "should still cache field names"
|
96
93
|
end
|
97
94
|
|
98
|
-
it
|
95
|
+
it "must be able to get the first result row only" do
|
99
96
|
load_current_schema
|
100
97
|
big_query = "SELECT [id] FROM [datatypes]"
|
101
|
-
one = @client.execute(big_query).each(:
|
98
|
+
one = @client.execute(big_query).each(first: true)
|
102
99
|
many = @client.execute(big_query).each
|
103
100
|
assert many.size > 1
|
104
101
|
assert one.size == 1
|
105
102
|
end
|
106
103
|
|
107
|
-
it
|
108
|
-
data = @client.execute("SELECT [id] FROM [datatypes] WHERE [id] = -1").each(:
|
104
|
+
it "copes with no results when using first option" do
|
105
|
+
data = @client.execute("SELECT [id] FROM [datatypes] WHERE [id] = -1").each(first: true)
|
109
106
|
assert_equal [], data
|
110
107
|
end
|
111
108
|
|
112
|
-
it
|
109
|
+
it "must delete, insert and find data" do
|
113
110
|
rollback_transaction(@client) do
|
114
|
-
text =
|
111
|
+
text = "test insert and delete"
|
115
112
|
@client.execute("DELETE FROM [datatypes] WHERE [varchar_50] IS NOT NULL").do
|
116
113
|
@client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
|
117
114
|
row = @client.execute("SELECT [varchar_50] FROM [datatypes] WHERE [varchar_50] IS NOT NULL").each.first
|
118
115
|
assert row
|
119
|
-
assert_equal text, row[
|
116
|
+
assert_equal text, row["varchar_50"]
|
120
117
|
end
|
121
118
|
end
|
122
119
|
|
123
|
-
it
|
120
|
+
it "must insert and find unicode data" do
|
124
121
|
rollback_transaction(@client) do
|
125
|
-
text =
|
122
|
+
text = "😍"
|
126
123
|
@client.execute("DELETE FROM [datatypes] WHERE [nvarchar_50] IS NOT NULL").do
|
127
124
|
@client.execute("INSERT INTO [datatypes] ([nvarchar_50]) VALUES (N'#{text}')").do
|
128
125
|
row = @client.execute("SELECT [nvarchar_50] FROM [datatypes] WHERE [nvarchar_50] IS NOT NULL").each.first
|
129
|
-
assert_equal text, row[
|
126
|
+
assert_equal text, row["nvarchar_50"]
|
130
127
|
end
|
131
128
|
end
|
132
129
|
|
133
|
-
it
|
130
|
+
it "must delete and update with affected rows support and insert with identity support in native sql" do
|
134
131
|
rollback_transaction(@client) do
|
135
|
-
text =
|
132
|
+
text = "test affected rows sql"
|
136
133
|
@client.execute("DELETE FROM [datatypes]").do
|
137
|
-
afrows = @client.execute("SELECT @@ROWCOUNT AS AffectedRows").each.first[
|
138
|
-
_([
|
134
|
+
afrows = @client.execute("SELECT @@ROWCOUNT AS AffectedRows").each.first["AffectedRows"]
|
135
|
+
_(["Fixnum", "Integer"]).must_include afrows.class.name
|
139
136
|
@client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
|
140
|
-
pk1 = @client.execute(@client.identity_sql).each.first[
|
141
|
-
_([
|
137
|
+
pk1 = @client.execute(@client.identity_sql).each.first["Ident"]
|
138
|
+
_(["Fixnum", "Integer"]).must_include pk1.class.name, "we it be able to CAST to bigint"
|
142
139
|
@client.execute("UPDATE [datatypes] SET [varchar_50] = NULL WHERE [varchar_50] = '#{text}'").do
|
143
|
-
afrows = @client.execute("SELECT @@ROWCOUNT AS AffectedRows").each.first[
|
140
|
+
afrows = @client.execute("SELECT @@ROWCOUNT AS AffectedRows").each.first["AffectedRows"]
|
144
141
|
assert_equal 1, afrows
|
145
142
|
end
|
146
143
|
end
|
147
144
|
|
148
|
-
it
|
145
|
+
it "has a #do method that cancels result rows and returns affected rows natively" do
|
149
146
|
rollback_transaction(@client) do
|
150
|
-
text =
|
151
|
-
count = @client.execute("SELECT COUNT(*) AS [count] FROM [datatypes]").each.first[
|
147
|
+
text = "test affected rows native"
|
148
|
+
count = @client.execute("SELECT COUNT(*) AS [count] FROM [datatypes]").each.first["count"]
|
152
149
|
deleted_rows = @client.execute("DELETE FROM [datatypes]").do
|
153
|
-
assert_equal count, deleted_rows,
|
150
|
+
assert_equal count, deleted_rows, "should have deleted rows equal to count"
|
154
151
|
inserted_rows = @client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
|
155
|
-
assert_equal 1, inserted_rows,
|
152
|
+
assert_equal 1, inserted_rows, "should have inserted row for one above"
|
156
153
|
updated_rows = @client.execute("UPDATE [datatypes] SET [varchar_50] = NULL WHERE [varchar_50] = '#{text}'").do
|
157
|
-
assert_equal 1, updated_rows,
|
154
|
+
assert_equal 1, updated_rows, "should have updated row for one above"
|
158
155
|
end
|
159
156
|
end
|
160
157
|
|
161
|
-
it
|
158
|
+
it "allows native affected rows using #do to work under transaction" do
|
162
159
|
rollback_transaction(@client) do
|
163
|
-
text =
|
160
|
+
text = "test affected rows native in transaction"
|
164
161
|
@client.execute("BEGIN TRANSACTION").do
|
165
162
|
@client.execute("DELETE FROM [datatypes]").do
|
166
163
|
inserted_rows = @client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
|
167
|
-
assert_equal 1, inserted_rows,
|
164
|
+
assert_equal 1, inserted_rows, "should have inserted row for one above"
|
168
165
|
updated_rows = @client.execute("UPDATE [datatypes] SET [varchar_50] = NULL WHERE [varchar_50] = '#{text}'").do
|
169
|
-
assert_equal 1, updated_rows,
|
166
|
+
assert_equal 1, updated_rows, "should have updated row for one above"
|
170
167
|
end
|
171
168
|
end
|
172
169
|
|
173
|
-
it
|
170
|
+
it "has an #insert method that cancels result rows and returns IDENTITY natively" do
|
174
171
|
rollback_transaction(@client) do
|
175
|
-
text =
|
172
|
+
text = "test scope identity rows native"
|
176
173
|
@client.execute("DELETE FROM [datatypes] WHERE [varchar_50] = '#{text}'").do
|
177
174
|
@client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").do
|
178
|
-
sql_identity = @client.execute(@client.identity_sql).each.first[
|
175
|
+
sql_identity = @client.execute(@client.identity_sql).each.first["Ident"]
|
179
176
|
native_identity = @client.execute("INSERT INTO [datatypes] ([varchar_50]) VALUES ('#{text}')").insert
|
180
177
|
assert_equal sql_identity + 1, native_identity
|
181
178
|
end
|
182
179
|
end
|
183
180
|
|
184
|
-
it
|
181
|
+
it "returns bigint for #insert when needed" do
|
185
182
|
return if sqlserver_azure? # We can not alter clustered index like this test does.
|
186
183
|
# 'CREATE TABLE' command is not allowed within a multi-statement transaction
|
187
184
|
# and and sp_helpindex creates a temporary table #spindtab.
|
188
185
|
rollback_transaction(@client) do
|
189
186
|
seed = 9223372036854775805
|
190
187
|
@client.execute("DELETE FROM [datatypes]").do
|
191
|
-
id_constraint_name = @client.execute("EXEC sp_helpindex [datatypes]").detect { |row| row[
|
188
|
+
id_constraint_name = @client.execute("EXEC sp_helpindex [datatypes]").detect { |row| row["index_keys"] == "id" }["index_name"]
|
192
189
|
@client.execute("ALTER TABLE [datatypes] DROP CONSTRAINT [#{id_constraint_name}]").do
|
193
190
|
@client.execute("ALTER TABLE [datatypes] DROP COLUMN [id]").do
|
194
191
|
@client.execute("ALTER TABLE [datatypes] ADD [id] [bigint] NOT NULL IDENTITY(1,1) PRIMARY KEY").do
|
@@ -198,59 +195,59 @@ class ResultTest < TinyTds::TestCase
|
|
198
195
|
end
|
199
196
|
end
|
200
197
|
|
201
|
-
it
|
198
|
+
it "must be able to begin/commit transactions with raw sql" do
|
202
199
|
rollback_transaction(@client) do
|
203
200
|
@client.execute("BEGIN TRANSACTION").do
|
204
201
|
@client.execute("DELETE FROM [datatypes]").do
|
205
202
|
@client.execute("COMMIT TRANSACTION").do
|
206
|
-
count = @client.execute("SELECT COUNT(*) AS [count] FROM [datatypes]").each.first[
|
203
|
+
count = @client.execute("SELECT COUNT(*) AS [count] FROM [datatypes]").each.first["count"]
|
207
204
|
assert_equal 0, count
|
208
205
|
end
|
209
206
|
end
|
210
207
|
|
211
|
-
it
|
208
|
+
it "must be able to begin/rollback transactions with raw sql" do
|
212
209
|
load_current_schema
|
213
210
|
@client.execute("BEGIN TRANSACTION").do
|
214
211
|
@client.execute("DELETE FROM [datatypes]").do
|
215
212
|
@client.execute("ROLLBACK TRANSACTION").do
|
216
|
-
count = @client.execute("SELECT COUNT(*) AS [count] FROM [datatypes]").each.first[
|
213
|
+
count = @client.execute("SELECT COUNT(*) AS [count] FROM [datatypes]").each.first["count"]
|
217
214
|
_(count).wont_equal 0
|
218
215
|
end
|
219
216
|
|
220
|
-
it
|
217
|
+
it "has a #fields accessor with logic default and valid outcome" do
|
221
218
|
result = @client.execute(@query1)
|
222
|
-
_(result.fields).must_equal [
|
219
|
+
_(result.fields).must_equal ["one"]
|
223
220
|
result.each
|
224
|
-
_(result.fields).must_equal [
|
221
|
+
_(result.fields).must_equal ["one"]
|
225
222
|
end
|
226
223
|
|
227
|
-
it
|
224
|
+
it "always returns an array for fields for all sql" do
|
228
225
|
result = @client.execute("USE [tinytdstest]")
|
229
226
|
_(result.fields).must_equal []
|
230
227
|
result.do
|
231
228
|
_(result.fields).must_equal []
|
232
229
|
end
|
233
230
|
|
234
|
-
it
|
231
|
+
it "returns fields even when no results are found" do
|
235
232
|
no_results_query = "SELECT [id], [varchar_50] FROM [datatypes] WHERE [varchar_50] = 'NOTFOUND'"
|
236
233
|
# Fields before each.
|
237
234
|
result = @client.execute(no_results_query)
|
238
|
-
_(result.fields).must_equal [
|
235
|
+
_(result.fields).must_equal ["id", "varchar_50"]
|
239
236
|
result.each
|
240
|
-
_(result.fields).must_equal [
|
237
|
+
_(result.fields).must_equal ["id", "varchar_50"]
|
241
238
|
# Each then fields
|
242
239
|
result = @client.execute(no_results_query)
|
243
240
|
result.each
|
244
|
-
_(result.fields).must_equal [
|
241
|
+
_(result.fields).must_equal ["id", "varchar_50"]
|
245
242
|
end
|
246
243
|
|
247
|
-
it
|
244
|
+
it "allows the result to be canceled before reading" do
|
248
245
|
result = @client.execute(@query1)
|
249
246
|
result.cancel
|
250
247
|
@client.execute(@query1).each
|
251
248
|
end
|
252
249
|
|
253
|
-
it
|
250
|
+
it "works in tandem with the client when needing to find out if client has sql sent and result is canceled or not" do
|
254
251
|
# Default state.
|
255
252
|
@client = TinyTds::Client.new(connection_options)
|
256
253
|
_(@client.sqlsent?).must_equal false
|
@@ -262,21 +259,21 @@ class ResultTest < TinyTds::TestCase
|
|
262
259
|
result.cancel
|
263
260
|
_(@client.sqlsent?).must_equal false
|
264
261
|
_(@client.canceled?).must_equal true
|
265
|
-
assert result.cancel,
|
262
|
+
assert result.cancel, "must be safe to call again"
|
266
263
|
# With each and no block.
|
267
264
|
@client.execute(@query1).each
|
268
265
|
_(@client.sqlsent?).must_equal false
|
269
266
|
_(@client.canceled?).must_equal false
|
270
267
|
# With each and block.
|
271
268
|
@client.execute(@query1).each do |row|
|
272
|
-
_(@client.sqlsent?).must_equal true,
|
269
|
+
_(@client.sqlsent?).must_equal true, "when iterating over each row in a block"
|
273
270
|
_(@client.canceled?).must_equal false
|
274
271
|
end
|
275
272
|
_(@client.sqlsent?).must_equal false
|
276
273
|
_(@client.canceled?).must_equal false
|
277
274
|
# With each and block canceled half way thru.
|
278
|
-
count = @client.execute("SELECT COUNT([id]) AS [count] FROM [datatypes]").each[0][
|
279
|
-
assert count > 10,
|
275
|
+
count = @client.execute("SELECT COUNT([id]) AS [count] FROM [datatypes]").each[0]["count"]
|
276
|
+
assert count > 10, "since we want to cancel early for test"
|
280
277
|
result = @client.execute("SELECT [id] FROM [datatypes]")
|
281
278
|
index = 0
|
282
279
|
result.each do |row|
|
@@ -299,48 +296,51 @@ class ResultTest < TinyTds::TestCase
|
|
299
296
|
_(@client.canceled?).must_equal true
|
300
297
|
end
|
301
298
|
# With first
|
302
|
-
@client.execute("SELECT [id] FROM [datatypes]").each(:
|
299
|
+
@client.execute("SELECT [id] FROM [datatypes]").each(first: true)
|
303
300
|
_(@client.sqlsent?).must_equal false
|
304
301
|
_(@client.canceled?).must_equal true
|
305
302
|
end
|
306
303
|
|
307
|
-
it
|
304
|
+
it "use same string object for hash keys" do
|
308
305
|
data = @client.execute("SELECT [id], [bigint] FROM [datatypes]").each
|
309
306
|
assert_equal data.first.keys.map { |r| r.object_id }, data.last.keys.map { |r| r.object_id }
|
310
307
|
end
|
311
308
|
|
312
|
-
it
|
309
|
+
it "has properly encoded column names with symbol keys" do
|
313
310
|
col_name = "öäüß"
|
314
|
-
|
311
|
+
begin
|
312
|
+
@client.execute("DROP TABLE [test_encoding]").do
|
313
|
+
rescue
|
314
|
+
nil
|
315
|
+
end
|
315
316
|
@client.execute("CREATE TABLE [dbo].[test_encoding] ( [id] int NOT NULL IDENTITY(1,1) PRIMARY KEY, [#{col_name}] [nvarchar](10) NOT NULL )").do
|
316
317
|
@client.execute("INSERT INTO [test_encoding] ([#{col_name}]) VALUES (N'#{col_name}')").do
|
317
318
|
result = @client.execute("SELECT [#{col_name}] FROM [test_encoding]")
|
318
|
-
row = result.each(:
|
319
|
+
row = result.each(as: :hash, symbolize_keys: true).first
|
319
320
|
assert_instance_of Symbol, result.fields.first
|
320
321
|
assert_equal col_name.to_sym, result.fields.first
|
321
322
|
assert_instance_of Symbol, row.keys.first
|
322
323
|
assert_equal col_name.to_sym, row.keys.first
|
323
324
|
end
|
324
325
|
|
325
|
-
it
|
326
|
+
it "allows #return_code to work with stored procedures and reset per sql batch" do
|
326
327
|
assert_nil @client.return_code
|
327
328
|
result = @client.execute("EXEC tinytds_TestReturnCodes")
|
328
|
-
assert_equal [{
|
329
|
+
assert_equal [{"one" => 1}], result.each
|
329
330
|
assert_equal 420, @client.return_code
|
330
331
|
assert_equal 420, result.return_code
|
331
|
-
result = @client.execute(
|
332
|
+
result = @client.execute("SELECT 1 as [one]")
|
332
333
|
result.each
|
333
334
|
assert_nil @client.return_code
|
334
335
|
assert_nil result.return_code
|
335
336
|
end
|
336
337
|
|
337
|
-
it
|
338
|
-
v = @client.execute("SELECT LOGINPROPERTY('sa', 'IsLocked') as v").first[
|
338
|
+
it "with LOGINPROPERTY function" do
|
339
|
+
v = @client.execute("SELECT LOGINPROPERTY('sa', 'IsLocked') as v").first["v"]
|
339
340
|
_(v).must_equal 0
|
340
341
|
end
|
341
342
|
|
342
|
-
describe
|
343
|
-
|
343
|
+
describe "with multiple result sets" do
|
344
344
|
before do
|
345
345
|
@empty_select = "SELECT 1 AS [rs1] WHERE 1 = 0"
|
346
346
|
@double_select = "SELECT 1 AS [rs1]
|
@@ -356,42 +356,41 @@ class ResultTest < TinyTds::TestCase
|
|
356
356
|
SELECT 3 AS [rs3] WHERE 1 = 0"
|
357
357
|
end
|
358
358
|
|
359
|
-
it
|
359
|
+
it "handles a command buffer with double selects" do
|
360
360
|
result = @client.execute(@double_select)
|
361
361
|
result_sets = result.each
|
362
362
|
assert_equal 2, result_sets.size
|
363
|
-
assert_equal [{
|
364
|
-
assert_equal [{
|
365
|
-
assert_equal [[
|
366
|
-
assert_equal result.each.object_id, result.each.object_id,
|
363
|
+
assert_equal [{"rs1" => 1}], result_sets.first
|
364
|
+
assert_equal [{"rs2" => 2}], result_sets.last
|
365
|
+
assert_equal [["rs1"], ["rs2"]], result.fields
|
366
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
367
367
|
# As array
|
368
368
|
result = @client.execute(@double_select)
|
369
|
-
result_sets = result.each(:
|
369
|
+
result_sets = result.each(as: :array)
|
370
370
|
assert_equal 2, result_sets.size
|
371
371
|
assert_equal [[1]], result_sets.first
|
372
372
|
assert_equal [[2]], result_sets.last
|
373
|
-
assert_equal [[
|
374
|
-
assert_equal result.each.object_id, result.each.object_id,
|
373
|
+
assert_equal [["rs1"], ["rs2"]], result.fields
|
374
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
375
375
|
end
|
376
376
|
|
377
|
-
it
|
377
|
+
it "yields each row for each result set" do
|
378
378
|
data = []
|
379
379
|
result_sets = @client.execute(@double_select).each { |row| data << row }
|
380
380
|
assert_equal data.first, result_sets.first[0]
|
381
381
|
assert_equal data.last, result_sets.last[0]
|
382
382
|
end
|
383
383
|
|
384
|
-
it
|
384
|
+
it "works from a stored procedure" do
|
385
385
|
results1, results2 = @client.execute("EXEC sp_helpconstraint '[datatypes]'").each
|
386
|
-
assert_equal [{
|
386
|
+
assert_equal [{"Object Name" => "[datatypes]"}], results1
|
387
387
|
constraint_info = results2.first
|
388
388
|
assert constraint_info.key?("constraint_keys")
|
389
389
|
assert constraint_info.key?("constraint_type")
|
390
390
|
assert constraint_info.key?("constraint_name")
|
391
391
|
end
|
392
392
|
|
393
|
-
describe
|
394
|
-
|
393
|
+
describe "using :empty_sets TRUE" do
|
395
394
|
before do
|
396
395
|
close_client
|
397
396
|
@old_query_option_value = TinyTds::Client.default_query_options[:empty_sets]
|
@@ -403,76 +402,74 @@ class ResultTest < TinyTds::TestCase
|
|
403
402
|
TinyTds::Client.default_query_options[:empty_sets] = @old_query_option_value
|
404
403
|
end
|
405
404
|
|
406
|
-
it
|
405
|
+
it "handles a basic empty result set" do
|
407
406
|
result = @client.execute(@empty_select)
|
408
407
|
assert_equal [], result.each
|
409
|
-
assert_equal [
|
408
|
+
assert_equal ["rs1"], result.fields
|
410
409
|
end
|
411
410
|
|
412
|
-
it
|
411
|
+
it "includes empty result sets by default - using 1st empty buffer" do
|
413
412
|
result = @client.execute(@triple_select_1st_empty)
|
414
413
|
result_sets = result.each
|
415
414
|
assert_equal 3, result_sets.size
|
416
415
|
assert_equal [], result_sets[0]
|
417
|
-
assert_equal [{
|
418
|
-
assert_equal [{
|
419
|
-
assert_equal [[
|
420
|
-
assert_equal result.each.object_id, result.each.object_id,
|
416
|
+
assert_equal [{"rs2" => 2}], result_sets[1]
|
417
|
+
assert_equal [{"rs3" => 3}], result_sets[2]
|
418
|
+
assert_equal [["rs1"], ["rs2"], ["rs3"]], result.fields
|
419
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
421
420
|
# As array
|
422
421
|
result = @client.execute(@triple_select_1st_empty)
|
423
|
-
result_sets = result.each(:
|
422
|
+
result_sets = result.each(as: :array)
|
424
423
|
assert_equal 3, result_sets.size
|
425
424
|
assert_equal [], result_sets[0]
|
426
425
|
assert_equal [[2]], result_sets[1]
|
427
426
|
assert_equal [[3]], result_sets[2]
|
428
|
-
assert_equal [[
|
429
|
-
assert_equal result.each.object_id, result.each.object_id,
|
427
|
+
assert_equal [["rs1"], ["rs2"], ["rs3"]], result.fields
|
428
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
430
429
|
end
|
431
430
|
|
432
|
-
it
|
431
|
+
it "includes empty result sets by default - using 2nd empty buffer" do
|
433
432
|
result = @client.execute(@triple_select_2nd_empty)
|
434
433
|
result_sets = result.each
|
435
434
|
assert_equal 3, result_sets.size
|
436
|
-
assert_equal [{
|
435
|
+
assert_equal [{"rs1" => 1}], result_sets[0]
|
437
436
|
assert_equal [], result_sets[1]
|
438
|
-
assert_equal [{
|
439
|
-
assert_equal [[
|
440
|
-
assert_equal result.each.object_id, result.each.object_id,
|
437
|
+
assert_equal [{"rs3" => 3}], result_sets[2]
|
438
|
+
assert_equal [["rs1"], ["rs2"], ["rs3"]], result.fields
|
439
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
441
440
|
# As array
|
442
441
|
result = @client.execute(@triple_select_2nd_empty)
|
443
|
-
result_sets = result.each(:
|
442
|
+
result_sets = result.each(as: :array)
|
444
443
|
assert_equal 3, result_sets.size
|
445
444
|
assert_equal [[1]], result_sets[0]
|
446
445
|
assert_equal [], result_sets[1]
|
447
446
|
assert_equal [[3]], result_sets[2]
|
448
|
-
assert_equal [[
|
449
|
-
assert_equal result.each.object_id, result.each.object_id,
|
447
|
+
assert_equal [["rs1"], ["rs2"], ["rs3"]], result.fields
|
448
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
450
449
|
end
|
451
450
|
|
452
|
-
it
|
451
|
+
it "includes empty result sets by default - using 3rd empty buffer" do
|
453
452
|
result = @client.execute(@triple_select_3rd_empty)
|
454
453
|
result_sets = result.each
|
455
454
|
assert_equal 3, result_sets.size
|
456
|
-
assert_equal [{
|
457
|
-
assert_equal [{
|
455
|
+
assert_equal [{"rs1" => 1}], result_sets[0]
|
456
|
+
assert_equal [{"rs2" => 2}], result_sets[1]
|
458
457
|
assert_equal [], result_sets[2]
|
459
|
-
assert_equal [[
|
460
|
-
assert_equal result.each.object_id, result.each.object_id,
|
458
|
+
assert_equal [["rs1"], ["rs2"], ["rs3"]], result.fields
|
459
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
461
460
|
# As array
|
462
461
|
result = @client.execute(@triple_select_3rd_empty)
|
463
|
-
result_sets = result.each(:
|
462
|
+
result_sets = result.each(as: :array)
|
464
463
|
assert_equal 3, result_sets.size
|
465
464
|
assert_equal [[1]], result_sets[0]
|
466
465
|
assert_equal [[2]], result_sets[1]
|
467
466
|
assert_equal [], result_sets[2]
|
468
|
-
assert_equal [[
|
469
|
-
assert_equal result.each.object_id, result.each.object_id,
|
467
|
+
assert_equal [["rs1"], ["rs2"], ["rs3"]], result.fields
|
468
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
470
469
|
end
|
471
|
-
|
472
470
|
end
|
473
471
|
|
474
|
-
describe
|
475
|
-
|
472
|
+
describe "using :empty_sets FALSE" do
|
476
473
|
before do
|
477
474
|
close_client
|
478
475
|
@old_query_option_value = TinyTds::Client.default_query_options[:empty_sets]
|
@@ -484,169 +481,160 @@ class ResultTest < TinyTds::TestCase
|
|
484
481
|
TinyTds::Client.default_query_options[:empty_sets] = @old_query_option_value
|
485
482
|
end
|
486
483
|
|
487
|
-
it
|
484
|
+
it "handles a basic empty result set" do
|
488
485
|
result = @client.execute(@empty_select)
|
489
486
|
assert_equal [], result.each
|
490
|
-
assert_equal [
|
487
|
+
assert_equal ["rs1"], result.fields
|
491
488
|
end
|
492
489
|
|
493
|
-
it
|
490
|
+
it "must not include empty result sets by default - using 1st empty buffer" do
|
494
491
|
result = @client.execute(@triple_select_1st_empty)
|
495
492
|
result_sets = result.each
|
496
493
|
assert_equal 2, result_sets.size
|
497
|
-
assert_equal [{
|
498
|
-
assert_equal [{
|
499
|
-
assert_equal [[
|
500
|
-
assert_equal result.each.object_id, result.each.object_id,
|
494
|
+
assert_equal [{"rs2" => 2}], result_sets[0]
|
495
|
+
assert_equal [{"rs3" => 3}], result_sets[1]
|
496
|
+
assert_equal [["rs2"], ["rs3"]], result.fields
|
497
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
501
498
|
# As array
|
502
499
|
result = @client.execute(@triple_select_1st_empty)
|
503
|
-
result_sets = result.each(:
|
500
|
+
result_sets = result.each(as: :array)
|
504
501
|
assert_equal 2, result_sets.size
|
505
502
|
assert_equal [[2]], result_sets[0]
|
506
503
|
assert_equal [[3]], result_sets[1]
|
507
|
-
assert_equal [[
|
508
|
-
assert_equal result.each.object_id, result.each.object_id,
|
504
|
+
assert_equal [["rs2"], ["rs3"]], result.fields
|
505
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
509
506
|
end
|
510
507
|
|
511
|
-
it
|
508
|
+
it "must not include empty result sets by default - using 2nd empty buffer" do
|
512
509
|
result = @client.execute(@triple_select_2nd_empty)
|
513
510
|
result_sets = result.each
|
514
511
|
assert_equal 2, result_sets.size
|
515
|
-
assert_equal [{
|
516
|
-
assert_equal [{
|
517
|
-
assert_equal [[
|
518
|
-
assert_equal result.each.object_id, result.each.object_id,
|
512
|
+
assert_equal [{"rs1" => 1}], result_sets[0]
|
513
|
+
assert_equal [{"rs3" => 3}], result_sets[1]
|
514
|
+
assert_equal [["rs1"], ["rs3"]], result.fields
|
515
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
519
516
|
# As array
|
520
517
|
result = @client.execute(@triple_select_2nd_empty)
|
521
|
-
result_sets = result.each(:
|
518
|
+
result_sets = result.each(as: :array)
|
522
519
|
assert_equal 2, result_sets.size
|
523
520
|
assert_equal [[1]], result_sets[0]
|
524
521
|
assert_equal [[3]], result_sets[1]
|
525
|
-
assert_equal [[
|
526
|
-
assert_equal result.each.object_id, result.each.object_id,
|
522
|
+
assert_equal [["rs1"], ["rs3"]], result.fields
|
523
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
527
524
|
end
|
528
525
|
|
529
|
-
it
|
526
|
+
it "must not include empty result sets by default - using 3rd empty buffer" do
|
530
527
|
result = @client.execute(@triple_select_3rd_empty)
|
531
528
|
result_sets = result.each
|
532
529
|
assert_equal 2, result_sets.size
|
533
|
-
assert_equal [{
|
534
|
-
assert_equal [{
|
535
|
-
assert_equal [[
|
536
|
-
assert_equal result.each.object_id, result.each.object_id,
|
530
|
+
assert_equal [{"rs1" => 1}], result_sets[0]
|
531
|
+
assert_equal [{"rs2" => 2}], result_sets[1]
|
532
|
+
assert_equal [["rs1"], ["rs2"]], result.fields
|
533
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
537
534
|
# As array
|
538
535
|
result = @client.execute(@triple_select_3rd_empty)
|
539
|
-
result_sets = result.each(:
|
536
|
+
result_sets = result.each(as: :array)
|
540
537
|
assert_equal 2, result_sets.size
|
541
538
|
assert_equal [[1]], result_sets[0]
|
542
539
|
assert_equal [[2]], result_sets[1]
|
543
|
-
assert_equal [[
|
544
|
-
assert_equal result.each.object_id, result.each.object_id,
|
540
|
+
assert_equal [["rs1"], ["rs2"]], result.fields
|
541
|
+
assert_equal result.each.object_id, result.each.object_id, "same cached rows"
|
545
542
|
end
|
546
|
-
|
547
543
|
end
|
548
|
-
|
549
544
|
end
|
550
545
|
|
551
|
-
|
546
|
+
unless sqlserver_azure?
|
547
|
+
describe "Complex query with multiple results sets but no actual results" do
|
548
|
+
let(:backup_file) { 'C:\\Users\\Public\\tinytdstest.bak' }
|
552
549
|
|
553
|
-
|
550
|
+
after { File.delete(backup_file) if File.exist?(backup_file) }
|
554
551
|
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
@client.execute("BACKUP DATABASE tinytdstest TO DISK = '#{backup_file}'").do
|
552
|
+
it "must not cancel the query until complete" do
|
553
|
+
@client.execute("BACKUP DATABASE tinytdstest TO DISK = '#{backup_file}'").do
|
554
|
+
end
|
559
555
|
end
|
556
|
+
end
|
560
557
|
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
it 'returns fixnum for 1' do
|
566
|
-
value = @client.execute('SELECT 1 AS [fixnum]').each.first['fixnum']
|
558
|
+
describe "when casting to native ruby values" do
|
559
|
+
it "returns fixnum for 1" do
|
560
|
+
value = @client.execute("SELECT 1 AS [fixnum]").each.first["fixnum"]
|
567
561
|
assert_equal 1, value
|
568
562
|
end
|
569
563
|
|
570
|
-
it
|
571
|
-
value = @client.execute(
|
564
|
+
it "returns nil for NULL" do
|
565
|
+
value = @client.execute("SELECT NULL AS [null]").each.first["null"]
|
572
566
|
assert_nil value
|
573
567
|
end
|
574
|
-
|
575
568
|
end
|
576
569
|
|
577
|
-
describe
|
578
|
-
|
579
|
-
describe 'char max' do
|
580
|
-
|
570
|
+
describe "with data type" do
|
571
|
+
describe "char max" do
|
581
572
|
before do
|
582
|
-
@big_text =
|
583
|
-
@old_textsize = @client.execute("SELECT @@TEXTSIZE AS [textsize]").each.first[
|
573
|
+
@big_text = "x" * 2_000_000
|
574
|
+
@old_textsize = @client.execute("SELECT @@TEXTSIZE AS [textsize]").each.first["textsize"].inspect
|
584
575
|
@client.execute("SET TEXTSIZE #{(@big_text.length * 2) + 1}").do
|
585
576
|
end
|
586
577
|
|
587
|
-
it
|
578
|
+
it "must insert and select large varchar_max" do
|
588
579
|
insert_and_select_datatype :varchar_max
|
589
580
|
end
|
590
581
|
|
591
|
-
it
|
582
|
+
it "must insert and select large nvarchar_max" do
|
592
583
|
insert_and_select_datatype :nvarchar_max
|
593
584
|
end
|
594
|
-
|
595
585
|
end
|
596
|
-
|
597
586
|
end
|
598
587
|
|
599
|
-
describe
|
600
|
-
|
601
|
-
it 'copes with nil or empty buffer' do
|
588
|
+
describe "when shit happens" do
|
589
|
+
it "copes with nil or empty buffer" do
|
602
590
|
assert_raises(TypeError) { @client.execute(nil) }
|
603
|
-
assert_equal [], @client.execute(
|
591
|
+
assert_equal [], @client.execute("").each
|
604
592
|
end
|
605
593
|
|
606
|
-
describe
|
607
|
-
let(:messages) {
|
594
|
+
describe "using :message_handler option" do
|
595
|
+
let(:messages) { [] }
|
608
596
|
|
609
597
|
before do
|
610
598
|
close_client
|
611
|
-
@client = new_connection message_handler:
|
599
|
+
@client = new_connection message_handler: proc { |m| messages << m }
|
612
600
|
end
|
613
601
|
|
614
602
|
after do
|
615
603
|
messages.clear
|
616
604
|
end
|
617
605
|
|
618
|
-
it
|
606
|
+
it "has a message handler that responds to call" do
|
619
607
|
assert @client.message_handler.respond_to?(:call)
|
620
608
|
end
|
621
609
|
|
622
|
-
it
|
610
|
+
it "calls the provided message handler when severity is 10 or less" do
|
623
611
|
(1..10).to_a.each do |severity|
|
624
612
|
messages.clear
|
625
613
|
msg = "Test #{severity} severity"
|
626
614
|
state = rand(1..255)
|
627
615
|
@client.execute("RAISERROR(N'#{msg}', #{severity}, #{state})").do
|
628
616
|
m = messages.first
|
629
|
-
assert_equal 1, messages.length,
|
630
|
-
assert_equal msg, m.message,
|
631
|
-
assert_equal severity, m.severity,
|
632
|
-
assert_equal state, m.os_error_number,
|
617
|
+
assert_equal 1, messages.length, "there should be one message after one raiserror"
|
618
|
+
assert_equal msg, m.message, "message text"
|
619
|
+
assert_equal severity, m.severity, "message severity" unless severity == 10 && m.severity.to_i == 0
|
620
|
+
assert_equal state, m.os_error_number, "message state"
|
633
621
|
end
|
634
622
|
end
|
635
623
|
|
636
|
-
it
|
624
|
+
it "calls the provided message handler for `print` messages" do
|
637
625
|
messages.clear
|
638
|
-
msg =
|
626
|
+
msg = "hello"
|
639
627
|
@client.execute("PRINT '#{msg}'").do
|
640
628
|
m = messages.first
|
641
|
-
assert_equal 1, messages.length,
|
642
|
-
assert_equal msg, m.message,
|
629
|
+
assert_equal 1, messages.length, "there should be one message after one print statement"
|
630
|
+
assert_equal msg, m.message, "message text"
|
643
631
|
end
|
644
632
|
|
645
|
-
it
|
633
|
+
it "must raise an error preceded by a `print` message" do
|
646
634
|
messages.clear
|
647
635
|
action = lambda { @client.execute("EXEC tinytds_TestPrintWithError").do }
|
648
636
|
assert_raise_tinytds_error(action) do |e|
|
649
|
-
assert_equal
|
637
|
+
assert_equal "hello", messages.first.message, "message text"
|
650
638
|
|
651
639
|
assert_equal "Error following print", e.message
|
652
640
|
assert_equal 16, e.severity
|
@@ -654,44 +642,44 @@ class ResultTest < TinyTds::TestCase
|
|
654
642
|
end
|
655
643
|
end
|
656
644
|
|
657
|
-
it
|
645
|
+
it "calls the provided message handler for each of a series of `print` messages" do
|
658
646
|
messages.clear
|
659
647
|
@client.execute("EXEC tinytds_TestSeveralPrints").do
|
660
|
-
assert_equal [
|
648
|
+
assert_equal ["hello 1", "hello 2", "hello 3"], messages.map { |e| e.message }, "message list"
|
661
649
|
end
|
662
650
|
|
663
|
-
it
|
664
|
-
@client = new_connection timeout: 1, message_handler:
|
651
|
+
it "should flush info messages before raising error in cases of timeout" do
|
652
|
+
@client = new_connection timeout: 1, message_handler: proc { |m| messages << m }
|
665
653
|
action = lambda { @client.execute("print 'hello'; waitfor delay '00:00:02'").do }
|
666
654
|
messages.clear
|
667
655
|
assert_raise_tinytds_error(action) do |e|
|
668
|
-
assert_match %r{timed out}i, e.message,
|
656
|
+
assert_match %r{timed out}i, e.message, "ignore if non-english test run"
|
669
657
|
assert_equal 6, e.severity
|
670
658
|
assert_equal 20003, e.db_error_number
|
671
|
-
assert_equal
|
659
|
+
assert_equal "hello", messages.first&.message, "message text"
|
672
660
|
end
|
673
661
|
end
|
674
662
|
|
675
|
-
it
|
676
|
-
@client = new_connection timeout: 1, message_handler:
|
663
|
+
it "should print info messages before raising error in cases of timeout" do
|
664
|
+
@client = new_connection timeout: 1, message_handler: proc { |m| messages << m }
|
677
665
|
action = lambda { @client.execute("raiserror('hello', 1, 1) with nowait; waitfor delay '00:00:02'").do }
|
678
666
|
messages.clear
|
679
667
|
assert_raise_tinytds_error(action) do |e|
|
680
|
-
assert_match %r{timed out}i, e.message,
|
668
|
+
assert_match %r{timed out}i, e.message, "ignore if non-english test run"
|
681
669
|
assert_equal 6, e.severity
|
682
670
|
assert_equal 20003, e.db_error_number
|
683
|
-
assert_equal
|
671
|
+
assert_equal "hello", messages.first&.message, "message text"
|
684
672
|
end
|
685
673
|
end
|
686
674
|
end
|
687
675
|
|
688
|
-
it
|
676
|
+
it "must not raise an error when severity is 10 or less" do
|
689
677
|
(1..10).to_a.each do |severity|
|
690
678
|
@client.execute("RAISERROR(N'Test #{severity} severity', #{severity}, 1)").do
|
691
679
|
end
|
692
680
|
end
|
693
681
|
|
694
|
-
it
|
682
|
+
it "raises an error when severity is greater than 10" do
|
695
683
|
action = lambda { @client.execute("RAISERROR(N'Test 11 severity', 11, 1)").do }
|
696
684
|
assert_raise_tinytds_error(action) do |e|
|
697
685
|
assert_equal "Test 11 severity", e.message
|
@@ -706,7 +694,7 @@ class ResultTest < TinyTds::TestCase
|
|
706
694
|
|
707
695
|
def assert_followup_query
|
708
696
|
result = @client.execute(@query1)
|
709
|
-
assert_equal 1, result.each.first[
|
697
|
+
assert_equal 1, result.each.first["one"]
|
710
698
|
end
|
711
699
|
|
712
700
|
def insert_and_select_datatype(datatype)
|
@@ -717,5 +705,4 @@ class ResultTest < TinyTds::TestCase
|
|
717
705
|
flunk "Large #{datatype} data with a length of #{@big_text.length} did not match found text with length of #{found_text.length}" unless @big_text == found_text
|
718
706
|
end
|
719
707
|
end
|
720
|
-
|
721
708
|
end
|