mysql2 0.4.4 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,14 +3,16 @@ 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
+ end
8
+
9
+ def stmt_count
10
+ @client.query("SHOW STATUS LIKE 'Prepared_stmt_count'").first['Value'].to_i
7
11
  end
8
12
 
9
13
  it "should create a statement" do
10
14
  statement = nil
11
- expect { statement = @client.prepare 'SELECT 1' }.to change {
12
- @client.query("SHOW STATUS LIKE 'Prepared_stmt_count'").first['Value'].to_i
13
- }.by(1)
15
+ expect { statement = @client.prepare 'SELECT 1' }.to change(&method(:stmt_count)).by(1)
14
16
  expect(statement).to be_an_instance_of(Mysql2::Statement)
15
17
  end
16
18
 
@@ -59,6 +61,32 @@ RSpec.describe Mysql2::Statement do
59
61
  expect(rows).to eq([{ "1" => 1 }])
60
62
  end
61
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
+
70
+ it "should handle bignum but in int64_t" do
71
+ stmt = @client.prepare('SELECT ? AS max, ? AS min')
72
+ int64_max = (1 << 63) - 1
73
+ int64_min = -(1 << 63)
74
+ result = stmt.execute(int64_max, int64_min)
75
+ expect(result.to_a).to eq(['max' => int64_max, 'min' => int64_min])
76
+ end
77
+
78
+ it "should handle bignum but beyond int64_t" do
79
+ stmt = @client.prepare('SELECT ? AS max1, ? AS max2, ? AS max3, ? AS min1, ? AS min2, ? AS min3')
80
+ int64_max1 = (1 << 63)
81
+ int64_max2 = (1 << 64) - 1
82
+ int64_max3 = 1 << 64
83
+ int64_min1 = -(1 << 63) - 1
84
+ int64_min2 = -(1 << 64) + 1
85
+ int64_min3 = -0xC000000000000000
86
+ result = stmt.execute(int64_max1, int64_max2, int64_max3, int64_min1, int64_min2, int64_min3)
87
+ expect(result.to_a).to eq(['max1' => int64_max1, 'max2' => int64_max2, 'max3' => int64_max3, 'min1' => int64_min1, 'min2' => int64_min2, 'min3' => int64_min3])
88
+ end
89
+
62
90
  it "should keep its result after other query" do
63
91
  @client.query 'USE test'
64
92
  @client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
@@ -108,6 +136,37 @@ RSpec.describe Mysql2::Statement do
108
136
  expect(result.first.first[1]).to be_an_instance_of(Time)
109
137
  end
110
138
 
139
+ it "should prepare Date values" do
140
+ now = Date.today
141
+ statement = @client.prepare('SELECT ? AS a')
142
+ result = statement.execute(now)
143
+ expect(result.first['a'].to_s).to eql(now.strftime('%F'))
144
+ end
145
+
146
+ it "should prepare Time values with microseconds" do
147
+ now = Time.now
148
+ statement = @client.prepare('SELECT ? AS a')
149
+ result = statement.execute(now)
150
+ if RUBY_VERSION =~ /1.8/
151
+ expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
152
+ else
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'))
155
+ end
156
+ end
157
+
158
+ it "should prepare DateTime values with microseconds" do
159
+ now = DateTime.now
160
+ statement = @client.prepare('SELECT ? AS a')
161
+ result = statement.execute(now)
162
+ if RUBY_VERSION =~ /1.8/
163
+ expect(result.first['a'].strftime('%F %T %z')).to eql(now.strftime('%F %T %z'))
164
+ else
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'))
167
+ end
168
+ end
169
+
111
170
  it "should tell us about the fields" do
112
171
  statement = @client.prepare 'SELECT 1 as foo, 2'
113
172
  statement.execute
@@ -117,6 +176,13 @@ RSpec.describe Mysql2::Statement do
117
176
  expect(list[1]).to eq('2')
118
177
  end
119
178
 
179
+ it "should handle as a decimal binding a BigDecimal" do
180
+ stmt = @client.prepare('SELECT ? AS decimal_test')
181
+ test_result = stmt.execute(BigDecimal.new("123.45")).first
182
+ expect(test_result['decimal_test']).to be_an_instance_of(BigDecimal)
183
+ expect(test_result['decimal_test']).to eql(123.45)
184
+ end
185
+
120
186
  it "should update a DECIMAL value passing a BigDecimal" do
121
187
  @client.query 'USE test'
122
188
  @client.query 'DROP TABLE IF EXISTS mysql2_stmt_decimal_test'
@@ -268,18 +334,19 @@ RSpec.describe Mysql2::Statement do
268
334
  end
269
335
 
270
336
  context "#fields" do
271
- before(:each) do
272
- @client.query "USE test"
273
- @test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute
274
- end
275
-
276
337
  it "method should exist" do
277
- expect(@test_result).to respond_to(:fields)
338
+ stmt = @client.prepare("SELECT 1")
339
+ expect(stmt).to respond_to(:fields)
278
340
  end
279
341
 
280
342
  it "should return an array of field names in proper order" do
281
- result = @client.prepare("SELECT 'a', 'b', 'c'").execute
282
- 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)
283
350
  end
284
351
  end
285
352
 
@@ -309,36 +376,47 @@ RSpec.describe Mysql2::Statement do
309
376
  expect(@test_result['tiny_int_test']).to eql(1)
310
377
  end
311
378
 
312
- it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
313
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
314
- id1 = @client.last_id
315
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
316
- id2 = @client.last_id
317
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
318
- 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
319
386
 
320
- result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
321
- result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
322
- result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
323
- expect(result1.first['bool_cast_test']).to be true
324
- expect(result2.first['bool_cast_test']).to be false
325
- 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
326
390
 
327
- @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
328
400
  end
329
401
 
330
- it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
331
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
332
- id1 = @client.last_id
333
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
334
- 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
335
408
 
336
- result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
337
- result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
338
- expect(result1.first['single_bit_test']).to be true
339
- 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
340
412
 
341
- @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
342
420
  end
343
421
 
344
422
  it "should return Fixnum for a SMALLINT value" do
@@ -395,39 +473,39 @@ RSpec.describe Mysql2::Statement do
395
473
 
396
474
  it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
397
475
  # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
398
- 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
399
477
  expect(r.first['test']).to be_an_instance_of(klass)
400
478
  end
401
479
 
402
480
  it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
403
481
  # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
404
- 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
405
483
  expect(r.first['test']).to be_an_instance_of(klass)
406
484
  end
407
485
  elsif 1.size == 8 # 64bit
408
486
  if RUBY_VERSION =~ /1.8/
409
487
  it "should return Time when timestamp is > 0138-12-31 11:59:59" do
410
- 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
411
489
  expect(r.first['test']).to be_an_instance_of(Time)
412
490
  end
413
491
 
414
492
  it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
415
- 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
416
494
  expect(r.first['test']).to be_an_instance_of(DateTime)
417
495
  end
418
496
 
419
497
  it "should return Time when timestamp is > 2038-01-19T03:14:07" do
420
- 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
421
499
  expect(r.first['test']).to be_an_instance_of(Time)
422
500
  end
423
501
  else
424
502
  it "should return Time when timestamp is < 1901-12-13 20:45:52" do
425
- 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
426
504
  expect(r.first['test']).to be_an_instance_of(Time)
427
505
  end
428
506
 
429
507
  it "should return Time when timestamp is > 2038-01-19T03:14:07" do
430
- 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
431
509
  expect(r.first['test']).to be_an_instance_of(Time)
432
510
  end
433
511
  end
@@ -466,10 +544,9 @@ RSpec.describe Mysql2::Statement do
466
544
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
467
545
  expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
468
546
 
469
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
547
+ client2 = new_client(:encoding => 'ascii')
470
548
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
471
549
  expect(result['enum_test'].encoding).to eql(Encoding::US_ASCII)
472
- client2.close
473
550
  end
474
551
  end
475
552
 
@@ -499,10 +576,9 @@ RSpec.describe Mysql2::Statement do
499
576
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
500
577
  expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
501
578
 
502
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
579
+ client2 = new_client(:encoding => 'ascii')
503
580
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
504
581
  expect(result['set_test'].encoding).to eql(Encoding::US_ASCII)
505
- client2.close
506
582
  end
507
583
  end
508
584
 
@@ -593,10 +669,9 @@ RSpec.describe Mysql2::Statement do
593
669
  result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
594
670
  expect(result[field].encoding).to eql(Encoding::UTF_8)
595
671
 
596
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
672
+ client2 = new_client(:encoding => 'ascii')
597
673
  result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
598
674
  expect(result[field].encoding).to eql(Encoding::US_ASCII)
599
- client2.close
600
675
  end
601
676
  end
602
677
 
@@ -689,9 +764,7 @@ RSpec.describe Mysql2::Statement do
689
764
  context 'close' do
690
765
  it 'should free server resources' do
691
766
  stmt = @client.prepare 'SELECT 1'
692
- expect { stmt.close }.to change {
693
- @client.query("SHOW STATUS LIKE 'Prepared_stmt_count'").first['Value'].to_i
694
- }.by(-1)
767
+ expect { stmt.close }.to change(&method(:stmt_count)).by(-1)
695
768
  end
696
769
 
697
770
  it 'should raise an error on subsequent execution' do
data/spec/spec_helper.rb CHANGED
@@ -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
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- set -eu
3
+ set -eux
4
4
 
5
5
  echo "
6
6
  [ ca ]