mysql2 0.4.10 → 0.5.4

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