mysql2 0.3.21 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1 -0
  3. data/README.md +28 -6
  4. data/examples/eventmachine.rb +1 -1
  5. data/examples/threaded.rb +4 -6
  6. data/ext/mysql2/client.c +93 -84
  7. data/ext/mysql2/client.h +21 -1
  8. data/ext/mysql2/extconf.rb +60 -41
  9. data/ext/mysql2/infile.c +2 -2
  10. data/ext/mysql2/mysql2_ext.c +1 -0
  11. data/ext/mysql2/mysql2_ext.h +5 -6
  12. data/ext/mysql2/mysql_enc_name_to_ruby.h +2 -2
  13. data/ext/mysql2/mysql_enc_to_ruby.h +25 -22
  14. data/ext/mysql2/result.c +464 -97
  15. data/ext/mysql2/result.h +11 -4
  16. data/ext/mysql2/statement.c +491 -0
  17. data/ext/mysql2/statement.h +18 -0
  18. data/lib/mysql2/client.rb +32 -24
  19. data/lib/mysql2/console.rb +1 -1
  20. data/lib/mysql2/em.rb +5 -6
  21. data/lib/mysql2/error.rb +17 -26
  22. data/lib/mysql2/field.rb +3 -0
  23. data/lib/mysql2/statement.rb +17 -0
  24. data/lib/mysql2/version.rb +1 -1
  25. data/lib/mysql2.rb +37 -35
  26. data/spec/em/em_spec.rb +21 -21
  27. data/spec/mysql2/client_spec.rb +322 -290
  28. data/spec/mysql2/error_spec.rb +37 -36
  29. data/spec/mysql2/result_spec.rb +200 -209
  30. data/spec/mysql2/statement_spec.rb +684 -0
  31. data/spec/spec_helper.rb +7 -0
  32. data/spec/ssl/ca-cert.pem +17 -0
  33. data/spec/ssl/ca-key.pem +27 -0
  34. data/spec/ssl/ca.cnf +22 -0
  35. data/spec/ssl/cert.cnf +22 -0
  36. data/spec/ssl/client-cert.pem +17 -0
  37. data/spec/ssl/client-key.pem +27 -0
  38. data/spec/ssl/client-req.pem +15 -0
  39. data/spec/ssl/gen_certs.sh +48 -0
  40. data/spec/ssl/pkcs8-client-key.pem +28 -0
  41. data/spec/ssl/pkcs8-server-key.pem +28 -0
  42. data/spec/ssl/server-cert.pem +17 -0
  43. data/spec/ssl/server-key.pem +27 -0
  44. data/spec/ssl/server-req.pem +15 -0
  45. data/support/mysql_enc_to_ruby.rb +7 -8
  46. data/support/ruby_enc_to_mysql.rb +1 -1
  47. metadata +41 -47
@@ -0,0 +1,684 @@
1
+ # encoding: UTF-8
2
+ require './spec/spec_helper.rb'
3
+
4
+ RSpec.describe Mysql2::Statement do
5
+ before :each do
6
+ @client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
7
+ end
8
+
9
+ it "should create a statement" do
10
+ 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)
14
+ expect(statement).to be_an_instance_of(Mysql2::Statement)
15
+ end
16
+
17
+ it "should raise an exception when server disconnects" do
18
+ @client.close
19
+ expect { @client.prepare 'SELECT 1' }.to raise_error(Mysql2::Error)
20
+ end
21
+
22
+ it "should tell us the param count" do
23
+ statement = @client.prepare 'SELECT ?, ?'
24
+ expect(statement.param_count).to eq(2)
25
+
26
+ statement2 = @client.prepare 'SELECT 1'
27
+ expect(statement2.param_count).to eq(0)
28
+ end
29
+
30
+ it "should tell us the field count" do
31
+ statement = @client.prepare 'SELECT ?, ?'
32
+ expect(statement.field_count).to eq(2)
33
+
34
+ statement2 = @client.prepare 'SELECT 1'
35
+ expect(statement2.field_count).to eq(1)
36
+ end
37
+
38
+ it "should let us execute our statement" do
39
+ statement = @client.prepare 'SELECT 1'
40
+ expect(statement.execute).not_to eq(nil)
41
+ end
42
+
43
+ it "should raise an exception without a block" do
44
+ statement = @client.prepare 'SELECT 1'
45
+ expect { statement.execute.each }.to raise_error(LocalJumpError)
46
+ end
47
+
48
+ it "should tell us the result count" do
49
+ statement = @client.prepare 'SELECT 1'
50
+ result = statement.execute
51
+ expect(result.count).to eq(1)
52
+ end
53
+
54
+ it "should let us iterate over results" do
55
+ statement = @client.prepare 'SELECT 1'
56
+ result = statement.execute
57
+ rows = []
58
+ result.each { |r| rows << r }
59
+ expect(rows).to eq([{ "1" => 1 }])
60
+ end
61
+
62
+ it "should keep its result after other query" do
63
+ @client.query 'USE test'
64
+ @client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int)'
65
+ @client.query 'INSERT INTO mysql2_stmt_q (a) VALUES (1), (2)'
66
+ stmt = @client.prepare('SELECT a FROM mysql2_stmt_q WHERE a = ?')
67
+ result1 = stmt.execute(1)
68
+ result2 = stmt.execute(2)
69
+ expect(result2.first).to eq("a" => 2)
70
+ expect(result1.first).to eq("a" => 1)
71
+ @client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
72
+ end
73
+
74
+ it "should be reusable 1000 times" do
75
+ statement = @client.prepare 'SELECT 1'
76
+ 1000.times do
77
+ result = statement.execute
78
+ expect(result.to_a.length).to eq(1)
79
+ end
80
+ end
81
+
82
+ it "should be reusable 10000 times" do
83
+ statement = @client.prepare 'SELECT 1'
84
+ 10000.times do
85
+ result = statement.execute
86
+ expect(result.to_a.length).to eq(1)
87
+ end
88
+ end
89
+
90
+ it "should handle comparisons and likes" do
91
+ @client.query 'USE test'
92
+ @client.query 'CREATE TABLE IF NOT EXISTS mysql2_stmt_q(a int, b varchar(10))'
93
+ @client.query 'INSERT INTO mysql2_stmt_q (a, b) VALUES (1, "Hello"), (2, "World")'
94
+ statement = @client.prepare 'SELECT * FROM mysql2_stmt_q WHERE a < ?'
95
+ results = statement.execute(2)
96
+ expect(results.first).to eq("a" => 1, "b" => "Hello")
97
+
98
+ statement = @client.prepare 'SELECT * FROM mysql2_stmt_q WHERE b LIKE ?'
99
+ results = statement.execute('%orld')
100
+ expect(results.first).to eq("a" => 2, "b" => "World")
101
+
102
+ @client.query 'DROP TABLE IF EXISTS mysql2_stmt_q'
103
+ end
104
+
105
+ it "should select dates" do
106
+ statement = @client.prepare 'SELECT NOW()'
107
+ result = statement.execute
108
+ expect(result.first.first[1]).to be_an_instance_of(Time)
109
+ end
110
+
111
+ it "should tell us about the fields" do
112
+ statement = @client.prepare 'SELECT 1 as foo, 2'
113
+ statement.execute
114
+ list = statement.fields
115
+ expect(list.length).to eq(2)
116
+ expect(list.first).to eq('foo')
117
+ expect(list[1]).to eq('2')
118
+ end
119
+
120
+ context "utf8_db" do
121
+ before(:each) do
122
+ @client.query("DROP DATABASE IF EXISTS test_mysql2_stmt_utf8")
123
+ @client.query("CREATE DATABASE test_mysql2_stmt_utf8")
124
+ @client.query("USE test_mysql2_stmt_utf8")
125
+ @client.query("CREATE TABLE テーブル (整数 int, 文字列 varchar(32)) charset=utf8")
126
+ @client.query("INSERT INTO テーブル (整数, 文字列) VALUES (1, 'イチ'), (2, '弐'), (3, 'さん')")
127
+ end
128
+
129
+ after(:each) do
130
+ @client.query("DROP DATABASE test_mysql2_stmt_utf8")
131
+ end
132
+
133
+ it "should be able to retrieve utf8 field names correctly" do
134
+ stmt = @client.prepare 'SELECT * FROM `テーブル`'
135
+ expect(stmt.fields).to eq(%w(整数 文字列))
136
+ result = stmt.execute
137
+
138
+ expect(result.to_a).to eq([{ "整数" => 1, "文字列" => "イチ" }, { "整数" => 2, "文字列" => "弐" }, { "整数" => 3, "文字列" => "さん" }])
139
+ end
140
+
141
+ it "should be able to retrieve utf8 param query correctly" do
142
+ stmt = @client.prepare 'SELECT 整数 FROM テーブル WHERE 文字列 = ?'
143
+ expect(stmt.param_count).to eq(1)
144
+
145
+ result = stmt.execute 'イチ'
146
+
147
+ expect(result.to_a).to eq([{ "整数" => 1 }])
148
+ end
149
+
150
+ it "should be able to retrieve query with param in different encoding correctly" do
151
+ stmt = @client.prepare 'SELECT 整数 FROM テーブル WHERE 文字列 = ?'
152
+ expect(stmt.param_count).to eq(1)
153
+
154
+ param = 'イチ'.encode("EUC-JP")
155
+ result = stmt.execute param
156
+
157
+ expect(result.to_a).to eq([{ "整数" => 1 }])
158
+ end
159
+ end if defined? Encoding
160
+
161
+ context "streaming result" do
162
+ it "should be able to stream query result" do
163
+ n = 1
164
+ stmt = @client.prepare("SELECT 1 UNION SELECT 2")
165
+
166
+ @client.query_options.merge!(:stream => true, :cache_rows => false, :as => :array)
167
+
168
+ stmt.execute.each do |r|
169
+ case n
170
+ when 1
171
+ expect(r).to eq([1])
172
+ when 2
173
+ expect(r).to eq([2])
174
+ else
175
+ violated "returned more than two rows"
176
+ end
177
+ n += 1
178
+ end
179
+ end
180
+ end
181
+
182
+ context "#each" do
183
+ # note: The current impl. of prepared statement requires results to be cached on #execute except for streaming queries
184
+ # The drawback of this is that args of Result#each is ignored...
185
+
186
+ it "should yield rows as hash's" do
187
+ @result = @client.prepare("SELECT 1").execute
188
+ @result.each do |row|
189
+ expect(row).to be_an_instance_of(Hash)
190
+ end
191
+ end
192
+
193
+ it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
194
+ @client.query_options[:symbolize_keys] = true
195
+ @result = @client.prepare("SELECT 1").execute
196
+ @result.each do |row|
197
+ expect(row.keys.first).to be_an_instance_of(Symbol)
198
+ end
199
+ @client.query_options[:symbolize_keys] = false
200
+ end
201
+
202
+ it "should be able to return results as an array" do
203
+ @client.query_options[:as] = :array
204
+
205
+ @result = @client.prepare("SELECT 1").execute
206
+ @result.each do |row|
207
+ expect(row).to be_an_instance_of(Array)
208
+ end
209
+
210
+ @client.query_options[:as] = :hash
211
+ end
212
+
213
+ it "should cache previously yielded results by default" do
214
+ @result = @client.prepare("SELECT 1").execute
215
+ expect(@result.first.object_id).to eql(@result.first.object_id)
216
+ end
217
+
218
+ it "should yield different value for #first if streaming" do
219
+ @client.query_options[:stream] = true
220
+ @client.query_options[:cache_rows] = false
221
+
222
+ result = @client.prepare("SELECT 1 UNION SELECT 2").execute
223
+ expect(result.first).not_to eql(result.first)
224
+
225
+ @client.query_options[:stream] = false
226
+ @client.query_options[:cache_rows] = true
227
+ end
228
+
229
+ it "should yield the same value for #first if streaming is disabled" do
230
+ @client.query_options[:stream] = false
231
+ result = @client.prepare("SELECT 1 UNION SELECT 2").execute
232
+ expect(result.first).to eql(result.first)
233
+ end
234
+
235
+ it "should throw an exception if we try to iterate twice when streaming is enabled" do
236
+ @client.query_options[:stream] = true
237
+ @client.query_options[:cache_rows] = false
238
+
239
+ result = @client.prepare("SELECT 1 UNION SELECT 2").execute
240
+
241
+ expect {
242
+ result.each {}
243
+ result.each {}
244
+ }.to raise_exception(Mysql2::Error)
245
+
246
+ @client.query_options[:stream] = false
247
+ @client.query_options[:cache_rows] = true
248
+ end
249
+ end
250
+
251
+ context "#fields" do
252
+ before(:each) do
253
+ @client.query "USE test"
254
+ @test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute
255
+ end
256
+
257
+ it "method should exist" do
258
+ expect(@test_result).to respond_to(:fields)
259
+ end
260
+
261
+ it "should return an array of field names in proper order" do
262
+ result = @client.prepare("SELECT 'a', 'b', 'c'").execute
263
+ expect(result.fields).to eql(%w(a b c))
264
+ end
265
+ end
266
+
267
+ context "row data type mapping" do
268
+ before(:each) do
269
+ @client.query "USE test"
270
+ @test_result = @client.prepare("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").execute.first
271
+ end
272
+
273
+ it "should return nil for a NULL value" do
274
+ expect(@test_result['null_test']).to be_an_instance_of(NilClass)
275
+ expect(@test_result['null_test']).to eql(nil)
276
+ end
277
+
278
+ it "should return String for a BIT(64) value" do
279
+ expect(@test_result['bit_test']).to be_an_instance_of(String)
280
+ expect(@test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
281
+ end
282
+
283
+ it "should return String for a BIT(1) value" do
284
+ expect(@test_result['single_bit_test']).to be_an_instance_of(String)
285
+ expect(@test_result['single_bit_test']).to eql("\001")
286
+ end
287
+
288
+ it "should return Fixnum for a TINYINT value" do
289
+ expect([Fixnum, Bignum]).to include(@test_result['tiny_int_test'].class)
290
+ expect(@test_result['tiny_int_test']).to eql(1)
291
+ end
292
+
293
+ it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
294
+ @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
295
+ id1 = @client.last_id
296
+ @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
297
+ id2 = @client.last_id
298
+ @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
299
+ id3 = @client.last_id
300
+
301
+ result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
302
+ result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
303
+ result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
304
+ expect(result1.first['bool_cast_test']).to be true
305
+ expect(result2.first['bool_cast_test']).to be false
306
+ expect(result3.first['bool_cast_test']).to be true
307
+
308
+ @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
309
+ end
310
+
311
+ it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
312
+ @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
313
+ id1 = @client.last_id
314
+ @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
315
+ id2 = @client.last_id
316
+
317
+ result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
318
+ result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
319
+ expect(result1.first['single_bit_test']).to be true
320
+ expect(result2.first['single_bit_test']).to be false
321
+
322
+ @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
323
+ end
324
+
325
+ it "should return Fixnum for a SMALLINT value" do
326
+ expect([Fixnum, Bignum]).to include(@test_result['small_int_test'].class)
327
+ expect(@test_result['small_int_test']).to eql(10)
328
+ end
329
+
330
+ it "should return Fixnum for a MEDIUMINT value" do
331
+ expect([Fixnum, Bignum]).to include(@test_result['medium_int_test'].class)
332
+ expect(@test_result['medium_int_test']).to eql(10)
333
+ end
334
+
335
+ it "should return Fixnum for an INT value" do
336
+ expect([Fixnum, Bignum]).to include(@test_result['int_test'].class)
337
+ expect(@test_result['int_test']).to eql(10)
338
+ end
339
+
340
+ it "should return Fixnum for a BIGINT value" do
341
+ expect([Fixnum, Bignum]).to include(@test_result['big_int_test'].class)
342
+ expect(@test_result['big_int_test']).to eql(10)
343
+ end
344
+
345
+ it "should return Fixnum for a YEAR value" do
346
+ expect([Fixnum, Bignum]).to include(@test_result['year_test'].class)
347
+ expect(@test_result['year_test']).to eql(2009)
348
+ end
349
+
350
+ it "should return BigDecimal for a DECIMAL value" do
351
+ expect(@test_result['decimal_test']).to be_an_instance_of(BigDecimal)
352
+ expect(@test_result['decimal_test']).to eql(10.3)
353
+ end
354
+
355
+ it "should return Float for a FLOAT value" do
356
+ expect(@test_result['float_test']).to be_an_instance_of(Float)
357
+ expect(@test_result['float_test']).to be_within(1e-5).of(10.3)
358
+ end
359
+
360
+ it "should return Float for a DOUBLE value" do
361
+ expect(@test_result['double_test']).to be_an_instance_of(Float)
362
+ expect(@test_result['double_test']).to eql(10.3)
363
+ end
364
+
365
+ it "should return Time for a DATETIME value when within the supported range" do
366
+ expect(@test_result['date_time_test']).to be_an_instance_of(Time)
367
+ expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
368
+ end
369
+
370
+ if 1.size == 4 # 32bit
371
+ klass = if RUBY_VERSION =~ /1.8/
372
+ DateTime
373
+ else
374
+ Time
375
+ end
376
+
377
+ it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
378
+ # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
379
+ r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
380
+ expect(r.first['test']).to be_an_instance_of(klass)
381
+ end
382
+
383
+ it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
384
+ # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
385
+ r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
386
+ expect(r.first['test']).to be_an_instance_of(klass)
387
+ end
388
+ elsif 1.size == 8 # 64bit
389
+ if RUBY_VERSION =~ /1.8/
390
+ it "should return Time when timestamp is > 0138-12-31 11:59:59" do
391
+ r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
392
+ expect(r.first['test']).to be_an_instance_of(Time)
393
+ end
394
+
395
+ it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
396
+ r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
397
+ expect(r.first['test']).to be_an_instance_of(DateTime)
398
+ end
399
+
400
+ it "should return Time when timestamp is > 2038-01-19T03:14:07" do
401
+ r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
402
+ expect(r.first['test']).to be_an_instance_of(Time)
403
+ end
404
+ else
405
+ it "should return Time when timestamp is < 1901-12-13 20:45:52" do
406
+ r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
407
+ expect(r.first['test']).to be_an_instance_of(Time)
408
+ end
409
+
410
+ it "should return Time when timestamp is > 2038-01-19T03:14:07" do
411
+ r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
412
+ expect(r.first['test']).to be_an_instance_of(Time)
413
+ end
414
+ end
415
+ end
416
+
417
+ it "should return Time for a TIMESTAMP value when within the supported range" do
418
+ expect(@test_result['timestamp_test']).to be_an_instance_of(Time)
419
+ expect(@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
420
+ end
421
+
422
+ it "should return Time for a TIME value" do
423
+ expect(@test_result['time_test']).to be_an_instance_of(Time)
424
+ expect(@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2000-01-01 11:44:00')
425
+ end
426
+
427
+ it "should return Date for a DATE value" do
428
+ expect(@test_result['date_test']).to be_an_instance_of(Date)
429
+ expect(@test_result['date_test'].strftime("%Y-%m-%d")).to eql('2010-04-04')
430
+ end
431
+
432
+ it "should return String for an ENUM value" do
433
+ expect(@test_result['enum_test']).to be_an_instance_of(String)
434
+ expect(@test_result['enum_test']).to eql('val1')
435
+ end
436
+
437
+ it "should raise an error given an invalid DATETIME" do
438
+ expect { @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each }.to \
439
+ raise_error(Mysql2::Error, "Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
440
+ end
441
+
442
+ context "string encoding for ENUM values" do
443
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
444
+
445
+ it "should default to the connection's encoding if Encoding.default_internal is nil" do
446
+ with_internal_encoding nil do
447
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
448
+ expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
449
+
450
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
451
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
452
+ expect(result['enum_test'].encoding).to eql(Encoding::US_ASCII)
453
+ client2.close
454
+ end
455
+ end
456
+
457
+ it "should use Encoding.default_internal" do
458
+ with_internal_encoding Encoding::UTF_8 do
459
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
460
+ expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
461
+ end
462
+
463
+ with_internal_encoding Encoding::ASCII do
464
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
465
+ expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
466
+ end
467
+ end
468
+ end
469
+
470
+ it "should return String for a SET value" do
471
+ expect(@test_result['set_test']).to be_an_instance_of(String)
472
+ expect(@test_result['set_test']).to eql('val1,val2')
473
+ end
474
+
475
+ context "string encoding for SET values" do
476
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
477
+
478
+ it "should default to the connection's encoding if Encoding.default_internal is nil" do
479
+ with_internal_encoding nil do
480
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
481
+ expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
482
+
483
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
484
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
485
+ expect(result['set_test'].encoding).to eql(Encoding::US_ASCII)
486
+ client2.close
487
+ end
488
+ end
489
+
490
+ it "should use Encoding.default_internal" do
491
+ with_internal_encoding Encoding::UTF_8 do
492
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
493
+ expect(result['set_test'].encoding).to eql(Encoding.default_internal)
494
+ end
495
+
496
+ with_internal_encoding Encoding::ASCII do
497
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
498
+ expect(result['set_test'].encoding).to eql(Encoding.default_internal)
499
+ end
500
+ end
501
+ end
502
+
503
+ it "should return String for a BINARY value" do
504
+ expect(@test_result['binary_test']).to be_an_instance_of(String)
505
+ expect(@test_result['binary_test']).to eql("test#{"\000" * 6}")
506
+ end
507
+
508
+ context "string encoding for BINARY values" do
509
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
510
+
511
+ it "should default to binary if Encoding.default_internal is nil" do
512
+ with_internal_encoding nil do
513
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
514
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
515
+ end
516
+ end
517
+
518
+ it "should not use Encoding.default_internal" do
519
+ with_internal_encoding Encoding::UTF_8 do
520
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
521
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
522
+ end
523
+
524
+ with_internal_encoding Encoding::ASCII do
525
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
526
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
527
+ end
528
+ end
529
+ end
530
+
531
+ {
532
+ 'char_test' => 'CHAR',
533
+ 'varchar_test' => 'VARCHAR',
534
+ 'varbinary_test' => 'VARBINARY',
535
+ 'tiny_blob_test' => 'TINYBLOB',
536
+ 'tiny_text_test' => 'TINYTEXT',
537
+ 'blob_test' => 'BLOB',
538
+ 'text_test' => 'TEXT',
539
+ 'medium_blob_test' => 'MEDIUMBLOB',
540
+ 'medium_text_test' => 'MEDIUMTEXT',
541
+ 'long_blob_test' => 'LONGBLOB',
542
+ 'long_text_test' => 'LONGTEXT',
543
+ }.each do |field, type|
544
+ it "should return a String for #{type}" do
545
+ expect(@test_result[field]).to be_an_instance_of(String)
546
+ expect(@test_result[field]).to eql("test")
547
+ end
548
+
549
+ context "string encoding for #{type} values" do
550
+ before { pending('Encoding is undefined') unless defined?(Encoding) }
551
+
552
+ if %w(VARBINARY TINYBLOB BLOB MEDIUMBLOB LONGBLOB).include?(type)
553
+ it "should default to binary if Encoding.default_internal is nil" do
554
+ with_internal_encoding nil do
555
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
556
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
557
+ end
558
+ end
559
+
560
+ it "should not use Encoding.default_internal" do
561
+ with_internal_encoding Encoding::UTF_8 do
562
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
563
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
564
+ end
565
+
566
+ with_internal_encoding Encoding::ASCII do
567
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
568
+ expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
569
+ end
570
+ end
571
+ else
572
+ it "should default to utf-8 if Encoding.default_internal is nil" do
573
+ with_internal_encoding nil do
574
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
575
+ expect(result[field].encoding).to eql(Encoding::UTF_8)
576
+
577
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
578
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
579
+ expect(result[field].encoding).to eql(Encoding::US_ASCII)
580
+ client2.close
581
+ end
582
+ end
583
+
584
+ it "should use Encoding.default_internal" do
585
+ with_internal_encoding Encoding::UTF_8 do
586
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
587
+ expect(result[field].encoding).to eql(Encoding.default_internal)
588
+ end
589
+
590
+ with_internal_encoding Encoding::ASCII do
591
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
592
+ expect(result[field].encoding).to eql(Encoding.default_internal)
593
+ end
594
+ end
595
+ end
596
+ end
597
+ end
598
+ end
599
+
600
+ context 'last_id' do
601
+ before(:each) do
602
+ @client.query 'USE test'
603
+ @client.query 'CREATE TABLE IF NOT EXISTS lastIdTest (`id` BIGINT NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))'
604
+ end
605
+
606
+ after(:each) do
607
+ @client.query 'DROP TABLE lastIdTest'
608
+ end
609
+
610
+ it 'should return last insert id' do
611
+ stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
612
+ expect(stmt.last_id).to eq 0
613
+ stmt.execute 1
614
+ expect(stmt.last_id).to eq 1
615
+ end
616
+
617
+ it 'should handle bigint ids' do
618
+ stmt = @client.prepare 'INSERT INTO lastIdTest (id, blah) VALUES (?, ?)'
619
+ stmt.execute 5000000000, 5000
620
+ expect(stmt.last_id).to eql(5000000000)
621
+
622
+ stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
623
+ stmt.execute 5001
624
+ expect(stmt.last_id).to eql(5000000001)
625
+ end
626
+ end
627
+
628
+ context 'affected_rows' do
629
+ before :each do
630
+ @client.query 'USE test'
631
+ @client.query 'CREATE TABLE IF NOT EXISTS lastIdTest (`id` BIGINT NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))'
632
+ end
633
+
634
+ after :each do
635
+ @client.query 'DROP TABLE lastIdTest'
636
+ end
637
+
638
+ it 'should return number of rows affected by an insert' do
639
+ stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
640
+ expect(stmt.affected_rows).to eq 0
641
+ stmt.execute 1
642
+ expect(stmt.affected_rows).to eq 1
643
+ end
644
+
645
+ it 'should return number of rows affected by an update' do
646
+ stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
647
+ stmt.execute 1
648
+ expect(stmt.affected_rows).to eq 1
649
+ stmt.execute 2
650
+ expect(stmt.affected_rows).to eq 1
651
+
652
+ stmt = @client.prepare 'UPDATE lastIdTest SET blah=? WHERE blah=?'
653
+ stmt.execute 0, 1
654
+ expect(stmt.affected_rows).to eq 1
655
+ end
656
+
657
+ it 'should return number of rows affected by a delete' do
658
+ stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
659
+ stmt.execute 1
660
+ expect(stmt.affected_rows).to eq 1
661
+ stmt.execute 2
662
+ expect(stmt.affected_rows).to eq 1
663
+
664
+ stmt = @client.prepare 'DELETE FROM lastIdTest WHERE blah=?'
665
+ stmt.execute 1
666
+ expect(stmt.affected_rows).to eq 1
667
+ end
668
+ end
669
+
670
+ context 'close' do
671
+ it 'should free server resources' do
672
+ stmt = @client.prepare 'SELECT 1'
673
+ expect { stmt.close }.to change {
674
+ @client.query("SHOW STATUS LIKE 'Prepared_stmt_count'").first['Value'].to_i
675
+ }.by(-1)
676
+ end
677
+
678
+ it 'should raise an error on subsequent execution' do
679
+ stmt = @client.prepare 'SELECT 1'
680
+ stmt.close
681
+ expect { stmt.execute }.to raise_error(Mysql2::Error, /Invalid statement handle/)
682
+ end
683
+ end
684
+ end