mysql2 0.3.11-x86-mswin32-60 → 0.3.18-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +15 -0
  2. data/README.md +280 -75
  3. data/ext/mysql2/client.c +721 -206
  4. data/ext/mysql2/client.h +26 -12
  5. data/ext/mysql2/extconf.rb +120 -16
  6. data/ext/mysql2/infile.c +122 -0
  7. data/ext/mysql2/infile.h +1 -0
  8. data/ext/mysql2/mysql2_ext.h +7 -4
  9. data/ext/mysql2/mysql_enc_name_to_ruby.h +168 -0
  10. data/ext/mysql2/mysql_enc_to_ruby.h +246 -0
  11. data/ext/mysql2/result.c +230 -112
  12. data/ext/mysql2/result.h +4 -1
  13. data/lib/mysql2.rb +46 -3
  14. data/lib/mysql2/1.8/mysql2.so +0 -0
  15. data/lib/mysql2/1.9/mysql2.so +0 -0
  16. data/lib/mysql2/2.0/mysql2.so +0 -0
  17. data/lib/mysql2/2.1/mysql2.so +0 -0
  18. data/lib/mysql2/client.rb +48 -200
  19. data/lib/mysql2/console.rb +5 -0
  20. data/lib/mysql2/em.rb +22 -3
  21. data/lib/mysql2/error.rb +71 -6
  22. data/lib/mysql2/mysql2.rb +2 -0
  23. data/lib/mysql2/version.rb +1 -1
  24. data/spec/configuration.yml.example +17 -0
  25. data/spec/em/em_spec.rb +90 -5
  26. data/spec/my.cnf.example +9 -0
  27. data/spec/mysql2/client_spec.rb +501 -69
  28. data/spec/mysql2/error_spec.rb +58 -44
  29. data/spec/mysql2/result_spec.rb +191 -74
  30. data/spec/spec_helper.rb +23 -3
  31. data/spec/test_data +1 -0
  32. data/support/libmysql.def +219 -0
  33. data/support/mysql_enc_to_ruby.rb +82 -0
  34. data/support/ruby_enc_to_mysql.rb +61 -0
  35. data/vendor/README +654 -0
  36. data/vendor/libmysql.dll +0 -0
  37. metadata +86 -221
  38. data/.gitignore +0 -12
  39. data/.rspec +0 -3
  40. data/.rvmrc +0 -1
  41. data/.travis.yml +0 -7
  42. data/CHANGELOG.md +0 -244
  43. data/Gemfile +0 -3
  44. data/MIT-LICENSE +0 -20
  45. data/Rakefile +0 -5
  46. data/benchmark/active_record.rb +0 -51
  47. data/benchmark/active_record_threaded.rb +0 -42
  48. data/benchmark/allocations.rb +0 -33
  49. data/benchmark/escape.rb +0 -36
  50. data/benchmark/query_with_mysql_casting.rb +0 -80
  51. data/benchmark/query_without_mysql_casting.rb +0 -56
  52. data/benchmark/sequel.rb +0 -37
  53. data/benchmark/setup_db.rb +0 -119
  54. data/benchmark/threaded.rb +0 -44
  55. data/mysql2.gemspec +0 -29
  56. data/tasks/benchmarks.rake +0 -20
  57. data/tasks/compile.rake +0 -71
  58. data/tasks/rspec.rake +0 -16
  59. data/tasks/vendor_mysql.rake +0 -40
@@ -1,68 +1,82 @@
1
1
  # encoding: UTF-8
2
+
2
3
  require 'spec_helper'
3
4
 
4
5
  describe Mysql2::Error do
5
- before(:each) do
6
- @client = Mysql2::Client.new :encoding => "utf8"
7
- begin
8
- @client.query("HAHAHA")
9
- rescue Mysql2::Error => e
10
- @error = e
11
- end
6
+ let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) }
12
7
 
13
- @client2 = Mysql2::Client.new :encoding => "big5"
8
+ let :error do
14
9
  begin
15
- @client2.query("HAHAHA")
10
+ client.query("HAHAHA")
16
11
  rescue Mysql2::Error => e
17
- @error2 = e
12
+ error = e
13
+ ensure
14
+ client.close
18
15
  end
19
- end
20
16
 
21
- it "should respond to #error_number" do
22
- @error.should respond_to(:error_number)
17
+ error
23
18
  end
24
19
 
25
- it "should respond to #sql_state" do
26
- @error.should respond_to(:sql_state)
27
- end
20
+ it "responds to error_number and sql_state, with aliases" do
21
+ error.should respond_to(:error_number)
22
+ error.should respond_to(:sql_state)
28
23
 
29
- # Mysql gem compatibility
30
- it "should alias #error_number to #errno" do
31
- @error.should respond_to(:errno)
24
+ # Mysql gem compatibility
25
+ error.should respond_to(:errno)
26
+ error.should respond_to(:error)
32
27
  end
33
28
 
34
- it "should alias #message to #error" do
35
- @error.should respond_to(:error)
36
- end
29
+ if "".respond_to? :encoding
30
+ let :error do
31
+ client = Mysql2::Client.new(DatabaseCredentials['root'])
32
+ begin
33
+ client.query("\xE9\x80\xA0\xE5\xAD\x97")
34
+ rescue Mysql2::Error => e
35
+ error = e
36
+ ensure
37
+ client.close
38
+ end
37
39
 
38
- if RUBY_VERSION =~ /1.9/
39
- it "#message encoding should match the connection's encoding, or Encoding.default_internal if set" do
40
- if Encoding.default_internal.nil?
41
- @error.message.encoding.should eql(@client.encoding)
42
- @error2.message.encoding.should eql(@client2.encoding)
43
- else
44
- @error.message.encoding.should eql(Encoding.default_internal)
45
- @error2.message.encoding.should eql(Encoding.default_internal)
40
+ error
41
+ end
42
+
43
+ let :bad_err do
44
+ client = Mysql2::Client.new(DatabaseCredentials['root'])
45
+ begin
46
+ client.query("\xE5\xC6\x7D\x1F")
47
+ rescue Mysql2::Error => e
48
+ error = e
49
+ ensure
50
+ client.close
46
51
  end
52
+
53
+ error
47
54
  end
48
55
 
49
- it "#error encoding should match the connection's encoding, or Encoding.default_internal if set" do
50
- if Encoding.default_internal.nil?
51
- @error.error.encoding.should eql(@client.encoding)
52
- @error2.error.encoding.should eql(@client2.encoding)
53
- else
54
- @error.error.encoding.should eql(Encoding.default_internal)
55
- @error2.error.encoding.should eql(Encoding.default_internal)
56
+ it "returns error messages as UTF-8 by default" do
57
+ with_internal_encoding nil do
58
+ error.message.encoding.should eql(Encoding::UTF_8)
59
+ error.message.valid_encoding?
60
+
61
+ bad_err.message.encoding.should eql(Encoding::UTF_8)
62
+ bad_err.message.valid_encoding?
63
+
64
+ bad_err.message.should include("??}\u001F")
56
65
  end
57
66
  end
58
67
 
59
- it "#sql_state encoding should match the connection's encoding, or Encoding.default_internal if set" do
60
- if Encoding.default_internal.nil?
61
- @error.sql_state.encoding.should eql(@client.encoding)
62
- @error2.sql_state.encoding.should eql(@client2.encoding)
63
- else
64
- @error.sql_state.encoding.should eql(Encoding.default_internal)
65
- @error2.sql_state.encoding.should eql(Encoding.default_internal)
68
+ it "returns sql state as ASCII" do
69
+ error.sql_state.encoding.should eql(Encoding::US_ASCII)
70
+ error.sql_state.valid_encoding?
71
+ end
72
+
73
+ it "returns error messages and sql state in Encoding.default_internal if set" do
74
+ with_internal_encoding 'UTF-16LE' do
75
+ error.message.encoding.should eql(Encoding.default_internal)
76
+ error.message.valid_encoding?
77
+
78
+ bad_err.message.encoding.should eql(Encoding.default_internal)
79
+ bad_err.message.valid_encoding?
66
80
  end
67
81
  end
68
82
  end
@@ -2,10 +2,6 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe Mysql2::Result do
5
- before(:each) do
6
- @client = Mysql2::Client.new :host => "localhost", :username => "root", :database => 'test'
7
- end
8
-
9
5
  before(:each) do
10
6
  @result = @client.query "SELECT 1"
11
7
  end
@@ -73,11 +69,29 @@ describe Mysql2::Result do
73
69
  result = @client.query "SELECT 1", :cache_rows => false
74
70
  result.first.object_id.should_not eql(result.first.object_id)
75
71
  end
72
+
73
+ it "should yield different value for #first if streaming" do
74
+ result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
75
+ result.first.should_not eql(result.first)
76
+ end
77
+
78
+ it "should yield the same value for #first if streaming is disabled" do
79
+ result = @client.query "SELECT 1 UNION SELECT 2", :stream => false
80
+ result.first.should eql(result.first)
81
+ end
82
+
83
+ it "should throw an exception if we try to iterate twice when streaming is enabled" do
84
+ result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
85
+
86
+ expect {
87
+ result.each.to_a
88
+ result.each.to_a
89
+ }.to raise_exception(Mysql2::Error)
90
+ end
76
91
  end
77
92
 
78
93
  context "#fields" do
79
94
  before(:each) do
80
- @client.query "USE test"
81
95
  @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1")
82
96
  end
83
97
 
@@ -91,20 +105,70 @@ describe Mysql2::Result do
91
105
  end
92
106
  end
93
107
 
108
+ context "streaming" do
109
+ it "should maintain a count while streaming" do
110
+ result = @client.query('SELECT 1')
111
+
112
+ result.count.should eql(1)
113
+ result.each.to_a
114
+ result.count.should eql(1)
115
+ end
116
+
117
+ it "should set the actual count of rows after streaming" do
118
+ result = @client.query("SELECT * FROM mysql2_test", :stream => true, :cache_rows => false)
119
+ result.count.should eql(0)
120
+ result.each {|r| }
121
+ result.count.should eql(1)
122
+ end
123
+
124
+ it "should not yield nil at the end of streaming" do
125
+ result = @client.query('SELECT * FROM mysql2_test', :stream => true, :cache_rows => false)
126
+ result.each { |r| r.should_not be_nil}
127
+ end
128
+
129
+ it "#count should be zero for rows after streaming when there were no results" do
130
+ result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
131
+ result.count.should eql(0)
132
+ result.each.to_a
133
+ result.count.should eql(0)
134
+ end
135
+
136
+ it "should raise an exception if streaming ended due to a timeout" do
137
+ # Create an extra client instance, since we're going to time it out
138
+ client = Mysql2::Client.new DatabaseCredentials['root']
139
+ client.query "CREATE TEMPORARY TABLE streamingTest (val BINARY(255)) ENGINE=MEMORY"
140
+
141
+ # Insert enough records to force the result set into multiple reads
142
+ # (the BINARY type is used simply because it forces full width results)
143
+ 10000.times do |i|
144
+ client.query "INSERT INTO streamingTest (val) VALUES ('Foo #{i}')"
145
+ end
146
+
147
+ client.query "SET net_write_timeout = 1"
148
+ res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
149
+
150
+ lambda {
151
+ res.each_with_index do |row, i|
152
+ # Exhaust the first result packet then trigger a timeout
153
+ sleep 2 if i > 0 && i % 1000 == 0
154
+ end
155
+ }.should raise_error(Mysql2::Error, /Lost connection/)
156
+ end
157
+ end
158
+
94
159
  context "row data type mapping" do
95
160
  before(:each) do
96
- @client.query "USE test"
97
161
  @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
98
162
  end
99
163
 
100
164
  it "should return nil values for NULL and strings for everything else when :cast is false" do
101
165
  result = @client.query('SELECT null_test, tiny_int_test, bool_cast_test, int_test, date_test, enum_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast => false).first
102
166
  result["null_test"].should be_nil
103
- result["tiny_int_test"].should == "1"
104
- result["bool_cast_test"].should == "1"
105
- result["int_test"].should == "10"
106
- result["date_test"].should == "2010-04-04"
107
- result["enum_test"].should == "val1"
167
+ result["tiny_int_test"].should eql("1")
168
+ result["bool_cast_test"].should eql("1")
169
+ result["int_test"].should eql("10")
170
+ result["date_test"].should eql("2010-04-04")
171
+ result["enum_test"].should eql("val1")
108
172
  end
109
173
 
110
174
  it "should return nil for a NULL value" do
@@ -112,11 +176,16 @@ describe Mysql2::Result do
112
176
  @test_result['null_test'].should eql(nil)
113
177
  end
114
178
 
115
- it "should return Fixnum for a BIT value" do
179
+ it "should return String for a BIT(64) value" do
116
180
  @test_result['bit_test'].class.should eql(String)
117
181
  @test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005")
118
182
  end
119
183
 
184
+ it "should return String for a BIT(1) value" do
185
+ @test_result['single_bit_test'].class.should eql(String)
186
+ @test_result['single_bit_test'].should eql("\001")
187
+ end
188
+
120
189
  it "should return Fixnum for a TINYINT value" do
121
190
  [Fixnum, Bignum].should include(@test_result['tiny_int_test'].class)
122
191
  @test_result['tiny_int_test'].should eql(1)
@@ -127,11 +196,29 @@ describe Mysql2::Result do
127
196
  id1 = @client.last_id
128
197
  @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
129
198
  id2 = @client.last_id
199
+ @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
200
+ id3 = @client.last_id
130
201
 
131
202
  result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
132
203
  result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
204
+ result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
133
205
  result1.first['bool_cast_test'].should be_true
134
206
  result2.first['bool_cast_test'].should be_false
207
+ result3.first['bool_cast_test'].should be_true
208
+
209
+ @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
210
+ end
211
+
212
+ it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
213
+ @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
214
+ id1 = @client.last_id
215
+ @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
216
+ id2 = @client.last_id
217
+
218
+ result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
219
+ result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
220
+ result1.first['single_bit_test'].should be_true
221
+ result2.first['single_bit_test'].should be_false
135
222
 
136
223
  @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
137
224
  end
@@ -182,7 +269,7 @@ describe Mysql2::Result do
182
269
  end
183
270
 
184
271
  if 1.size == 4 # 32bit
185
- if RUBY_VERSION =~ /1.9/
272
+ unless RUBY_VERSION =~ /1.8/
186
273
  klass = Time
187
274
  else
188
275
  klass = DateTime
@@ -200,7 +287,7 @@ describe Mysql2::Result do
200
287
  r.first['test'].class.should eql(klass)
201
288
  end
202
289
  elsif 1.size == 8 # 64bit
203
- if RUBY_VERSION =~ /1.9/
290
+ unless RUBY_VERSION =~ /1.8/
204
291
  it "should return Time when timestamp is < 1901-12-13 20:45:52" do
205
292
  r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
206
293
  r.first['test'].class.should eql(Time)
@@ -248,26 +335,40 @@ describe Mysql2::Result do
248
335
  @test_result['enum_test'].should eql('val1')
249
336
  end
250
337
 
338
+ it "should raise an error given an invalid DATETIME" do
339
+ begin
340
+ @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each
341
+ rescue Mysql2::Error => e
342
+ error = e
343
+ end
344
+
345
+ error.message.should eql("Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
346
+ end
347
+
251
348
  if defined? Encoding
252
349
  context "string encoding for ENUM values" do
253
350
  it "should default to the connection's encoding if Encoding.default_internal is nil" do
254
- Encoding.default_internal = nil
255
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
256
- result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
257
-
258
- client2 = Mysql2::Client.new :encoding => 'ascii'
259
- client2.query "USE test"
260
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
261
- result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
351
+ with_internal_encoding nil do
352
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
353
+ result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
354
+
355
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
356
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
357
+ result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
358
+ client2.close
359
+ end
262
360
  end
263
361
 
264
362
  it "should use Encoding.default_internal" do
265
- Encoding.default_internal = Encoding.find('utf-8')
266
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
267
- result['enum_test'].encoding.should eql(Encoding.default_internal)
268
- Encoding.default_internal = Encoding.find('us-ascii')
269
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
270
- result['enum_test'].encoding.should eql(Encoding.default_internal)
363
+ with_internal_encoding 'utf-8' do
364
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
365
+ result['enum_test'].encoding.should eql(Encoding.default_internal)
366
+ end
367
+
368
+ with_internal_encoding 'us-ascii' do
369
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
370
+ result['enum_test'].encoding.should eql(Encoding.default_internal)
371
+ end
271
372
  end
272
373
  end
273
374
  end
@@ -280,23 +381,27 @@ describe Mysql2::Result do
280
381
  if defined? Encoding
281
382
  context "string encoding for SET values" do
282
383
  it "should default to the connection's encoding if Encoding.default_internal is nil" do
283
- Encoding.default_internal = nil
284
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
285
- result['set_test'].encoding.should eql(Encoding.find('utf-8'))
286
-
287
- client2 = Mysql2::Client.new :encoding => 'ascii'
288
- client2.query "USE test"
289
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
290
- result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
384
+ with_internal_encoding nil do
385
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
386
+ result['set_test'].encoding.should eql(Encoding.find('utf-8'))
387
+
388
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
389
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
390
+ result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
391
+ client2.close
392
+ end
291
393
  end
292
394
 
293
395
  it "should use Encoding.default_internal" do
294
- Encoding.default_internal = Encoding.find('utf-8')
295
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
296
- result['set_test'].encoding.should eql(Encoding.default_internal)
297
- Encoding.default_internal = Encoding.find('us-ascii')
298
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
299
- result['set_test'].encoding.should eql(Encoding.default_internal)
396
+ with_internal_encoding 'utf-8' do
397
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
398
+ result['set_test'].encoding.should eql(Encoding.default_internal)
399
+ end
400
+
401
+ with_internal_encoding 'us-ascii' do
402
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
403
+ result['set_test'].encoding.should eql(Encoding.default_internal)
404
+ end
300
405
  end
301
406
  end
302
407
  end
@@ -309,18 +414,22 @@ describe Mysql2::Result do
309
414
  if defined? Encoding
310
415
  context "string encoding for BINARY values" do
311
416
  it "should default to binary if Encoding.default_internal is nil" do
312
- Encoding.default_internal = nil
313
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
314
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
417
+ with_internal_encoding nil do
418
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
419
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
420
+ end
315
421
  end
316
422
 
317
423
  it "should not use Encoding.default_internal" do
318
- Encoding.default_internal = Encoding.find('utf-8')
319
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
320
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
321
- Encoding.default_internal = Encoding.find('us-ascii')
322
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
323
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
424
+ with_internal_encoding 'utf-8' do
425
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
426
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
427
+ end
428
+
429
+ with_internal_encoding 'us-ascii' do
430
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
431
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
432
+ end
324
433
  end
325
434
  end
326
435
  end
@@ -347,38 +456,46 @@ describe Mysql2::Result do
347
456
  context "string encoding for #{type} values" do
348
457
  if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
349
458
  it "should default to binary if Encoding.default_internal is nil" do
350
- Encoding.default_internal = nil
351
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
352
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
459
+ with_internal_encoding nil do
460
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
461
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
462
+ end
353
463
  end
354
464
 
355
465
  it "should not use Encoding.default_internal" do
356
- Encoding.default_internal = Encoding.find('utf-8')
357
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
358
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
359
- Encoding.default_internal = Encoding.find('us-ascii')
360
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
361
- result['binary_test'].encoding.should eql(Encoding.find('binary'))
466
+ with_internal_encoding 'utf-8' do
467
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
468
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
469
+ end
470
+
471
+ with_internal_encoding 'us-ascii' do
472
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
473
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
474
+ end
362
475
  end
363
476
  else
364
477
  it "should default to utf-8 if Encoding.default_internal is nil" do
365
- Encoding.default_internal = nil
366
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
367
- result[field].encoding.should eql(Encoding.find('utf-8'))
368
-
369
- client2 = Mysql2::Client.new :encoding => 'ascii'
370
- client2.query "USE test"
371
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
372
- result[field].encoding.should eql(Encoding.find('us-ascii'))
478
+ with_internal_encoding nil do
479
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
480
+ result[field].encoding.should eql(Encoding.find('utf-8'))
481
+
482
+ client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
483
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
484
+ result[field].encoding.should eql(Encoding.find('us-ascii'))
485
+ client2.close
486
+ end
373
487
  end
374
488
 
375
489
  it "should use Encoding.default_internal" do
376
- Encoding.default_internal = Encoding.find('utf-8')
377
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
378
- result[field].encoding.should eql(Encoding.default_internal)
379
- Encoding.default_internal = Encoding.find('us-ascii')
380
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
381
- result[field].encoding.should eql(Encoding.default_internal)
490
+ with_internal_encoding 'utf-8' do
491
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
492
+ result[field].encoding.should eql(Encoding.default_internal)
493
+ end
494
+
495
+ with_internal_encoding 'us-ascii' do
496
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
497
+ result[field].encoding.should eql(Encoding.default_internal)
498
+ end
382
499
  end
383
500
  end
384
501
  end