mysql2 0.4.6 → 0.4.10

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.
@@ -3,11 +3,9 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Mysql2::Error do
6
- let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) }
7
-
8
6
  let(:error) do
9
7
  begin
10
- client.query("HAHAHA")
8
+ @client.query("HAHAHA")
11
9
  rescue Mysql2::Error => e
12
10
  error = e
13
11
  end
@@ -28,7 +26,7 @@ RSpec.describe Mysql2::Error do
28
26
  let(:valid_utf8) { '造字' }
29
27
  let(:error) do
30
28
  begin
31
- client.query(valid_utf8)
29
+ @client.query(valid_utf8)
32
30
  rescue Mysql2::Error => e
33
31
  e
34
32
  end
@@ -37,7 +35,7 @@ RSpec.describe Mysql2::Error do
37
35
  let(:invalid_utf8) { ["e5c67d1f"].pack('H*').force_encoding(Encoding::UTF_8) }
38
36
  let(:bad_err) do
39
37
  begin
40
- client.query(invalid_utf8)
38
+ @client.query(invalid_utf8)
41
39
  rescue Mysql2::Error => e
42
40
  e
43
41
  end
@@ -153,18 +153,16 @@ RSpec.describe Mysql2::Result do
153
153
  end
154
154
 
155
155
  it "should raise an exception if streaming ended due to a timeout" do
156
- # Create an extra client instance, since we're going to time it out
157
- client = Mysql2::Client.new DatabaseCredentials['root']
158
- client.query "CREATE TEMPORARY TABLE streamingTest (val BINARY(255)) ENGINE=MEMORY"
156
+ @client.query "CREATE TEMPORARY TABLE streamingTest (val BINARY(255)) ENGINE=MEMORY"
159
157
 
160
158
  # Insert enough records to force the result set into multiple reads
161
159
  # (the BINARY type is used simply because it forces full width results)
162
160
  10000.times do |i|
163
- client.query "INSERT INTO streamingTest (val) VALUES ('Foo #{i}')"
161
+ @client.query "INSERT INTO streamingTest (val) VALUES ('Foo #{i}')"
164
162
  end
165
163
 
166
- client.query "SET net_write_timeout = 1"
167
- res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
164
+ @client.query "SET net_write_timeout = 1"
165
+ res = @client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
168
166
 
169
167
  expect {
170
168
  res.each_with_index do |_, i|
@@ -210,36 +208,43 @@ RSpec.describe Mysql2::Result do
210
208
  expect(@test_result['tiny_int_test']).to eql(1)
211
209
  end
212
210
 
213
- it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
214
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
215
- id1 = @client.last_id
216
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
217
- id2 = @client.last_id
218
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
219
- id3 = @client.last_id
211
+ context "cast booleans for TINYINT if :cast_booleans is enabled" do
212
+ # rubocop:disable Style/Semicolon
213
+ let(:id1) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)'; @client.last_id }
214
+ let(:id2) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)'; @client.last_id }
215
+ let(:id3) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'; @client.last_id }
216
+ # rubocop:enable Style/Semicolon
220
217
 
221
- result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
222
- result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
223
- result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
224
- expect(result1.first['bool_cast_test']).to be true
225
- expect(result2.first['bool_cast_test']).to be false
226
- expect(result3.first['bool_cast_test']).to be true
218
+ after do
219
+ @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
220
+ end
227
221
 
228
- @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
222
+ it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
223
+ result1 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id1} LIMIT 1", :cast_booleans => true
224
+ result2 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id2} LIMIT 1", :cast_booleans => true
225
+ result3 = @client.query "SELECT bool_cast_test FROM mysql2_test WHERE id = #{id3} LIMIT 1", :cast_booleans => true
226
+ expect(result1.first['bool_cast_test']).to be true
227
+ expect(result2.first['bool_cast_test']).to be false
228
+ expect(result3.first['bool_cast_test']).to be true
229
+ end
229
230
  end
230
231
 
231
- it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
232
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
233
- id1 = @client.last_id
234
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
235
- id2 = @client.last_id
232
+ context "cast booleans for BIT(1) if :cast_booleans is enabled" do
233
+ # rubocop:disable Style/Semicolon
234
+ let(:id1) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'; @client.last_id }
235
+ let(:id2) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'; @client.last_id }
236
+ # rubocop:enable Style/Semicolon
236
237
 
237
- result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
238
- result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
239
- expect(result1.first['single_bit_test']).to be true
240
- expect(result2.first['single_bit_test']).to be false
238
+ after do
239
+ @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
240
+ end
241
241
 
242
- @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
242
+ it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
243
+ result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
244
+ result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
245
+ expect(result1.first['single_bit_test']).to be true
246
+ expect(result2.first['single_bit_test']).to be false
247
+ end
243
248
  end
244
249
 
245
250
  it "should return Fixnum for a SMALLINT value" do
@@ -287,6 +292,30 @@ RSpec.describe Mysql2::Result do
287
292
  expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
288
293
  end
289
294
 
295
+ it "should return Time values with microseconds" do
296
+ now = Time.now
297
+ if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506
298
+ result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
299
+ expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
300
+ else
301
+ result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
302
+ # microseconds is 6 digits after the decimal, but only test on 5 significant figures
303
+ expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
304
+ end
305
+ end
306
+
307
+ it "should return DateTime values with microseconds" do
308
+ now = DateTime.now
309
+ if RUBY_VERSION =~ /1.8/ || @client.server_info[:id] / 100 < 506
310
+ result = @client.query("SELECT CAST('#{now.strftime('%F %T %z')}' AS DATETIME) AS a")
311
+ expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
312
+ else
313
+ result = @client.query("SELECT CAST('#{now.strftime('%F %T.%6N %z')}' AS DATETIME(6)) AS a")
314
+ # microseconds is 6 digits after the decimal, but only test on 5 significant figures
315
+ expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
316
+ end
317
+ end
318
+
290
319
  if 1.size == 4 # 32bit
291
320
  klass = if RUBY_VERSION =~ /1.8/
292
321
  DateTime
@@ -367,10 +396,9 @@ RSpec.describe Mysql2::Result do
367
396
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
368
397
  expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
369
398
 
370
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
399
+ client2 = new_client(:encoding => 'ascii')
371
400
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
372
401
  expect(result['enum_test'].encoding).to eql(Encoding::ASCII)
373
- client2.close
374
402
  end
375
403
  end
376
404
 
@@ -400,10 +428,9 @@ RSpec.describe Mysql2::Result do
400
428
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
401
429
  expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
402
430
 
403
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
431
+ client2 = new_client(:encoding => 'ascii')
404
432
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
405
433
  expect(result['set_test'].encoding).to eql(Encoding::ASCII)
406
- client2.close
407
434
  end
408
435
  end
409
436
 
@@ -494,10 +521,9 @@ RSpec.describe Mysql2::Result do
494
521
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
495
522
  expect(result[field].encoding).to eql(Encoding::UTF_8)
496
523
 
497
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
524
+ client2 = new_client(:encoding => 'ascii')
498
525
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
499
526
  expect(result[field].encoding).to eql(Encoding::ASCII)
500
- client2.close
501
527
  end
502
528
  end
503
529
 
@@ -3,7 +3,7 @@ require './spec/spec_helper.rb'
3
3
 
4
4
  RSpec.describe Mysql2::Statement do
5
5
  before :each do
6
- @client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
6
+ @client = new_client(:encoding => "utf8")
7
7
  end
8
8
 
9
9
  def stmt_count
@@ -61,6 +61,12 @@ RSpec.describe Mysql2::Statement do
61
61
  expect(rows).to eq([{ "1" => 1 }])
62
62
  end
63
63
 
64
+ it "should handle booleans" do
65
+ stmt = @client.prepare('SELECT ? AS `true`, ? AS `false`')
66
+ result = stmt.execute(true, false)
67
+ expect(result.to_a).to eq(['true' => 1, 'false' => 0])
68
+ end
69
+
64
70
  it "should handle bignum but in int64_t" do
65
71
  stmt = @client.prepare('SELECT ? AS max, ? AS min')
66
72
  int64_max = (1 << 63) - 1
@@ -144,7 +150,8 @@ RSpec.describe Mysql2::Statement do
144
150
  if RUBY_VERSION =~ /1.8/
145
151
  expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
146
152
  else
147
- expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
153
+ # microseconds is six digits after the decimal, but only test on 5 significant figures
154
+ expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
148
155
  end
149
156
  end
150
157
 
@@ -155,7 +162,8 @@ RSpec.describe Mysql2::Statement do
155
162
  if RUBY_VERSION =~ /1.8/
156
163
  expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
157
164
  else
158
- expect(result.first['a'].strftime('%F %T.%6N %z')).to eql(now.strftime('%F %T.%6N %z'))
165
+ # microseconds is six digits after the decimal, but only test on 5 significant figures
166
+ expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
159
167
  end
160
168
  end
161
169
 
@@ -326,18 +334,19 @@ RSpec.describe Mysql2::Statement do
326
334
  end
327
335
 
328
336
  context "#fields" do
329
- before(:each) do
330
- @client.query "USE test"
331
- @test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute
332
- end
333
-
334
337
  it "method should exist" do
335
- expect(@test_result).to respond_to(:fields)
338
+ stmt = @client.prepare("SELECT 1")
339
+ expect(stmt).to respond_to(:fields)
336
340
  end
337
341
 
338
342
  it "should return an array of field names in proper order" do
339
- result = @client.prepare("SELECT 'a', 'b', 'c'").execute
340
- expect(result.fields).to eql(%w(a b c))
343
+ stmt = @client.prepare("SELECT 'a', 'b', 'c'")
344
+ expect(stmt.fields).to eql(%w(a b c))
345
+ end
346
+
347
+ it "should return nil for statement with no result fields" do
348
+ stmt = @client.prepare("INSERT INTO mysql2_test () VALUES ()")
349
+ expect(stmt.fields).to eql(nil)
341
350
  end
342
351
  end
343
352
 
@@ -367,36 +376,47 @@ RSpec.describe Mysql2::Statement do
367
376
  expect(@test_result['tiny_int_test']).to eql(1)
368
377
  end
369
378
 
370
- it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
371
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
372
- id1 = @client.last_id
373
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
374
- id2 = @client.last_id
375
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
376
- id3 = @client.last_id
379
+ context "cast booleans for TINYINT if :cast_booleans is enabled" do
380
+ # rubocop:disable Style/Semicolon
381
+ let(:client) { new_client(:cast_booleans => true) }
382
+ let(:id1) { client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)'; client.last_id }
383
+ let(:id2) { client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)'; client.last_id }
384
+ let(:id3) { client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'; client.last_id }
385
+ # rubocop:enable Style/Semicolon
377
386
 
378
- result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
379
- result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
380
- result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
381
- expect(result1.first['bool_cast_test']).to be true
382
- expect(result2.first['bool_cast_test']).to be false
383
- expect(result3.first['bool_cast_test']).to be true
387
+ after do
388
+ client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
389
+ end
384
390
 
385
- @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
391
+ it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
392
+ query = client.prepare 'SELECT bool_cast_test FROM mysql2_test WHERE id = ?'
393
+ result1 = query.execute id1
394
+ result2 = query.execute id2
395
+ result3 = query.execute id3
396
+ expect(result1.first['bool_cast_test']).to be true
397
+ expect(result2.first['bool_cast_test']).to be false
398
+ expect(result3.first['bool_cast_test']).to be true
399
+ end
386
400
  end
387
401
 
388
- it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
389
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
390
- id1 = @client.last_id
391
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
392
- id2 = @client.last_id
402
+ context "cast booleans for BIT(1) if :cast_booleans is enabled" do
403
+ # rubocop:disable Style/Semicolon
404
+ let(:client) { new_client(:cast_booleans => true) }
405
+ let(:id1) { client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'; client.last_id }
406
+ let(:id2) { client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'; client.last_id }
407
+ # rubocop:enable Style/Semicolon
393
408
 
394
- result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
395
- result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
396
- expect(result1.first['single_bit_test']).to be true
397
- expect(result2.first['single_bit_test']).to be false
409
+ after do
410
+ client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
411
+ end
398
412
 
399
- @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
413
+ it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
414
+ query = client.prepare 'SELECT single_bit_test FROM mysql2_test WHERE id = ?'
415
+ result1 = query.execute id1
416
+ result2 = query.execute id2
417
+ expect(result1.first['single_bit_test']).to be true
418
+ expect(result2.first['single_bit_test']).to be false
419
+ end
400
420
  end
401
421
 
402
422
  it "should return Fixnum for a SMALLINT value" do
@@ -453,39 +473,39 @@ RSpec.describe Mysql2::Statement do
453
473
 
454
474
  it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
455
475
  # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
456
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
476
+ r = @client.prepare("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test").execute
457
477
  expect(r.first['test']).to be_an_instance_of(klass)
458
478
  end
459
479
 
460
480
  it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
461
481
  # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
462
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
482
+ r = @client.prepare("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test").execute
463
483
  expect(r.first['test']).to be_an_instance_of(klass)
464
484
  end
465
485
  elsif 1.size == 8 # 64bit
466
486
  if RUBY_VERSION =~ /1.8/
467
487
  it "should return Time when timestamp is > 0138-12-31 11:59:59" do
468
- r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
488
+ r = @client.prepare("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test").execute
469
489
  expect(r.first['test']).to be_an_instance_of(Time)
470
490
  end
471
491
 
472
492
  it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
473
- r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
493
+ r = @client.prepare("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test").execute
474
494
  expect(r.first['test']).to be_an_instance_of(DateTime)
475
495
  end
476
496
 
477
497
  it "should return Time when timestamp is > 2038-01-19T03:14:07" do
478
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
498
+ r = @client.prepare("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test").execute
479
499
  expect(r.first['test']).to be_an_instance_of(Time)
480
500
  end
481
501
  else
482
502
  it "should return Time when timestamp is < 1901-12-13 20:45:52" do
483
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
503
+ r = @client.prepare("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test").execute
484
504
  expect(r.first['test']).to be_an_instance_of(Time)
485
505
  end
486
506
 
487
507
  it "should return Time when timestamp is > 2038-01-19T03:14:07" do
488
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
508
+ r = @client.prepare("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test").execute
489
509
  expect(r.first['test']).to be_an_instance_of(Time)
490
510
  end
491
511
  end
@@ -524,10 +544,9 @@ RSpec.describe Mysql2::Statement do
524
544
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
525
545
  expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
526
546
 
527
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
547
+ client2 = new_client(:encoding => 'ascii')
528
548
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
529
549
  expect(result['enum_test'].encoding).to eql(Encoding::US_ASCII)
530
- client2.close
531
550
  end
532
551
  end
533
552
 
@@ -557,10 +576,9 @@ RSpec.describe Mysql2::Statement do
557
576
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
558
577
  expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
559
578
 
560
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
579
+ client2 = new_client(:encoding => 'ascii')
561
580
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
562
581
  expect(result['set_test'].encoding).to eql(Encoding::US_ASCII)
563
- client2.close
564
582
  end
565
583
  end
566
584
 
@@ -651,10 +669,9 @@ RSpec.describe Mysql2::Statement do
651
669
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
652
670
  expect(result[field].encoding).to eql(Encoding::UTF_8)
653
671
 
654
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
672
+ client2 = new_client(:encoding => 'ascii')
655
673
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
656
674
  expect(result[field].encoding).to eql(Encoding::US_ASCII)
657
- client2.close
658
675
  end
659
676
  end
660
677
 
@@ -23,72 +23,86 @@ RSpec.configure do |config|
23
23
  $VERBOSE = old_verbose
24
24
  end
25
25
 
26
+ def new_client(option_overrides = {})
27
+ client = Mysql2::Client.new(DatabaseCredentials['root'].merge(option_overrides))
28
+ @clients ||= []
29
+ @clients << client
30
+ return client unless block_given?
31
+ begin
32
+ yield client
33
+ ensure
34
+ client.close
35
+ @clients.delete(client)
36
+ end
37
+ end
38
+
26
39
  config.before :each do
27
- @client = Mysql2::Client.new DatabaseCredentials['root']
40
+ @client = new_client
28
41
  end
29
42
 
30
43
  config.after :each do
31
- @client.close
44
+ @clients.each(&:close)
32
45
  end
33
46
 
34
47
  config.before(:all) do
35
- client = Mysql2::Client.new DatabaseCredentials['root']
36
- client.query %[
37
- CREATE TABLE IF NOT EXISTS mysql2_test (
38
- id MEDIUMINT NOT NULL AUTO_INCREMENT,
39
- null_test VARCHAR(10),
40
- bit_test BIT(64),
41
- single_bit_test BIT(1),
42
- tiny_int_test TINYINT,
43
- bool_cast_test TINYINT(1),
44
- small_int_test SMALLINT,
45
- medium_int_test MEDIUMINT,
46
- int_test INT,
47
- big_int_test BIGINT,
48
- float_test FLOAT(10,3),
49
- float_zero_test FLOAT(10,3),
50
- double_test DOUBLE(10,3),
51
- decimal_test DECIMAL(10,3),
52
- decimal_zero_test DECIMAL(10,3),
53
- date_test DATE,
54
- date_time_test DATETIME,
55
- timestamp_test TIMESTAMP,
56
- time_test TIME,
57
- year_test YEAR(4),
58
- char_test CHAR(10),
59
- varchar_test VARCHAR(10),
60
- binary_test BINARY(10),
61
- varbinary_test VARBINARY(10),
62
- tiny_blob_test TINYBLOB,
63
- tiny_text_test TINYTEXT,
64
- blob_test BLOB,
65
- text_test TEXT,
66
- medium_blob_test MEDIUMBLOB,
67
- medium_text_test MEDIUMTEXT,
68
- long_blob_test LONGBLOB,
69
- long_text_test LONGTEXT,
70
- enum_test ENUM('val1', 'val2'),
71
- set_test SET('val1', 'val2'),
72
- PRIMARY KEY (id)
73
- )
74
- ]
75
- client.query "DELETE FROM mysql2_test;"
76
- client.query %[
77
- INSERT INTO mysql2_test (
78
- null_test, bit_test, single_bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
79
- float_test, float_zero_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
80
- year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
81
- tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
82
- long_blob_test, long_text_test, enum_test, set_test
83
- )
48
+ new_client do |client|
49
+ client.query %[
50
+ CREATE TABLE IF NOT EXISTS mysql2_test (
51
+ id MEDIUMINT NOT NULL AUTO_INCREMENT,
52
+ null_test VARCHAR(10),
53
+ bit_test BIT(64),
54
+ single_bit_test BIT(1),
55
+ tiny_int_test TINYINT,
56
+ bool_cast_test TINYINT(1),
57
+ small_int_test SMALLINT,
58
+ medium_int_test MEDIUMINT,
59
+ int_test INT,
60
+ big_int_test BIGINT,
61
+ float_test FLOAT(10,3),
62
+ float_zero_test FLOAT(10,3),
63
+ double_test DOUBLE(10,3),
64
+ decimal_test DECIMAL(10,3),
65
+ decimal_zero_test DECIMAL(10,3),
66
+ date_test DATE,
67
+ date_time_test DATETIME,
68
+ timestamp_test TIMESTAMP,
69
+ time_test TIME,
70
+ year_test YEAR(4),
71
+ char_test CHAR(10),
72
+ varchar_test VARCHAR(10),
73
+ binary_test BINARY(10),
74
+ varbinary_test VARBINARY(10),
75
+ tiny_blob_test TINYBLOB,
76
+ tiny_text_test TINYTEXT,
77
+ blob_test BLOB,
78
+ text_test TEXT,
79
+ medium_blob_test MEDIUMBLOB,
80
+ medium_text_test MEDIUMTEXT,
81
+ long_blob_test LONGBLOB,
82
+ long_text_test LONGTEXT,
83
+ enum_test ENUM('val1', 'val2'),
84
+ set_test SET('val1', 'val2'),
85
+ PRIMARY KEY (id)
86
+ )
87
+ ]
88
+ client.query "DELETE FROM mysql2_test;"
89
+ client.query %[
90
+ INSERT INTO mysql2_test (
91
+ null_test, bit_test, single_bit_test, tiny_int_test, bool_cast_test, small_int_test, medium_int_test, int_test, big_int_test,
92
+ float_test, float_zero_test, double_test, decimal_test, decimal_zero_test, date_test, date_time_test, timestamp_test, time_test,
93
+ year_test, char_test, varchar_test, binary_test, varbinary_test, tiny_blob_test,
94
+ tiny_text_test, blob_test, text_test, medium_blob_test, medium_text_test,
95
+ long_blob_test, long_text_test, enum_test, set_test
96
+ )
84
97
 
85
- VALUES (
86
- NULL, b'101', b'1', 1, 1, 10, 10, 10, 10,
87
- 10.3, 0, 10.3, 10.3, 0, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
88
- 2009, "test", "test", "test", "test", "test",
89
- "test", "test", "test", "test", "test",
90
- "test", "test", 'val1', 'val1,val2'
91
- )
92
- ]
98
+ VALUES (
99
+ NULL, b'101', b'1', 1, 1, 10, 10, 10, 10,
100
+ 10.3, 0, 10.3, 10.3, 0, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00',
101
+ 2009, "test", "test", "test", "test", "test",
102
+ "test", "test", "test", "test", "test",
103
+ "test", "test", 'val1', 'val1,val2'
104
+ )
105
+ ]
106
+ end
93
107
  end
94
108
  end