mysql2 0.4.10 → 0.5.1
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.
- checksums.yaml +4 -4
- data/README.md +23 -6
- data/examples/eventmachine.rb +0 -2
- data/examples/threaded.rb +2 -4
- data/ext/mysql2/client.c +100 -51
- data/ext/mysql2/client.h +1 -39
- data/ext/mysql2/extconf.rb +23 -20
- data/ext/mysql2/mysql2_ext.c +2 -1
- data/ext/mysql2/mysql2_ext.h +8 -4
- data/ext/mysql2/result.c +15 -75
- data/ext/mysql2/result.h +2 -3
- data/ext/mysql2/statement.c +78 -71
- data/ext/mysql2/statement.h +0 -2
- data/ext/mysql2/wait_for_single_fd.h +2 -1
- data/lib/mysql2.rb +14 -15
- data/lib/mysql2/client.rb +33 -27
- data/lib/mysql2/em.rb +2 -4
- data/lib/mysql2/error.rb +49 -20
- data/lib/mysql2/result.rb +2 -0
- data/lib/mysql2/statement.rb +3 -9
- data/lib/mysql2/version.rb +1 -1
- data/spec/em/em_spec.rb +5 -6
- data/spec/mysql2/client_spec.rb +206 -173
- data/spec/mysql2/error_spec.rb +0 -4
- data/spec/mysql2/result_spec.rb +94 -154
- data/spec/mysql2/statement_spec.rb +105 -169
- data/spec/spec_helper.rb +6 -2
- data/support/mysql_enc_to_ruby.rb +2 -2
- data/support/ruby_enc_to_mysql.rb +5 -5
- metadata +6 -5
@@ -1,12 +1,15 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
require './spec/spec_helper.rb'
|
3
2
|
|
4
3
|
RSpec.describe Mysql2::Statement do
|
5
4
|
before :each do
|
6
|
-
@client = new_client(:
|
5
|
+
@client = new_client(encoding: "utf8")
|
7
6
|
end
|
8
7
|
|
9
8
|
def stmt_count
|
9
|
+
# Use the performance schema in MySQL 5.7 and above
|
10
|
+
@client.query("SELECT COUNT(1) AS count FROM performance_schema.prepared_statements_instances").first['count'].to_i
|
11
|
+
rescue Mysql2::Error
|
12
|
+
# Fall back to the global prepapred statement counter
|
10
13
|
@client.query("SHOW STATUS LIKE 'Prepared_stmt_count'").first['Value'].to_i
|
11
14
|
end
|
12
15
|
|
@@ -87,6 +90,20 @@ RSpec.describe Mysql2::Statement do
|
|
87
90
|
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
91
|
end
|
89
92
|
|
93
|
+
it "should accept keyword arguments on statement execute" do
|
94
|
+
stmt = @client.prepare 'SELECT 1 AS a'
|
95
|
+
|
96
|
+
expect(stmt.execute(as: :hash).first).to eq("a" => 1)
|
97
|
+
expect(stmt.execute(as: :array).first).to eq([1])
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should accept bind arguments and keyword arguments on statement execute" do
|
101
|
+
stmt = @client.prepare 'SELECT ? AS a'
|
102
|
+
|
103
|
+
expect(stmt.execute(1, as: :hash).first).to eq("a" => 1)
|
104
|
+
expect(stmt.execute(1, as: :array).first).to eq([1])
|
105
|
+
end
|
106
|
+
|
90
107
|
it "should keep its result after other query" do
|
91
108
|
@client.query 'USE test'
|
92
109
|
@client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
|
@@ -147,24 +164,16 @@ RSpec.describe Mysql2::Statement do
|
|
147
164
|
now = Time.now
|
148
165
|
statement = @client.prepare('SELECT ? AS a')
|
149
166
|
result = statement.execute(now)
|
150
|
-
|
151
|
-
|
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
|
167
|
+
# microseconds is six digits after the decimal, but only test on 5 significant figures
|
168
|
+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
|
156
169
|
end
|
157
170
|
|
158
171
|
it "should prepare DateTime values with microseconds" do
|
159
172
|
now = DateTime.now
|
160
173
|
statement = @client.prepare('SELECT ? AS a')
|
161
174
|
result = statement.execute(now)
|
162
|
-
|
163
|
-
|
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
|
175
|
+
# microseconds is six digits after the decimal, but only test on 5 significant figures
|
176
|
+
expect(result.first['a'].strftime('%F %T.%5N %z')).to eql(now.strftime('%F %T.%5N %z'))
|
168
177
|
end
|
169
178
|
|
170
179
|
it "should tell us about the fields" do
|
@@ -178,7 +187,7 @@ RSpec.describe Mysql2::Statement do
|
|
178
187
|
|
179
188
|
it "should handle as a decimal binding a BigDecimal" do
|
180
189
|
stmt = @client.prepare('SELECT ? AS decimal_test')
|
181
|
-
test_result = stmt.execute(BigDecimal
|
190
|
+
test_result = stmt.execute(BigDecimal("123.45")).first
|
182
191
|
expect(test_result['decimal_test']).to be_an_instance_of(BigDecimal)
|
183
192
|
expect(test_result['decimal_test']).to eql(123.45)
|
184
193
|
end
|
@@ -188,17 +197,16 @@ RSpec.describe Mysql2::Statement do
|
|
188
197
|
@client.query 'DROP TABLE IF EXISTS mysql2_stmt_decimal_test'
|
189
198
|
@client.query 'CREATE TABLE mysql2_stmt_decimal_test (decimal_test DECIMAL(10,3))'
|
190
199
|
|
191
|
-
@client.prepare("INSERT INTO mysql2_stmt_decimal_test VALUES (?)").execute(BigDecimal
|
200
|
+
@client.prepare("INSERT INTO mysql2_stmt_decimal_test VALUES (?)").execute(BigDecimal("123.45"))
|
192
201
|
|
193
202
|
test_result = @client.query("SELECT * FROM mysql2_stmt_decimal_test").first
|
194
203
|
expect(test_result['decimal_test']).to eql(123.45)
|
195
204
|
end
|
196
205
|
|
197
206
|
it "should warn but still work if cache_rows is set to false" do
|
198
|
-
@client.query_options.merge!(:cache_rows => false)
|
199
207
|
statement = @client.prepare 'SELECT 1'
|
200
208
|
result = nil
|
201
|
-
expect { result = statement.execute.to_a }.to output(/:cache_rows is forced for prepared statements/).to_stderr
|
209
|
+
expect { result = statement.execute(cache_rows: false).to_a }.to output(/:cache_rows is forced for prepared statements/).to_stderr
|
202
210
|
expect(result.length).to eq(1)
|
203
211
|
end
|
204
212
|
|
@@ -217,7 +225,7 @@ RSpec.describe Mysql2::Statement do
|
|
217
225
|
|
218
226
|
it "should be able to retrieve utf8 field names correctly" do
|
219
227
|
stmt = @client.prepare 'SELECT * FROM `テーブル`'
|
220
|
-
expect(stmt.fields).to eq(%w
|
228
|
+
expect(stmt.fields).to eq(%w[整数 文字列])
|
221
229
|
result = stmt.execute
|
222
230
|
|
223
231
|
expect(result.to_a).to eq([{ "整数" => 1, "文字列" => "イチ" }, { "整数" => 2, "文字列" => "弐" }, { "整数" => 3, "文字列" => "さん" }])
|
@@ -241,16 +249,13 @@ RSpec.describe Mysql2::Statement do
|
|
241
249
|
|
242
250
|
expect(result.to_a).to eq([{ "整数" => 1 }])
|
243
251
|
end
|
244
|
-
end
|
252
|
+
end
|
245
253
|
|
246
254
|
context "streaming result" do
|
247
255
|
it "should be able to stream query result" do
|
248
256
|
n = 1
|
249
257
|
stmt = @client.prepare("SELECT 1 UNION SELECT 2")
|
250
|
-
|
251
|
-
@client.query_options.merge!(:stream => true, :cache_rows => false, :as => :array)
|
252
|
-
|
253
|
-
stmt.execute.each do |r|
|
258
|
+
stmt.execute(stream: true, cache_rows: false, as: :array).each do |r|
|
254
259
|
case n
|
255
260
|
when 1
|
256
261
|
expect(r).to eq([1])
|
@@ -276,23 +281,17 @@ RSpec.describe Mysql2::Statement do
|
|
276
281
|
end
|
277
282
|
|
278
283
|
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
|
279
|
-
@client.
|
280
|
-
@result = @client.prepare("SELECT 1").execute
|
284
|
+
@result = @client.prepare("SELECT 1").execute(symbolize_keys: true)
|
281
285
|
@result.each do |row|
|
282
286
|
expect(row.keys.first).to be_an_instance_of(Symbol)
|
283
287
|
end
|
284
|
-
@client.query_options[:symbolize_keys] = false
|
285
288
|
end
|
286
289
|
|
287
290
|
it "should be able to return results as an array" do
|
288
|
-
@client.
|
289
|
-
|
290
|
-
@result = @client.prepare("SELECT 1").execute
|
291
|
+
@result = @client.prepare("SELECT 1").execute(as: :array)
|
291
292
|
@result.each do |row|
|
292
293
|
expect(row).to be_an_instance_of(Array)
|
293
294
|
end
|
294
|
-
|
295
|
-
@client.query_options[:as] = :hash
|
296
295
|
end
|
297
296
|
|
298
297
|
it "should cache previously yielded results by default" do
|
@@ -301,35 +300,21 @@ RSpec.describe Mysql2::Statement do
|
|
301
300
|
end
|
302
301
|
|
303
302
|
it "should yield different value for #first if streaming" do
|
304
|
-
@client.
|
305
|
-
@client.query_options[:cache_rows] = false
|
306
|
-
|
307
|
-
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
|
303
|
+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: true, cache_rows: true)
|
308
304
|
expect(result.first).not_to eql(result.first)
|
309
|
-
|
310
|
-
@client.query_options[:stream] = false
|
311
|
-
@client.query_options[:cache_rows] = true
|
312
305
|
end
|
313
306
|
|
314
307
|
it "should yield the same value for #first if streaming is disabled" do
|
315
|
-
@client.
|
316
|
-
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
|
308
|
+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: false)
|
317
309
|
expect(result.first).to eql(result.first)
|
318
310
|
end
|
319
311
|
|
320
312
|
it "should throw an exception if we try to iterate twice when streaming is enabled" do
|
321
|
-
@client.
|
322
|
-
|
323
|
-
|
324
|
-
result = @client.prepare("SELECT 1 UNION SELECT 2").execute
|
325
|
-
|
326
|
-
expect {
|
313
|
+
result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: true, cache_rows: false)
|
314
|
+
expect do
|
327
315
|
result.each {}
|
328
316
|
result.each {}
|
329
|
-
|
330
|
-
|
331
|
-
@client.query_options[:stream] = false
|
332
|
-
@client.query_options[:cache_rows] = true
|
317
|
+
end.to raise_exception(Mysql2::Error)
|
333
318
|
end
|
334
319
|
end
|
335
320
|
|
@@ -341,7 +326,7 @@ RSpec.describe Mysql2::Statement do
|
|
341
326
|
|
342
327
|
it "should return an array of field names in proper order" do
|
343
328
|
stmt = @client.prepare("SELECT 'a', 'b', 'c'")
|
344
|
-
expect(stmt.fields).to eql(%w
|
329
|
+
expect(stmt.fields).to eql(%w[a b c])
|
345
330
|
end
|
346
331
|
|
347
332
|
it "should return nil for statement with no result fields" do
|
@@ -351,48 +336,44 @@ RSpec.describe Mysql2::Statement do
|
|
351
336
|
end
|
352
337
|
|
353
338
|
context "row data type mapping" do
|
354
|
-
|
355
|
-
@client.query "USE test"
|
356
|
-
@test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute.first
|
357
|
-
end
|
339
|
+
let(:test_result) { @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute.first }
|
358
340
|
|
359
341
|
it "should return nil for a NULL value" do
|
360
|
-
expect(
|
361
|
-
expect(
|
342
|
+
expect(test_result['null_test']).to be_an_instance_of(NilClass)
|
343
|
+
expect(test_result['null_test']).to eql(nil)
|
362
344
|
end
|
363
345
|
|
364
346
|
it "should return String for a BIT(64) value" do
|
365
|
-
expect(
|
366
|
-
expect(
|
347
|
+
expect(test_result['bit_test']).to be_an_instance_of(String)
|
348
|
+
expect(test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
|
367
349
|
end
|
368
350
|
|
369
351
|
it "should return String for a BIT(1) value" do
|
370
|
-
expect(
|
371
|
-
expect(
|
352
|
+
expect(test_result['single_bit_test']).to be_an_instance_of(String)
|
353
|
+
expect(test_result['single_bit_test']).to eql("\001")
|
372
354
|
end
|
373
355
|
|
374
356
|
it "should return Fixnum for a TINYINT value" do
|
375
|
-
expect(
|
376
|
-
expect(
|
357
|
+
expect(num_classes).to include(test_result['tiny_int_test'].class)
|
358
|
+
expect(test_result['tiny_int_test']).to eql(1)
|
377
359
|
end
|
378
360
|
|
379
361
|
context "cast booleans for TINYINT if :cast_booleans is enabled" do
|
380
362
|
# rubocop:disable Style/Semicolon
|
381
|
-
let(:
|
382
|
-
let(:
|
383
|
-
let(:
|
384
|
-
let(:id3) { client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'; client.last_id }
|
363
|
+
let(:id1) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 1)'; @client.last_id }
|
364
|
+
let(:id2) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES ( 0)'; @client.last_id }
|
365
|
+
let(:id3) { @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'; @client.last_id }
|
385
366
|
# rubocop:enable Style/Semicolon
|
386
367
|
|
387
368
|
after do
|
388
|
-
client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
|
369
|
+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
|
389
370
|
end
|
390
371
|
|
391
372
|
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
|
373
|
+
query = @client.prepare 'SELECT bool_cast_test FROM mysql2_test WHERE id = ?'
|
374
|
+
result1 = query.execute id1, cast_booleans: true
|
375
|
+
result2 = query.execute id2, cast_booleans: true
|
376
|
+
result3 = query.execute id3, cast_booleans: true
|
396
377
|
expect(result1.first['bool_cast_test']).to be true
|
397
378
|
expect(result2.first['bool_cast_test']).to be false
|
398
379
|
expect(result3.first['bool_cast_test']).to be true
|
@@ -401,134 +382,96 @@ RSpec.describe Mysql2::Statement do
|
|
401
382
|
|
402
383
|
context "cast booleans for BIT(1) if :cast_booleans is enabled" do
|
403
384
|
# rubocop:disable Style/Semicolon
|
404
|
-
let(:
|
405
|
-
let(:
|
406
|
-
let(:id2) { client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'; client.last_id }
|
385
|
+
let(:id1) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'; @client.last_id }
|
386
|
+
let(:id2) { @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'; @client.last_id }
|
407
387
|
# rubocop:enable Style/Semicolon
|
408
388
|
|
409
389
|
after do
|
410
|
-
client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
|
390
|
+
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
|
411
391
|
end
|
412
392
|
|
413
393
|
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
|
394
|
+
query = @client.prepare 'SELECT single_bit_test FROM mysql2_test WHERE id = ?'
|
395
|
+
result1 = query.execute id1, cast_booleans: true
|
396
|
+
result2 = query.execute id2, cast_booleans: true
|
417
397
|
expect(result1.first['single_bit_test']).to be true
|
418
398
|
expect(result2.first['single_bit_test']).to be false
|
419
399
|
end
|
420
400
|
end
|
421
401
|
|
422
402
|
it "should return Fixnum for a SMALLINT value" do
|
423
|
-
expect(
|
424
|
-
expect(
|
403
|
+
expect(num_classes).to include(test_result['small_int_test'].class)
|
404
|
+
expect(test_result['small_int_test']).to eql(10)
|
425
405
|
end
|
426
406
|
|
427
407
|
it "should return Fixnum for a MEDIUMINT value" do
|
428
|
-
expect(
|
429
|
-
expect(
|
408
|
+
expect(num_classes).to include(test_result['medium_int_test'].class)
|
409
|
+
expect(test_result['medium_int_test']).to eql(10)
|
430
410
|
end
|
431
411
|
|
432
412
|
it "should return Fixnum for an INT value" do
|
433
|
-
expect(
|
434
|
-
expect(
|
413
|
+
expect(num_classes).to include(test_result['int_test'].class)
|
414
|
+
expect(test_result['int_test']).to eql(10)
|
435
415
|
end
|
436
416
|
|
437
417
|
it "should return Fixnum for a BIGINT value" do
|
438
|
-
expect(
|
439
|
-
expect(
|
418
|
+
expect(num_classes).to include(test_result['big_int_test'].class)
|
419
|
+
expect(test_result['big_int_test']).to eql(10)
|
440
420
|
end
|
441
421
|
|
442
422
|
it "should return Fixnum for a YEAR value" do
|
443
|
-
expect(
|
444
|
-
expect(
|
423
|
+
expect(num_classes).to include(test_result['year_test'].class)
|
424
|
+
expect(test_result['year_test']).to eql(2009)
|
445
425
|
end
|
446
426
|
|
447
427
|
it "should return BigDecimal for a DECIMAL value" do
|
448
|
-
expect(
|
449
|
-
expect(
|
428
|
+
expect(test_result['decimal_test']).to be_an_instance_of(BigDecimal)
|
429
|
+
expect(test_result['decimal_test']).to eql(10.3)
|
450
430
|
end
|
451
431
|
|
452
432
|
it "should return Float for a FLOAT value" do
|
453
|
-
expect(
|
454
|
-
expect(
|
433
|
+
expect(test_result['float_test']).to be_an_instance_of(Float)
|
434
|
+
expect(test_result['float_test']).to be_within(1e-5).of(10.3)
|
455
435
|
end
|
456
436
|
|
457
437
|
it "should return Float for a DOUBLE value" do
|
458
|
-
expect(
|
459
|
-
expect(
|
438
|
+
expect(test_result['double_test']).to be_an_instance_of(Float)
|
439
|
+
expect(test_result['double_test']).to eql(10.3)
|
460
440
|
end
|
461
441
|
|
462
442
|
it "should return Time for a DATETIME value when within the supported range" do
|
463
|
-
expect(
|
464
|
-
expect(
|
443
|
+
expect(test_result['date_time_test']).to be_an_instance_of(Time)
|
444
|
+
expect(test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
|
465
445
|
end
|
466
446
|
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
Time
|
472
|
-
end
|
473
|
-
|
474
|
-
it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
|
475
|
-
# 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
|
476
|
-
r = @client.prepare("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test").execute
|
477
|
-
expect(r.first['test']).to be_an_instance_of(klass)
|
478
|
-
end
|
479
|
-
|
480
|
-
it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
|
481
|
-
# 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
|
482
|
-
r = @client.prepare("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test").execute
|
483
|
-
expect(r.first['test']).to be_an_instance_of(klass)
|
484
|
-
end
|
485
|
-
elsif 1.size == 8 # 64bit
|
486
|
-
if RUBY_VERSION =~ /1.8/
|
487
|
-
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
|
488
|
-
r = @client.prepare("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test").execute
|
489
|
-
expect(r.first['test']).to be_an_instance_of(Time)
|
490
|
-
end
|
491
|
-
|
492
|
-
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
|
493
|
-
r = @client.prepare("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test").execute
|
494
|
-
expect(r.first['test']).to be_an_instance_of(DateTime)
|
495
|
-
end
|
496
|
-
|
497
|
-
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
498
|
-
r = @client.prepare("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test").execute
|
499
|
-
expect(r.first['test']).to be_an_instance_of(Time)
|
500
|
-
end
|
501
|
-
else
|
502
|
-
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
|
503
|
-
r = @client.prepare("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test").execute
|
504
|
-
expect(r.first['test']).to be_an_instance_of(Time)
|
505
|
-
end
|
447
|
+
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
|
448
|
+
r = @client.prepare("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test").execute
|
449
|
+
expect(r.first['test']).to be_an_instance_of(Time)
|
450
|
+
end
|
506
451
|
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
end
|
511
|
-
end
|
452
|
+
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
453
|
+
r = @client.prepare("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test").execute
|
454
|
+
expect(r.first['test']).to be_an_instance_of(Time)
|
512
455
|
end
|
513
456
|
|
514
457
|
it "should return Time for a TIMESTAMP value when within the supported range" do
|
515
|
-
expect(
|
516
|
-
expect(
|
458
|
+
expect(test_result['timestamp_test']).to be_an_instance_of(Time)
|
459
|
+
expect(test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
|
517
460
|
end
|
518
461
|
|
519
462
|
it "should return Time for a TIME value" do
|
520
|
-
expect(
|
521
|
-
expect(
|
463
|
+
expect(test_result['time_test']).to be_an_instance_of(Time)
|
464
|
+
expect(test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2000-01-01 11:44:00')
|
522
465
|
end
|
523
466
|
|
524
467
|
it "should return Date for a DATE value" do
|
525
|
-
expect(
|
526
|
-
expect(
|
468
|
+
expect(test_result['date_test']).to be_an_instance_of(Date)
|
469
|
+
expect(test_result['date_test'].strftime("%Y-%m-%d")).to eql('2010-04-04')
|
527
470
|
end
|
528
471
|
|
529
472
|
it "should return String for an ENUM value" do
|
530
|
-
expect(
|
531
|
-
expect(
|
473
|
+
expect(test_result['enum_test']).to be_an_instance_of(String)
|
474
|
+
expect(test_result['enum_test']).to eql('val1')
|
532
475
|
end
|
533
476
|
|
534
477
|
it "should raise an error given an invalid DATETIME" do
|
@@ -537,14 +480,12 @@ RSpec.describe Mysql2::Statement do
|
|
537
480
|
end
|
538
481
|
|
539
482
|
context "string encoding for ENUM values" do
|
540
|
-
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
541
|
-
|
542
483
|
it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
543
484
|
with_internal_encoding nil do
|
544
485
|
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
545
486
|
expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
|
546
487
|
|
547
|
-
client2 = new_client(:
|
488
|
+
client2 = new_client(encoding: 'ascii')
|
548
489
|
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
549
490
|
expect(result['enum_test'].encoding).to eql(Encoding::US_ASCII)
|
550
491
|
end
|
@@ -564,19 +505,17 @@ RSpec.describe Mysql2::Statement do
|
|
564
505
|
end
|
565
506
|
|
566
507
|
it "should return String for a SET value" do
|
567
|
-
expect(
|
568
|
-
expect(
|
508
|
+
expect(test_result['set_test']).to be_an_instance_of(String)
|
509
|
+
expect(test_result['set_test']).to eql('val1,val2')
|
569
510
|
end
|
570
511
|
|
571
512
|
context "string encoding for SET values" do
|
572
|
-
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
573
|
-
|
574
513
|
it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
575
514
|
with_internal_encoding nil do
|
576
515
|
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
577
516
|
expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
|
578
517
|
|
579
|
-
client2 = new_client(:
|
518
|
+
client2 = new_client(encoding: 'ascii')
|
580
519
|
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
581
520
|
expect(result['set_test'].encoding).to eql(Encoding::US_ASCII)
|
582
521
|
end
|
@@ -596,13 +535,11 @@ RSpec.describe Mysql2::Statement do
|
|
596
535
|
end
|
597
536
|
|
598
537
|
it "should return String for a BINARY value" do
|
599
|
-
expect(
|
600
|
-
expect(
|
538
|
+
expect(test_result['binary_test']).to be_an_instance_of(String)
|
539
|
+
expect(test_result['binary_test']).to eql("test#{"\000" * 6}")
|
601
540
|
end
|
602
541
|
|
603
542
|
context "string encoding for BINARY values" do
|
604
|
-
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
605
|
-
|
606
543
|
it "should default to binary if Encoding.default_internal is nil" do
|
607
544
|
with_internal_encoding nil do
|
608
545
|
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
@@ -637,14 +574,12 @@ RSpec.describe Mysql2::Statement do
|
|
637
574
|
'long_text_test' => 'LONGTEXT',
|
638
575
|
}.each do |field, type|
|
639
576
|
it "should return a String for #{type}" do
|
640
|
-
expect(
|
641
|
-
expect(
|
577
|
+
expect(test_result[field]).to be_an_instance_of(String)
|
578
|
+
expect(test_result[field]).to eql("test")
|
642
579
|
end
|
643
580
|
|
644
581
|
context "string encoding for #{type} values" do
|
645
|
-
|
646
|
-
|
647
|
-
if %w(VARBINARY TINYBLOB BLOB MEDIUMBLOB LONGBLOB).include?(type)
|
582
|
+
if %w[VARBINARY TINYBLOB BLOB MEDIUMBLOB LONGBLOB].include?(type)
|
648
583
|
it "should default to binary if Encoding.default_internal is nil" do
|
649
584
|
with_internal_encoding nil do
|
650
585
|
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
@@ -669,7 +604,7 @@ RSpec.describe Mysql2::Statement do
|
|
669
604
|
result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
670
605
|
expect(result[field].encoding).to eql(Encoding::UTF_8)
|
671
606
|
|
672
|
-
client2 = new_client(:
|
607
|
+
client2 = new_client(encoding: 'ascii')
|
673
608
|
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
674
609
|
expect(result[field].encoding).to eql(Encoding::US_ASCII)
|
675
610
|
end
|
@@ -731,7 +666,6 @@ RSpec.describe Mysql2::Statement do
|
|
731
666
|
|
732
667
|
it 'should return number of rows affected by an insert' do
|
733
668
|
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
|
734
|
-
expect(stmt.affected_rows).to eq 0
|
735
669
|
stmt.execute 1
|
736
670
|
expect(stmt.affected_rows).to eq 1
|
737
671
|
end
|
@@ -764,7 +698,9 @@ RSpec.describe Mysql2::Statement do
|
|
764
698
|
context 'close' do
|
765
699
|
it 'should free server resources' do
|
766
700
|
stmt = @client.prepare 'SELECT 1'
|
701
|
+
GC.disable
|
767
702
|
expect { stmt.close }.to change(&method(:stmt_count)).by(-1)
|
703
|
+
GC.enable
|
768
704
|
end
|
769
705
|
|
770
706
|
it 'should raise an error on subsequent execution' do
|