mysql2 0.4.2 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +231 -86
  3. data/ext/mysql2/client.c +527 -128
  4. data/ext/mysql2/client.h +11 -52
  5. data/ext/mysql2/extconf.rb +100 -21
  6. data/ext/mysql2/mysql2_ext.c +8 -2
  7. data/ext/mysql2/mysql2_ext.h +21 -8
  8. data/ext/mysql2/mysql_enc_name_to_ruby.h +60 -56
  9. data/ext/mysql2/mysql_enc_to_ruby.h +64 -3
  10. data/ext/mysql2/result.c +333 -109
  11. data/ext/mysql2/result.h +1 -0
  12. data/ext/mysql2/statement.c +247 -90
  13. data/ext/mysql2/statement.h +0 -2
  14. data/ext/mysql2/wait_for_single_fd.h +2 -1
  15. data/lib/mysql2/client.rb +71 -31
  16. data/lib/mysql2/em.rb +2 -4
  17. data/lib/mysql2/error.rb +52 -22
  18. data/lib/mysql2/result.rb +2 -0
  19. data/lib/mysql2/statement.rb +3 -11
  20. data/lib/mysql2/version.rb +1 -1
  21. data/lib/mysql2.rb +19 -15
  22. data/support/3A79BD29.asc +49 -0
  23. data/support/5072E1F5.asc +432 -0
  24. data/support/C74CD1D8.asc +104 -0
  25. data/support/mysql_enc_to_ruby.rb +8 -3
  26. data/support/ruby_enc_to_mysql.rb +7 -5
  27. metadata +19 -61
  28. data/examples/eventmachine.rb +0 -21
  29. data/examples/threaded.rb +0 -18
  30. data/spec/configuration.yml.example +0 -17
  31. data/spec/em/em_spec.rb +0 -135
  32. data/spec/my.cnf.example +0 -9
  33. data/spec/mysql2/client_spec.rb +0 -939
  34. data/spec/mysql2/error_spec.rb +0 -84
  35. data/spec/mysql2/result_spec.rb +0 -510
  36. data/spec/mysql2/statement_spec.rb +0 -684
  37. data/spec/rcov.opts +0 -3
  38. data/spec/spec_helper.rb +0 -94
  39. data/spec/ssl/ca-cert.pem +0 -17
  40. data/spec/ssl/ca-key.pem +0 -27
  41. data/spec/ssl/ca.cnf +0 -22
  42. data/spec/ssl/cert.cnf +0 -22
  43. data/spec/ssl/client-cert.pem +0 -17
  44. data/spec/ssl/client-key.pem +0 -27
  45. data/spec/ssl/client-req.pem +0 -15
  46. data/spec/ssl/gen_certs.sh +0 -48
  47. data/spec/ssl/pkcs8-client-key.pem +0 -28
  48. data/spec/ssl/pkcs8-server-key.pem +0 -28
  49. data/spec/ssl/server-cert.pem +0 -17
  50. data/spec/ssl/server-key.pem +0 -27
  51. data/spec/ssl/server-req.pem +0 -15
  52. data/spec/test_data +0 -1
@@ -1,84 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe Mysql2::Error do
6
- let(:client) { Mysql2::Client.new(DatabaseCredentials['root']) }
7
-
8
- let(:error) do
9
- begin
10
- client.query("HAHAHA")
11
- rescue Mysql2::Error => e
12
- error = e
13
- end
14
-
15
- error
16
- end
17
-
18
- it "responds to error_number and sql_state, with aliases" do
19
- expect(error).to respond_to(:error_number)
20
- expect(error).to respond_to(:sql_state)
21
-
22
- # Mysql gem compatibility
23
- expect(error).to respond_to(:errno)
24
- expect(error).to respond_to(:error)
25
- end
26
-
27
- context 'encoding' do
28
- let(:valid_utf8) { '造字' }
29
- let(:error) do
30
- begin
31
- client.query(valid_utf8)
32
- rescue Mysql2::Error => e
33
- e
34
- end
35
- end
36
-
37
- let(:invalid_utf8) { "\xE5\xC6\x7D\x1F" }
38
- let(:bad_err) do
39
- begin
40
- client.query(invalid_utf8)
41
- rescue Mysql2::Error => e
42
- e
43
- end
44
- end
45
-
46
- before do
47
- pending('String#encoding is not defined') unless String.public_method_defined?(:encoding)
48
-
49
- # sanity check
50
- expect(valid_utf8.encoding).to eql(Encoding::UTF_8)
51
- expect(valid_utf8).to be_valid_encoding
52
-
53
- expect(invalid_utf8.encoding).to eql(Encoding::UTF_8)
54
- expect(invalid_utf8).to_not be_valid_encoding
55
- end
56
-
57
- it "returns error messages as UTF-8 by default" do
58
- with_internal_encoding nil do
59
- expect(error.message.encoding).to eql(Encoding::UTF_8)
60
- expect(error.message).to be_valid_encoding
61
-
62
- expect(bad_err.message.encoding).to eql(Encoding::UTF_8)
63
- expect(bad_err.message).to be_valid_encoding
64
-
65
- expect(bad_err.message).to include("??}\u001F")
66
- end
67
- end
68
-
69
- it "returns sql state as ASCII" do
70
- expect(error.sql_state.encoding).to eql(Encoding::US_ASCII)
71
- expect(error.sql_state).to be_valid_encoding
72
- end
73
-
74
- it "returns error messages and sql state in Encoding.default_internal if set" do
75
- with_internal_encoding Encoding::UTF_16LE do
76
- expect(error.message.encoding).to eql(Encoding.default_internal)
77
- expect(error.message).to be_valid_encoding
78
-
79
- expect(bad_err.message.encoding).to eql(Encoding.default_internal)
80
- expect(bad_err.message).to be_valid_encoding
81
- end
82
- end
83
- end
84
- end
@@ -1,510 +0,0 @@
1
- # encoding: UTF-8
2
- require 'spec_helper'
3
-
4
- RSpec.describe Mysql2::Result do
5
- before(:each) do
6
- @result = @client.query "SELECT 1"
7
- end
8
-
9
- it "should raise a TypeError exception when it doesn't wrap a result set" do
10
- r = Mysql2::Result.new
11
- expect { r.count }.to raise_error(TypeError)
12
- expect { r.fields }.to raise_error(TypeError)
13
- expect { r.size }.to raise_error(TypeError)
14
- expect { r.each }.to raise_error(TypeError)
15
- end
16
-
17
- it "should have included Enumerable" do
18
- expect(Mysql2::Result.ancestors.include?(Enumerable)).to be true
19
- end
20
-
21
- it "should respond to #each" do
22
- expect(@result).to respond_to(:each)
23
- end
24
-
25
- it "should raise a Mysql2::Error exception upon a bad query" do
26
- expect {
27
- @client.query "bad sql"
28
- }.to raise_error(Mysql2::Error)
29
-
30
- expect {
31
- @client.query "SELECT 1"
32
- }.not_to raise_error
33
- end
34
-
35
- it "should respond to #count, which is aliased as #size" do
36
- r = @client.query "SELECT 1"
37
- expect(r).to respond_to :count
38
- expect(r).to respond_to :size
39
- end
40
-
41
- it "should be able to return the number of rows in the result set" do
42
- r = @client.query "SELECT 1"
43
- expect(r.count).to eql(1)
44
- expect(r.size).to eql(1)
45
- end
46
-
47
- context "metadata queries" do
48
- it "should show tables" do
49
- @result = @client.query "SHOW TABLES"
50
- end
51
- end
52
-
53
- context "#each" do
54
- it "should yield rows as hash's" do
55
- @result.each do |row|
56
- expect(row).to be_an_instance_of(Hash)
57
- end
58
- end
59
-
60
- it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
61
- @result.each(:symbolize_keys => true) do |row|
62
- expect(row.keys.first).to be_an_instance_of(Symbol)
63
- end
64
- end
65
-
66
- it "should be able to return results as an array" do
67
- @result.each(:as => :array) do |row|
68
- expect(row).to be_an_instance_of(Array)
69
- end
70
- end
71
-
72
- it "should cache previously yielded results by default" do
73
- expect(@result.first.object_id).to eql(@result.first.object_id)
74
- end
75
-
76
- it "should not cache previously yielded results if cache_rows is disabled" do
77
- result = @client.query "SELECT 1", :cache_rows => false
78
- expect(result.first.object_id).not_to eql(result.first.object_id)
79
- end
80
-
81
- it "should yield different value for #first if streaming" do
82
- result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
83
- expect(result.first).not_to eql(result.first)
84
- end
85
-
86
- it "should yield the same value for #first if streaming is disabled" do
87
- result = @client.query "SELECT 1 UNION SELECT 2", :stream => false
88
- expect(result.first).to eql(result.first)
89
- end
90
-
91
- it "should throw an exception if we try to iterate twice when streaming is enabled" do
92
- result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
93
-
94
- expect {
95
- result.each.to_a
96
- result.each.to_a
97
- }.to raise_exception(Mysql2::Error)
98
- end
99
- end
100
-
101
- context "#fields" do
102
- before(:each) do
103
- @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1")
104
- end
105
-
106
- it "method should exist" do
107
- expect(@test_result).to respond_to(:fields)
108
- end
109
-
110
- it "should return an array of field names in proper order" do
111
- result = @client.query "SELECT 'a', 'b', 'c'"
112
- expect(result.fields).to eql(%w(a b c))
113
- end
114
- end
115
-
116
- context "streaming" do
117
- it "should maintain a count while streaming" do
118
- result = @client.query('SELECT 1', :stream => true, :cache_rows => false)
119
- expect(result.count).to eql(0)
120
- result.each.to_a
121
- expect(result.count).to eql(1)
122
- end
123
-
124
- it "should retain the count when mixing first and each" do
125
- result = @client.query("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false)
126
- expect(result.count).to eql(0)
127
- result.first
128
- expect(result.count).to eql(1)
129
- result.each.to_a
130
- expect(result.count).to eql(2)
131
- end
132
-
133
- it "should not yield nil at the end of streaming" do
134
- result = @client.query('SELECT * FROM mysql2_test', :stream => true, :cache_rows => false)
135
- result.each { |r| expect(r).not_to be_nil }
136
- end
137
-
138
- it "#count should be zero for rows after streaming when there were no results" do
139
- @client.query "USE test"
140
- result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
141
- expect(result.count).to eql(0)
142
- result.each.to_a
143
- expect(result.count).to eql(0)
144
- end
145
-
146
- it "should raise an exception if streaming ended due to a timeout" do
147
- # Create an extra client instance, since we're going to time it out
148
- client = Mysql2::Client.new DatabaseCredentials['root']
149
- client.query "CREATE TEMPORARY TABLE streamingTest (val BINARY(255)) ENGINE=MEMORY"
150
-
151
- # Insert enough records to force the result set into multiple reads
152
- # (the BINARY type is used simply because it forces full width results)
153
- 10000.times do |i|
154
- client.query "INSERT INTO streamingTest (val) VALUES ('Foo #{i}')"
155
- end
156
-
157
- client.query "SET net_write_timeout = 1"
158
- res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
159
-
160
- expect {
161
- res.each_with_index do |_, i|
162
- # Exhaust the first result packet then trigger a timeout
163
- sleep 2 if i > 0 && i % 1000 == 0
164
- end
165
- }.to raise_error(Mysql2::Error, /Lost connection/)
166
- end
167
- end
168
-
169
- context "row data type mapping" do
170
- before(:each) do
171
- @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
172
- end
173
-
174
- it "should return nil values for NULL and strings for everything else when :cast is false" do
175
- 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
176
- expect(result["null_test"]).to be_nil
177
- expect(result["tiny_int_test"]).to eql("1")
178
- expect(result["bool_cast_test"]).to eql("1")
179
- expect(result["int_test"]).to eql("10")
180
- expect(result["date_test"]).to eql("2010-04-04")
181
- expect(result["enum_test"]).to eql("val1")
182
- end
183
-
184
- it "should return nil for a NULL value" do
185
- expect(@test_result['null_test']).to be_an_instance_of(NilClass)
186
- expect(@test_result['null_test']).to eql(nil)
187
- end
188
-
189
- it "should return String for a BIT(64) value" do
190
- expect(@test_result['bit_test']).to be_an_instance_of(String)
191
- expect(@test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
192
- end
193
-
194
- it "should return String for a BIT(1) value" do
195
- expect(@test_result['single_bit_test']).to be_an_instance_of(String)
196
- expect(@test_result['single_bit_test']).to eql("\001")
197
- end
198
-
199
- it "should return Fixnum for a TINYINT value" do
200
- expect([Fixnum, Bignum]).to include(@test_result['tiny_int_test'].class)
201
- expect(@test_result['tiny_int_test']).to eql(1)
202
- end
203
-
204
- it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
205
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (1)'
206
- id1 = @client.last_id
207
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (0)'
208
- id2 = @client.last_id
209
- @client.query 'INSERT INTO mysql2_test (bool_cast_test) VALUES (-1)'
210
- id3 = @client.last_id
211
-
212
- result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
213
- result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
214
- result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
215
- expect(result1.first['bool_cast_test']).to be true
216
- expect(result2.first['bool_cast_test']).to be false
217
- expect(result3.first['bool_cast_test']).to be true
218
-
219
- @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
220
- end
221
-
222
- it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do
223
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)'
224
- id1 = @client.last_id
225
- @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)'
226
- id2 = @client.last_id
227
-
228
- result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
229
- result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
230
- expect(result1.first['single_bit_test']).to be true
231
- expect(result2.first['single_bit_test']).to be false
232
-
233
- @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
234
- end
235
-
236
- it "should return Fixnum for a SMALLINT value" do
237
- expect([Fixnum, Bignum]).to include(@test_result['small_int_test'].class)
238
- expect(@test_result['small_int_test']).to eql(10)
239
- end
240
-
241
- it "should return Fixnum for a MEDIUMINT value" do
242
- expect([Fixnum, Bignum]).to include(@test_result['medium_int_test'].class)
243
- expect(@test_result['medium_int_test']).to eql(10)
244
- end
245
-
246
- it "should return Fixnum for an INT value" do
247
- expect([Fixnum, Bignum]).to include(@test_result['int_test'].class)
248
- expect(@test_result['int_test']).to eql(10)
249
- end
250
-
251
- it "should return Fixnum for a BIGINT value" do
252
- expect([Fixnum, Bignum]).to include(@test_result['big_int_test'].class)
253
- expect(@test_result['big_int_test']).to eql(10)
254
- end
255
-
256
- it "should return Fixnum for a YEAR value" do
257
- expect([Fixnum, Bignum]).to include(@test_result['year_test'].class)
258
- expect(@test_result['year_test']).to eql(2009)
259
- end
260
-
261
- it "should return BigDecimal for a DECIMAL value" do
262
- expect(@test_result['decimal_test']).to be_an_instance_of(BigDecimal)
263
- expect(@test_result['decimal_test']).to eql(10.3)
264
- end
265
-
266
- it "should return Float for a FLOAT value" do
267
- expect(@test_result['float_test']).to be_an_instance_of(Float)
268
- expect(@test_result['float_test']).to eql(10.3)
269
- end
270
-
271
- it "should return Float for a DOUBLE value" do
272
- expect(@test_result['double_test']).to be_an_instance_of(Float)
273
- expect(@test_result['double_test']).to eql(10.3)
274
- end
275
-
276
- it "should return Time for a DATETIME value when within the supported range" do
277
- expect(@test_result['date_time_test']).to be_an_instance_of(Time)
278
- expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
279
- end
280
-
281
- if 1.size == 4 # 32bit
282
- klass = if RUBY_VERSION =~ /1.8/
283
- DateTime
284
- else
285
- Time
286
- end
287
-
288
- it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
289
- # 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
290
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
291
- expect(r.first['test']).to be_an_instance_of(klass)
292
- end
293
-
294
- it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
295
- # 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
296
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
297
- expect(r.first['test']).to be_an_instance_of(klass)
298
- end
299
- elsif 1.size == 8 # 64bit
300
- if RUBY_VERSION =~ /1.8/
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
- expect(r.first['test']).to be_an_instance_of(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
- expect(r.first['test']).to be_an_instance_of(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
- expect(r.first['test']).to be_an_instance_of(Time)
314
- end
315
- else
316
- it "should return Time when timestamp is < 1901-12-13 20:45:52" do
317
- r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
318
- expect(r.first['test']).to be_an_instance_of(Time)
319
- end
320
-
321
- it "should return Time when timestamp is > 2038-01-19T03:14:07" do
322
- r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
323
- expect(r.first['test']).to be_an_instance_of(Time)
324
- end
325
- end
326
- end
327
-
328
- it "should return Time for a TIMESTAMP value when within the supported range" do
329
- expect(@test_result['timestamp_test']).to be_an_instance_of(Time)
330
- expect(@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
331
- end
332
-
333
- it "should return Time for a TIME value" do
334
- expect(@test_result['time_test']).to be_an_instance_of(Time)
335
- expect(@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2000-01-01 11:44:00')
336
- end
337
-
338
- it "should return Date for a DATE value" do
339
- expect(@test_result['date_test']).to be_an_instance_of(Date)
340
- expect(@test_result['date_test'].strftime("%Y-%m-%d")).to eql('2010-04-04')
341
- end
342
-
343
- it "should return String for an ENUM value" do
344
- expect(@test_result['enum_test']).to be_an_instance_of(String)
345
- expect(@test_result['enum_test']).to eql('val1')
346
- end
347
-
348
- it "should raise an error given an invalid DATETIME" do
349
- expect { @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each }.to \
350
- raise_error(Mysql2::Error, "Invalid date in field 'bad_datetime': 1972-00-27 00:00:00")
351
- end
352
-
353
- context "string encoding for ENUM values" do
354
- before { pending('Encoding is undefined') unless defined?(Encoding) }
355
-
356
- it "should default to the connection's encoding if Encoding.default_internal is nil" do
357
- with_internal_encoding nil do
358
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
359
- expect(result['enum_test'].encoding).to eql(Encoding::UTF_8)
360
-
361
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
362
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
363
- expect(result['enum_test'].encoding).to eql(Encoding::ASCII)
364
- client2.close
365
- end
366
- end
367
-
368
- it "should use Encoding.default_internal" do
369
- with_internal_encoding Encoding::UTF_8 do
370
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
371
- expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
372
- end
373
-
374
- with_internal_encoding Encoding::ASCII do
375
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
376
- expect(result['enum_test'].encoding).to eql(Encoding.default_internal)
377
- end
378
- end
379
- end
380
-
381
- it "should return String for a SET value" do
382
- expect(@test_result['set_test']).to be_an_instance_of(String)
383
- expect(@test_result['set_test']).to eql('val1,val2')
384
- end
385
-
386
- context "string encoding for SET values" do
387
- before { pending('Encoding is undefined') unless defined?(Encoding) }
388
-
389
- it "should default to the connection's encoding if Encoding.default_internal is nil" do
390
- with_internal_encoding nil do
391
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
392
- expect(result['set_test'].encoding).to eql(Encoding::UTF_8)
393
-
394
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
395
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
396
- expect(result['set_test'].encoding).to eql(Encoding::ASCII)
397
- client2.close
398
- end
399
- end
400
-
401
- it "should use Encoding.default_internal" do
402
- with_internal_encoding Encoding::UTF_8 do
403
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
404
- expect(result['set_test'].encoding).to eql(Encoding.default_internal)
405
- end
406
-
407
- with_internal_encoding Encoding::ASCII do
408
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
409
- expect(result['set_test'].encoding).to eql(Encoding.default_internal)
410
- end
411
- end
412
- end
413
-
414
- it "should return String for a BINARY value" do
415
- expect(@test_result['binary_test']).to be_an_instance_of(String)
416
- expect(@test_result['binary_test']).to eql("test#{"\000" * 6}")
417
- end
418
-
419
- context "string encoding for BINARY values" do
420
- before { pending('Encoding is undefined') unless defined?(Encoding) }
421
-
422
- it "should default to binary if Encoding.default_internal is nil" do
423
- with_internal_encoding nil do
424
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
425
- expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
426
- end
427
- end
428
-
429
- it "should not use Encoding.default_internal" do
430
- with_internal_encoding Encoding::UTF_8 do
431
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
432
- expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
433
- end
434
-
435
- with_internal_encoding Encoding::ASCII do
436
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
437
- expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
438
- end
439
- end
440
- end
441
-
442
- {
443
- 'char_test' => 'CHAR',
444
- 'varchar_test' => 'VARCHAR',
445
- 'varbinary_test' => 'VARBINARY',
446
- 'tiny_blob_test' => 'TINYBLOB',
447
- 'tiny_text_test' => 'TINYTEXT',
448
- 'blob_test' => 'BLOB',
449
- 'text_test' => 'TEXT',
450
- 'medium_blob_test' => 'MEDIUMBLOB',
451
- 'medium_text_test' => 'MEDIUMTEXT',
452
- 'long_blob_test' => 'LONGBLOB',
453
- 'long_text_test' => 'LONGTEXT',
454
- }.each do |field, type|
455
- it "should return a String for #{type}" do
456
- expect(@test_result[field]).to be_an_instance_of(String)
457
- expect(@test_result[field]).to eql("test")
458
- end
459
-
460
- context "string encoding for #{type} values" do
461
- before { pending('Encoding is undefined') unless defined?(Encoding) }
462
-
463
- if %w(VARBINARY TINYBLOB BLOB MEDIUMBLOB LONGBLOB).include?(type)
464
- it "should default to binary if Encoding.default_internal is nil" do
465
- with_internal_encoding nil do
466
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
467
- expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
468
- end
469
- end
470
-
471
- it "should not use Encoding.default_internal" do
472
- with_internal_encoding Encoding::UTF_8 do
473
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
474
- expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
475
- end
476
-
477
- with_internal_encoding Encoding::ASCII do
478
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
479
- expect(result['binary_test'].encoding).to eql(Encoding::BINARY)
480
- end
481
- end
482
- else
483
- it "should default to utf-8 if Encoding.default_internal is nil" do
484
- with_internal_encoding nil do
485
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
486
- expect(result[field].encoding).to eql(Encoding::UTF_8)
487
-
488
- client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
489
- result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
490
- expect(result[field].encoding).to eql(Encoding::ASCII)
491
- client2.close
492
- end
493
- end
494
-
495
- it "should use Encoding.default_internal" do
496
- with_internal_encoding Encoding::UTF_8 do
497
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
498
- expect(result[field].encoding).to eql(Encoding.default_internal)
499
- end
500
-
501
- with_internal_encoding Encoding::ASCII do
502
- result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
503
- expect(result[field].encoding).to eql(Encoding.default_internal)
504
- end
505
- end
506
- end
507
- end
508
- end
509
- end
510
- end