mysql2 0.2.21 → 0.2.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,35 +6,6 @@ describe Mysql2::Result do
6
6
  @result = @client.query "SELECT 1"
7
7
  end
8
8
 
9
- it "should maintain a count while streaming" do
10
- result = @client.query('SELECT 1')
11
-
12
- result.count.should eql(1)
13
- result.each.to_a
14
- result.count.should eql(1)
15
- end
16
-
17
- it "should set the actual count of rows after streaming" do
18
- @client.query "USE test"
19
- result = @client.query("SELECT * FROM mysql2_test", :stream => true, :cache_rows => false)
20
- result.count.should eql(0)
21
- result.each {|r| }
22
- result.count.should eql(1)
23
- end
24
-
25
- it "should not yield nil at the end of streaming" do
26
- result = @client.query('SELECT * FROM mysql2_test', :stream => true)
27
- result.each { |r| r.should_not be_nil}
28
- end
29
-
30
- it "#count should be zero for rows after streaming when there were no results " do
31
- @client.query "USE test"
32
- result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
33
- result.count.should eql(0)
34
- result.each.to_a
35
- result.count.should eql(0)
36
- end
37
-
38
9
  it "should have included Enumerable" do
39
10
  Mysql2::Result.ancestors.include?(Enumerable).should be_true
40
11
  end
@@ -121,7 +92,6 @@ describe Mysql2::Result do
121
92
 
122
93
  context "#fields" do
123
94
  before(:each) do
124
- @client.query "USE test"
125
95
  @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1")
126
96
  end
127
97
 
@@ -135,9 +105,59 @@ describe Mysql2::Result do
135
105
  end
136
106
  end
137
107
 
108
+ context "streaming" do
109
+ it "should maintain a count while streaming" do
110
+ result = @client.query('SELECT 1')
111
+
112
+ result.count.should eql(1)
113
+ result.each.to_a
114
+ result.count.should eql(1)
115
+ end
116
+
117
+ it "should set the actual count of rows after streaming" do
118
+ result = @client.query("SELECT * FROM mysql2_test", :stream => true, :cache_rows => false)
119
+ result.count.should eql(0)
120
+ result.each {|r| }
121
+ result.count.should eql(1)
122
+ end
123
+
124
+ it "should not yield nil at the end of streaming" do
125
+ result = @client.query('SELECT * FROM mysql2_test', :stream => true, :cache_rows => false)
126
+ result.each { |r| r.should_not be_nil}
127
+ end
128
+
129
+ it "#count should be zero for rows after streaming when there were no results" do
130
+ result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
131
+ result.count.should eql(0)
132
+ result.each.to_a
133
+ result.count.should eql(0)
134
+ end
135
+
136
+ it "should raise an exception if streaming ended due to a timeout" do
137
+ # Create an extra client instance, since we're going to time it out
138
+ client = Mysql2::Client.new DatabaseCredentials['root']
139
+ client.query "CREATE TEMPORARY TABLE streamingTest (val BINARY(255))"
140
+
141
+ # Insert enough records to force the result set into multiple reads
142
+ # (the BINARY type is used simply because it forces full width results)
143
+ 10000.times do |i|
144
+ client.query "INSERT INTO streamingTest (val) VALUES ('Foo #{i}')"
145
+ end
146
+
147
+ client.query "SET net_write_timeout = 1"
148
+ res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
149
+
150
+ lambda {
151
+ res.each_with_index do |row, i|
152
+ # Exhaust the first result packet then trigger a timeout
153
+ sleep 2 if i > 0 && i % 1000 == 0
154
+ end
155
+ }.should raise_error(Mysql2::Error, /Lost connection/)
156
+ end
157
+ end
158
+
138
159
  context "row data type mapping" do
139
160
  before(:each) do
140
- @client.query "USE test"
141
161
  @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
142
162
  end
143
163
 
@@ -318,24 +338,27 @@ describe Mysql2::Result do
318
338
  if defined? Encoding
319
339
  context "string encoding for ENUM values" do
320
340
  it "should default to the connection's encoding if Encoding.default_internal is nil" do
321
- Encoding.default_internal = nil
322
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
323
- result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
324
-
325
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
326
- client2.query "USE test"
327
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
328
- result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
329
- client2.close
341
+ with_internal_encoding nil do
342
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
343
+ result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
344
+
345
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
346
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
347
+ result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
348
+ client2.close
349
+ end
330
350
  end
331
351
 
332
352
  it "should use Encoding.default_internal" do
333
- Encoding.default_internal = Encoding.find('utf-8')
334
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
335
- result['enum_test'].encoding.should eql(Encoding.default_internal)
336
- Encoding.default_internal = Encoding.find('us-ascii')
337
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
338
- result['enum_test'].encoding.should eql(Encoding.default_internal)
353
+ with_internal_encoding 'utf-8' do
354
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
355
+ result['enum_test'].encoding.should eql(Encoding.default_internal)
356
+ end
357
+
358
+ with_internal_encoding 'us-ascii' do
359
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
360
+ result['enum_test'].encoding.should eql(Encoding.default_internal)
361
+ end
339
362
  end
340
363
  end
341
364
  end
@@ -348,24 +371,27 @@ describe Mysql2::Result do
348
371
  if defined? Encoding
349
372
  context "string encoding for SET values" do
350
373
  it "should default to the connection's encoding if Encoding.default_internal is nil" do
351
- Encoding.default_internal = nil
352
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
353
- result['set_test'].encoding.should eql(Encoding.find('utf-8'))
354
-
355
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
356
- client2.query "USE test"
357
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
358
- result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
359
- client2.close
374
+ with_internal_encoding nil do
375
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
376
+ result['set_test'].encoding.should eql(Encoding.find('utf-8'))
377
+
378
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
379
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
380
+ result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
381
+ client2.close
382
+ end
360
383
  end
361
384
 
362
385
  it "should use Encoding.default_internal" do
363
- Encoding.default_internal = Encoding.find('utf-8')
364
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
365
- result['set_test'].encoding.should eql(Encoding.default_internal)
366
- Encoding.default_internal = Encoding.find('us-ascii')
367
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
368
- result['set_test'].encoding.should eql(Encoding.default_internal)
386
+ with_internal_encoding 'utf-8' do
387
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
388
+ result['set_test'].encoding.should eql(Encoding.default_internal)
389
+ end
390
+
391
+ with_internal_encoding 'us-ascii' do
392
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
393
+ result['set_test'].encoding.should eql(Encoding.default_internal)
394
+ end
369
395
  end
370
396
  end
371
397
  end
@@ -378,18 +404,22 @@ describe Mysql2::Result do
378
404
  if defined? Encoding
379
405
  context "string encoding for BINARY values" do
380
406
  it "should default to binary if Encoding.default_internal is nil" do
381
- Encoding.default_internal = nil
382
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
383
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
407
+ with_internal_encoding nil do
408
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
409
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
410
+ end
384
411
  end
385
412
 
386
413
  it "should not use Encoding.default_internal" do
387
- Encoding.default_internal = Encoding.find('utf-8')
388
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
389
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
390
- Encoding.default_internal = Encoding.find('us-ascii')
391
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
392
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
414
+ with_internal_encoding 'utf-8' do
415
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
416
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
417
+ end
418
+
419
+ with_internal_encoding 'us-ascii' do
420
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
421
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
422
+ end
393
423
  end
394
424
  end
395
425
  end
@@ -416,39 +446,46 @@ describe Mysql2::Result do
416
446
  context "string encoding for #{type} values" do
417
447
  if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
418
448
  it "should default to binary if Encoding.default_internal is nil" do
419
- Encoding.default_internal = nil
420
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
421
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
449
+ with_internal_encoding nil do
450
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
451
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
452
+ end
422
453
  end
423
454
 
424
455
  it "should not use Encoding.default_internal" do
425
- Encoding.default_internal = Encoding.find('utf-8')
426
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
427
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
428
- Encoding.default_internal = Encoding.find('us-ascii')
429
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
430
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
456
+ with_internal_encoding 'utf-8' do
457
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
458
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
459
+ end
460
+
461
+ with_internal_encoding 'us-ascii' do
462
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
463
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
464
+ end
431
465
  end
432
466
  else
433
467
  it "should default to utf-8 if Encoding.default_internal is nil" do
434
- Encoding.default_internal = nil
435
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
436
- result[field].encoding.should eql(Encoding.find('utf-8'))
437
-
438
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
439
- client2.query "USE test"
440
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
441
- result[field].encoding.should eql(Encoding.find('us-ascii'))
442
- client2.close
468
+ with_internal_encoding nil do
469
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
470
+ result[field].encoding.should eql(Encoding.find('utf-8'))
471
+
472
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
473
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
474
+ result[field].encoding.should eql(Encoding.find('us-ascii'))
475
+ client2.close
476
+ end
443
477
  end
444
478
 
445
479
  it "should use Encoding.default_internal" do
446
- Encoding.default_internal = Encoding.find('utf-8')
447
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
448
- result[field].encoding.should eql(Encoding.default_internal)
449
- Encoding.default_internal = Encoding.find('us-ascii')
450
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
451
- result[field].encoding.should eql(Encoding.default_internal)
480
+ with_internal_encoding 'utf-8' do
481
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
482
+ result[field].encoding.should eql(Encoding.default_internal)
483
+ end
484
+
485
+ with_internal_encoding 'us-ascii' do
486
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
487
+ result[field].encoding.should eql(Encoding.default_internal)
488
+ end
452
489
  end
453
490
  end
454
491
  end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,15 @@ require 'yaml'
7
7
  DatabaseCredentials = YAML.load_file('spec/configuration.yml')
8
8
 
9
9
  RSpec.configure do |config|
10
+ def with_internal_encoding(encoding)
11
+ old_enc = Encoding.default_internal
12
+ Encoding.default_internal = encoding
13
+
14
+ yield
15
+ ensure
16
+ Encoding.default_internal = old_enc
17
+ end
18
+
10
19
  config.before :each do
11
20
  @client = Mysql2::Client.new DatabaseCredentials['root']
12
21
  end
data/spec/test_data ADDED
@@ -0,0 +1 @@
1
+ \N Hello World
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.21
4
+ version: 0.2.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Lopez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-07 00:00:00.000000000 Z
11
+ date: 2014-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -78,6 +78,8 @@ files:
78
78
  - ext/mysql2/client.c
79
79
  - ext/mysql2/client.h
80
80
  - ext/mysql2/extconf.rb
81
+ - ext/mysql2/infile.c
82
+ - ext/mysql2/infile.h
81
83
  - ext/mysql2/mysql2_ext.c
82
84
  - ext/mysql2/mysql2_ext.h
83
85
  - ext/mysql2/mysql_enc_name_to_ruby.h
@@ -106,6 +108,7 @@ files:
106
108
  - spec/mysql2/result_spec.rb
107
109
  - spec/rcov.opts
108
110
  - spec/spec_helper.rb
111
+ - spec/test_data
109
112
  homepage: http://github.com/brianmario/mysql2
110
113
  licenses:
111
114
  - MIT
@@ -142,3 +145,4 @@ test_files:
142
145
  - spec/mysql2/result_spec.rb
143
146
  - spec/rcov.opts
144
147
  - spec/spec_helper.rb
148
+ - spec/test_data