mysql2 0.2.24 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +12 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/CHANGELOG.md +148 -0
  5. data/Gemfile +3 -0
  6. data/README.rdoc +257 -0
  7. data/Rakefile +5 -0
  8. data/benchmark/active_record.rb +51 -0
  9. data/benchmark/active_record_threaded.rb +42 -0
  10. data/benchmark/allocations.rb +33 -0
  11. data/benchmark/escape.rb +36 -0
  12. data/benchmark/query_with_mysql_casting.rb +80 -0
  13. data/benchmark/query_without_mysql_casting.rb +47 -0
  14. data/benchmark/sequel.rb +37 -0
  15. data/benchmark/setup_db.rb +119 -0
  16. data/benchmark/threaded.rb +44 -0
  17. data/ext/mysql2/client.c +272 -849
  18. data/ext/mysql2/client.h +12 -27
  19. data/ext/mysql2/extconf.rb +14 -72
  20. data/ext/mysql2/mysql2_ext.h +4 -7
  21. data/ext/mysql2/result.c +123 -319
  22. data/ext/mysql2/result.h +1 -4
  23. data/lib/active_record/connection_adapters/em_mysql2_adapter.rb +64 -0
  24. data/lib/active_record/fiber_patches.rb +104 -0
  25. data/lib/mysql2.rb +5 -20
  26. data/lib/mysql2/client.rb +200 -50
  27. data/lib/mysql2/em.rb +3 -13
  28. data/lib/mysql2/em_fiber.rb +31 -0
  29. data/lib/mysql2/error.rb +6 -71
  30. data/lib/mysql2/version.rb +2 -2
  31. data/mysql2.gemspec +32 -0
  32. data/spec/em/em_fiber_spec.rb +22 -0
  33. data/spec/em/em_spec.rb +9 -74
  34. data/spec/mysql2/client_spec.rb +126 -593
  35. data/spec/mysql2/error_spec.rb +44 -58
  36. data/spec/mysql2/result_spec.rb +85 -257
  37. data/spec/spec_helper.rb +3 -24
  38. data/tasks/benchmarks.rake +20 -0
  39. data/tasks/compile.rake +71 -0
  40. data/tasks/rspec.rake +16 -0
  41. data/tasks/vendor_mysql.rake +40 -0
  42. metadata +179 -92
  43. checksums.yaml +0 -7
  44. data/README.md +0 -524
  45. data/ext/mysql2/infile.c +0 -122
  46. data/ext/mysql2/infile.h +0 -1
  47. data/ext/mysql2/mysql_enc_name_to_ruby.h +0 -168
  48. data/ext/mysql2/mysql_enc_to_ruby.h +0 -246
  49. data/ext/mysql2/wait_for_single_fd.h +0 -36
  50. data/lib/active_record/connection_adapters/mysql2_adapter.rb +0 -635
  51. data/lib/arel/engines/sql/compilers/mysql2_compiler.rb +0 -11
  52. data/lib/mysql2/console.rb +0 -5
  53. data/spec/configuration.yml.example +0 -17
  54. data/spec/my.cnf.example +0 -9
  55. data/spec/test_data +0 -1
  56. data/support/mysql_enc_to_ruby.rb +0 -82
  57. data/support/ruby_enc_to_mysql.rb +0 -61
@@ -1,82 +1,68 @@
1
1
  # encoding: UTF-8
2
-
3
2
  require 'spec_helper'
4
3
 
5
4
  describe Mysql2::Error do
6
- let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) }
7
-
8
- let :error do
5
+ before(:each) do
6
+ @client = Mysql2::Client.new :encoding => "utf8"
9
7
  begin
10
- client.query("HAHAHA")
8
+ @client.query("HAHAHA")
11
9
  rescue Mysql2::Error => e
12
- error = e
13
- ensure
14
- client.close
10
+ @error = e
15
11
  end
16
12
 
17
- error
13
+ @client2 = Mysql2::Client.new :encoding => "big5"
14
+ begin
15
+ @client2.query("HAHAHA")
16
+ rescue Mysql2::Error => e
17
+ @error2 = e
18
+ end
18
19
  end
19
20
 
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)
21
+ it "should respond to #error_number" do
22
+ @error.should respond_to(:error_number)
23
+ end
23
24
 
24
- # Mysql gem compatibility
25
- error.should respond_to(:errno)
26
- error.should respond_to(:error)
25
+ it "should respond to #sql_state" do
26
+ @error.should respond_to(:sql_state)
27
27
  end
28
28
 
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
29
+ # Mysql gem compatibility
30
+ it "should alias #error_number to #errno" do
31
+ @error.should respond_to(:errno)
32
+ end
39
33
 
40
- error
41
- end
34
+ it "should alias #message to #error" do
35
+ @error.should respond_to(:error)
36
+ end
42
37
 
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
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)
51
46
  end
52
-
53
- error
54
47
  end
55
48
 
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")
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)
65
56
  end
66
57
  end
67
58
 
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?
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)
80
66
  end
81
67
  end
82
68
  end
@@ -2,6 +2,10 @@
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
+
5
9
  before(:each) do
6
10
  @result = @client.query "SELECT 1"
7
11
  end
@@ -24,24 +28,6 @@ describe Mysql2::Result do
24
28
  }.should_not raise_error(Mysql2::Error)
25
29
  end
26
30
 
27
- it "should respond to #count, which is aliased as #size" do
28
- r = @client.query "SELECT 1"
29
- r.should respond_to :count
30
- r.should respond_to :size
31
- end
32
-
33
- it "should be able to return the number of rows in the result set" do
34
- r = @client.query "SELECT 1"
35
- r.count.should eql(1)
36
- r.size.should eql(1)
37
- end
38
-
39
- context "metadata queries" do
40
- it "should show tables" do
41
- @result = @client.query "SHOW TABLES"
42
- end
43
- end
44
-
45
31
  context "#each" do
46
32
  it "should yield rows as hash's" do
47
33
  @result.each do |row|
@@ -69,29 +55,11 @@ describe Mysql2::Result do
69
55
  result = @client.query "SELECT 1", :cache_rows => false
70
56
  result.first.object_id.should_not eql(result.first.object_id)
71
57
  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
91
58
  end
92
59
 
93
60
  context "#fields" do
94
61
  before(:each) do
62
+ @client.query "USE test"
95
63
  @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1")
96
64
  end
97
65
 
@@ -105,87 +73,22 @@ describe Mysql2::Result do
105
73
  end
106
74
  end
107
75
 
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))"
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
-
159
76
  context "row data type mapping" do
160
77
  before(:each) do
78
+ @client.query "USE test"
161
79
  @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
162
80
  end
163
81
 
164
- it "should return nil values for NULL and strings for everything else when :cast is false" do
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
166
- result["null_test"].should be_nil
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")
172
- end
173
-
174
82
  it "should return nil for a NULL value" do
175
83
  @test_result['null_test'].class.should eql(NilClass)
176
84
  @test_result['null_test'].should eql(nil)
177
85
  end
178
86
 
179
- it "should return String for a BIT(64) value" do
87
+ it "should return Fixnum for a BIT value" do
180
88
  @test_result['bit_test'].class.should eql(String)
181
89
  @test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005")
182
90
  end
183
91
 
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
-
189
92
  it "should return Fixnum for a TINYINT value" do
190
93
  [Fixnum, Bignum].should include(@test_result['tiny_int_test'].class)
191
94
  @test_result['tiny_int_test'].should eql(1)
@@ -196,29 +99,11 @@ describe Mysql2::Result do
196
99
  id1 = @client.last_id
197
100
  @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
198
101
  id2 = @client.last_id
199
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
200
- id3 = @client.last_id
201
102
 
202
103
  result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
203
104
  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
205
105
  result1.first['bool_cast_test'].should be_true
206
106
  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
222
107
 
223
108
  @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
224
109
  end
@@ -265,69 +150,42 @@ describe Mysql2::Result do
265
150
 
266
151
  it "should return Time for a DATETIME value when within the supported range" do
267
152
  @test_result['date_time_test'].class.should eql(Time)
268
- @test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2010-04-04 11:44:00')
153
+ @test_result['date_time_test'].strftime("%F %T").should eql('2010-04-04 11:44:00')
269
154
  end
270
155
 
271
- if 1.size == 4 # 32bit
272
- unless RUBY_VERSION =~ /1.8/
273
- klass = Time
156
+ it "should return DateTime for a DATETIME value when outside the supported range, Time if otherwise" do
157
+ if RUBY_PLATFORM =~ /mswin/
158
+ inside_year = 1970
159
+ outside_year = inside_year-1
274
160
  else
275
- klass = DateTime
161
+ inside_year = 1902
162
+ outside_year = inside_year-1
276
163
  end
277
-
278
- it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
279
- # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
280
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
281
- r.first['test'].class.should eql(klass)
282
- end
283
-
284
- it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
285
- # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
286
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
287
- r.first['test'].class.should eql(klass)
288
- end
289
- elsif 1.size == 8 # 64bit
290
- unless RUBY_VERSION =~ /1.8/
291
- it "should return Time when timestamp is < 1901-12-13 20:45:52" do
292
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
293
- r.first['test'].class.should eql(Time)
294
- end
295
-
296
- it "should return Time when timestamp is > 2038-01-19T03:14:07" do
297
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
298
- r.first['test'].class.should eql(Time)
299
- end
164
+ r = @client.query("SELECT CAST('#{inside_year}-1-1 01:01:01' AS DATETIME) as test")
165
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
166
+ klass = DateTime
300
167
  else
301
- it "should return Time when timestamp is > 0138-12-31 11:59:59" do
302
- r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
303
- r.first['test'].class.should eql(Time)
304
- end
305
-
306
- it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
307
- r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
308
- r.first['test'].class.should eql(DateTime)
309
- end
310
-
311
- it "should return Time when timestamp is > 2038-01-19T03:14:07" do
312
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
313
- r.first['test'].class.should eql(Time)
314
- end
168
+ klass = Time
315
169
  end
170
+ r.first['test'].class.should eql(klass)
171
+
172
+ r = @client.query("SELECT CAST('#{outside_year}-1-1 01:01:01' AS DATETIME) as test")
173
+ r.first['test'].class.should eql(DateTime)
316
174
  end
317
175
 
318
176
  it "should return Time for a TIMESTAMP value when within the supported range" do
319
177
  @test_result['timestamp_test'].class.should eql(Time)
320
- @test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2010-04-04 11:44:00')
178
+ @test_result['timestamp_test'].strftime("%F %T").should eql('2010-04-04 11:44:00')
321
179
  end
322
180
 
323
181
  it "should return Time for a TIME value" do
324
182
  @test_result['time_test'].class.should eql(Time)
325
- @test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S").should eql('2000-01-01 11:44:00')
183
+ @test_result['time_test'].strftime("%F %T").should eql('2000-01-01 11:44:00')
326
184
  end
327
185
 
328
186
  it "should return Date for a DATE value" do
329
187
  @test_result['date_test'].class.should eql(Date)
330
- @test_result['date_test'].strftime("%Y-%m-%d").should eql('2010-04-04')
188
+ @test_result['date_test'].strftime("%F").should eql('2010-04-04')
331
189
  end
332
190
 
333
191
  it "should return String for an ENUM value" do
@@ -335,40 +193,26 @@ describe Mysql2::Result do
335
193
  @test_result['enum_test'].should eql('val1')
336
194
  end
337
195
 
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
-
348
196
  if defined? Encoding
349
197
  context "string encoding for ENUM values" do
350
198
  it "should default to the connection's encoding if Encoding.default_internal is nil" do
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
199
+ Encoding.default_internal = nil
200
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
201
+ result['enum_test'].encoding.should eql(Encoding.find('utf-8'))
202
+
203
+ client2 = Mysql2::Client.new :encoding => 'ascii'
204
+ client2.query "USE test"
205
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
206
+ result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
360
207
  end
361
208
 
362
209
  it "should use Encoding.default_internal" do
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
210
+ Encoding.default_internal = Encoding.find('utf-8')
211
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
212
+ result['enum_test'].encoding.should eql(Encoding.default_internal)
213
+ Encoding.default_internal = Encoding.find('us-ascii')
214
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
215
+ result['enum_test'].encoding.should eql(Encoding.default_internal)
372
216
  end
373
217
  end
374
218
  end
@@ -381,27 +225,23 @@ describe Mysql2::Result do
381
225
  if defined? Encoding
382
226
  context "string encoding for SET values" do
383
227
  it "should default to the connection's encoding if Encoding.default_internal is nil" do
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
228
+ Encoding.default_internal = nil
229
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
230
+ result['set_test'].encoding.should eql(Encoding.find('utf-8'))
231
+
232
+ client2 = Mysql2::Client.new :encoding => 'ascii'
233
+ client2.query "USE test"
234
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
235
+ result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
393
236
  end
394
237
 
395
238
  it "should use Encoding.default_internal" do
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
239
+ Encoding.default_internal = Encoding.find('utf-8')
240
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
241
+ result['set_test'].encoding.should eql(Encoding.default_internal)
242
+ Encoding.default_internal = Encoding.find('us-ascii')
243
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
244
+ result['set_test'].encoding.should eql(Encoding.default_internal)
405
245
  end
406
246
  end
407
247
  end
@@ -414,22 +254,18 @@ describe Mysql2::Result do
414
254
  if defined? Encoding
415
255
  context "string encoding for BINARY values" do
416
256
  it "should default to binary if Encoding.default_internal is nil" do
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
257
+ Encoding.default_internal = nil
258
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
259
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
421
260
  end
422
261
 
423
262
  it "should not use Encoding.default_internal" do
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
263
+ Encoding.default_internal = Encoding.find('utf-8')
264
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
265
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
266
+ Encoding.default_internal = Encoding.find('us-ascii')
267
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
268
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
433
269
  end
434
270
  end
435
271
  end
@@ -456,46 +292,38 @@ describe Mysql2::Result do
456
292
  context "string encoding for #{type} values" do
457
293
  if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
458
294
  it "should default to binary if Encoding.default_internal is nil" do
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
295
+ Encoding.default_internal = nil
296
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
297
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
463
298
  end
464
299
 
465
300
  it "should not use Encoding.default_internal" do
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
301
+ Encoding.default_internal = Encoding.find('utf-8')
302
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
303
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
304
+ Encoding.default_internal = Encoding.find('us-ascii')
305
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
306
+ result['binary_test'].encoding.should eql(Encoding.find('binary'))
475
307
  end
476
308
  else
477
309
  it "should default to utf-8 if Encoding.default_internal is nil" do
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
310
+ Encoding.default_internal = nil
311
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
312
+ result[field].encoding.should eql(Encoding.find('utf-8'))
313
+
314
+ client2 = Mysql2::Client.new :encoding => 'ascii'
315
+ client2.query "USE test"
316
+ result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
317
+ result[field].encoding.should eql(Encoding.find('us-ascii'))
487
318
  end
488
319
 
489
320
  it "should use Encoding.default_internal" do
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
321
+ Encoding.default_internal = Encoding.find('utf-8')
322
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
323
+ result[field].encoding.should eql(Encoding.default_internal)
324
+ Encoding.default_internal = Encoding.find('us-ascii')
325
+ result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
326
+ result[field].encoding.should eql(Encoding.default_internal)
499
327
  end
500
328
  end
501
329
  end