mysql2 0.3.18 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -0
- data/LICENSE +21 -0
- data/README.md +28 -6
- data/examples/threaded.rb +4 -6
- data/ext/mysql2/client.c +124 -126
- data/ext/mysql2/client.h +21 -0
- data/ext/mysql2/extconf.rb +29 -15
- data/ext/mysql2/mysql2_ext.c +1 -0
- data/ext/mysql2/mysql2_ext.h +1 -0
- data/ext/mysql2/mysql_enc_name_to_ruby.h +6 -6
- data/ext/mysql2/result.c +499 -128
- data/ext/mysql2/result.h +9 -3
- data/ext/mysql2/statement.c +454 -0
- data/ext/mysql2/statement.h +22 -0
- data/lib/mysql2/client.rb +24 -1
- data/lib/mysql2/error.rb +9 -21
- data/lib/mysql2/field.rb +4 -0
- data/lib/mysql2/statement.rb +5 -0
- data/lib/mysql2/version.rb +1 -1
- data/lib/mysql2.rb +2 -0
- data/spec/em/em_spec.rb +13 -13
- data/spec/mysql2/client_spec.rb +337 -326
- data/spec/mysql2/error_spec.rb +37 -36
- data/spec/mysql2/result_spec.rb +197 -192
- data/spec/mysql2/statement_spec.rb +598 -0
- data/spec/spec_helper.rb +7 -0
- metadata +15 -46
data/spec/mysql2/result_spec.rb
CHANGED
@@ -1,39 +1,47 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe Mysql2::Result do
|
4
|
+
RSpec.describe Mysql2::Result do
|
5
5
|
before(:each) do
|
6
6
|
@result = @client.query "SELECT 1"
|
7
7
|
end
|
8
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
|
+
|
9
17
|
it "should have included Enumerable" do
|
10
|
-
Mysql2::Result.ancestors.include?(Enumerable).
|
18
|
+
expect(Mysql2::Result.ancestors.include?(Enumerable)).to be true
|
11
19
|
end
|
12
20
|
|
13
21
|
it "should respond to #each" do
|
14
|
-
@result.
|
22
|
+
expect(@result).to respond_to(:each)
|
15
23
|
end
|
16
24
|
|
17
25
|
it "should raise a Mysql2::Error exception upon a bad query" do
|
18
|
-
|
26
|
+
expect {
|
19
27
|
@client.query "bad sql"
|
20
|
-
}.
|
28
|
+
}.to raise_error(Mysql2::Error)
|
21
29
|
|
22
|
-
|
30
|
+
expect {
|
23
31
|
@client.query "SELECT 1"
|
24
|
-
}.
|
32
|
+
}.not_to raise_error
|
25
33
|
end
|
26
34
|
|
27
35
|
it "should respond to #count, which is aliased as #size" do
|
28
36
|
r = @client.query "SELECT 1"
|
29
|
-
r.
|
30
|
-
r.
|
37
|
+
expect(r).to respond_to :count
|
38
|
+
expect(r).to respond_to :size
|
31
39
|
end
|
32
40
|
|
33
41
|
it "should be able to return the number of rows in the result set" do
|
34
42
|
r = @client.query "SELECT 1"
|
35
|
-
r.count.
|
36
|
-
r.size.
|
43
|
+
expect(r.count).to eql(1)
|
44
|
+
expect(r.size).to eql(1)
|
37
45
|
end
|
38
46
|
|
39
47
|
context "metadata queries" do
|
@@ -45,39 +53,39 @@ describe Mysql2::Result do
|
|
45
53
|
context "#each" do
|
46
54
|
it "should yield rows as hash's" do
|
47
55
|
@result.each do |row|
|
48
|
-
row.class.
|
56
|
+
expect(row.class).to eql(Hash)
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
52
60
|
it "should yield rows as hash's with symbol keys if :symbolize_keys was set to true" do
|
53
61
|
@result.each(:symbolize_keys => true) do |row|
|
54
|
-
row.keys.first.class.
|
62
|
+
expect(row.keys.first.class).to eql(Symbol)
|
55
63
|
end
|
56
64
|
end
|
57
65
|
|
58
66
|
it "should be able to return results as an array" do
|
59
67
|
@result.each(:as => :array) do |row|
|
60
|
-
row.class.
|
68
|
+
expect(row.class).to eql(Array)
|
61
69
|
end
|
62
70
|
end
|
63
71
|
|
64
72
|
it "should cache previously yielded results by default" do
|
65
|
-
@result.first.object_id.
|
73
|
+
expect(@result.first.object_id).to eql(@result.first.object_id)
|
66
74
|
end
|
67
75
|
|
68
76
|
it "should not cache previously yielded results if cache_rows is disabled" do
|
69
77
|
result = @client.query "SELECT 1", :cache_rows => false
|
70
|
-
result.first.object_id.
|
78
|
+
expect(result.first.object_id).not_to eql(result.first.object_id)
|
71
79
|
end
|
72
80
|
|
73
81
|
it "should yield different value for #first if streaming" do
|
74
82
|
result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
|
75
|
-
result.first.
|
83
|
+
expect(result.first).not_to eql(result.first)
|
76
84
|
end
|
77
85
|
|
78
86
|
it "should yield the same value for #first if streaming is disabled" do
|
79
87
|
result = @client.query "SELECT 1 UNION SELECT 2", :stream => false
|
80
|
-
result.first.
|
88
|
+
expect(result.first).to eql(result.first)
|
81
89
|
end
|
82
90
|
|
83
91
|
it "should throw an exception if we try to iterate twice when streaming is enabled" do
|
@@ -96,41 +104,43 @@ describe Mysql2::Result do
|
|
96
104
|
end
|
97
105
|
|
98
106
|
it "method should exist" do
|
99
|
-
@test_result.
|
107
|
+
expect(@test_result).to respond_to(:fields)
|
100
108
|
end
|
101
109
|
|
102
110
|
it "should return an array of field names in proper order" do
|
103
111
|
result = @client.query "SELECT 'a', 'b', 'c'"
|
104
|
-
result.fields.
|
112
|
+
expect(result.fields).to eql(['a', 'b', 'c'])
|
105
113
|
end
|
106
114
|
end
|
107
115
|
|
108
116
|
context "streaming" do
|
109
117
|
it "should maintain a count while streaming" do
|
110
|
-
result = @client.query('SELECT 1')
|
111
|
-
|
112
|
-
result.count.should eql(1)
|
118
|
+
result = @client.query('SELECT 1', :stream => true, :cache_rows => false)
|
119
|
+
expect(result.count).to eql(0)
|
113
120
|
result.each.to_a
|
114
|
-
result.count.
|
121
|
+
expect(result.count).to eql(1)
|
115
122
|
end
|
116
123
|
|
117
|
-
it "should
|
118
|
-
result = @client.query("SELECT
|
119
|
-
result.count.
|
120
|
-
result.
|
121
|
-
result.count.
|
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)
|
122
131
|
end
|
123
132
|
|
124
133
|
it "should not yield nil at the end of streaming" do
|
125
134
|
result = @client.query('SELECT * FROM mysql2_test', :stream => true, :cache_rows => false)
|
126
|
-
result.each { |r| r.
|
135
|
+
result.each { |r| expect(r).not_to be_nil}
|
127
136
|
end
|
128
137
|
|
129
138
|
it "#count should be zero for rows after streaming when there were no results" do
|
139
|
+
@client.query "USE test"
|
130
140
|
result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
|
131
|
-
result.count.
|
141
|
+
expect(result.count).to eql(0)
|
132
142
|
result.each.to_a
|
133
|
-
result.count.
|
143
|
+
expect(result.count).to eql(0)
|
134
144
|
end
|
135
145
|
|
136
146
|
it "should raise an exception if streaming ended due to a timeout" do
|
@@ -147,12 +157,12 @@ describe Mysql2::Result do
|
|
147
157
|
client.query "SET net_write_timeout = 1"
|
148
158
|
res = client.query "SELECT * FROM streamingTest", :stream => true, :cache_rows => false
|
149
159
|
|
150
|
-
|
160
|
+
expect {
|
151
161
|
res.each_with_index do |row, i|
|
152
162
|
# Exhaust the first result packet then trigger a timeout
|
153
163
|
sleep 2 if i > 0 && i % 1000 == 0
|
154
164
|
end
|
155
|
-
}.
|
165
|
+
}.to raise_error(Mysql2::Error, /Lost connection/)
|
156
166
|
end
|
157
167
|
end
|
158
168
|
|
@@ -163,32 +173,32 @@ describe Mysql2::Result do
|
|
163
173
|
|
164
174
|
it "should return nil values for NULL and strings for everything else when :cast is false" do
|
165
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
|
166
|
-
result["null_test"].
|
167
|
-
result["tiny_int_test"].
|
168
|
-
result["bool_cast_test"].
|
169
|
-
result["int_test"].
|
170
|
-
result["date_test"].
|
171
|
-
result["enum_test"].
|
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")
|
172
182
|
end
|
173
183
|
|
174
184
|
it "should return nil for a NULL value" do
|
175
|
-
@test_result['null_test'].class.
|
176
|
-
@test_result['null_test'].
|
185
|
+
expect(@test_result['null_test'].class).to eql(NilClass)
|
186
|
+
expect(@test_result['null_test']).to eql(nil)
|
177
187
|
end
|
178
188
|
|
179
189
|
it "should return String for a BIT(64) value" do
|
180
|
-
@test_result['bit_test'].class.
|
181
|
-
@test_result['bit_test'].
|
190
|
+
expect(@test_result['bit_test'].class).to eql(String)
|
191
|
+
expect(@test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
|
182
192
|
end
|
183
193
|
|
184
194
|
it "should return String for a BIT(1) value" do
|
185
|
-
@test_result['single_bit_test'].class.
|
186
|
-
@test_result['single_bit_test'].
|
195
|
+
expect(@test_result['single_bit_test'].class).to eql(String)
|
196
|
+
expect(@test_result['single_bit_test']).to eql("\001")
|
187
197
|
end
|
188
198
|
|
189
199
|
it "should return Fixnum for a TINYINT value" do
|
190
|
-
[Fixnum, Bignum].
|
191
|
-
@test_result['tiny_int_test'].
|
200
|
+
expect([Fixnum, Bignum]).to include(@test_result['tiny_int_test'].class)
|
201
|
+
expect(@test_result['tiny_int_test']).to eql(1)
|
192
202
|
end
|
193
203
|
|
194
204
|
it "should return TrueClass or FalseClass for a TINYINT value if :cast_booleans is enabled" do
|
@@ -202,9 +212,9 @@ describe Mysql2::Result do
|
|
202
212
|
result1 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 1 LIMIT 1', :cast_booleans => true
|
203
213
|
result2 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = 0 LIMIT 1', :cast_booleans => true
|
204
214
|
result3 = @client.query 'SELECT bool_cast_test FROM mysql2_test WHERE bool_cast_test = -1 LIMIT 1', :cast_booleans => true
|
205
|
-
result1.first['bool_cast_test'].
|
206
|
-
result2.first['bool_cast_test'].
|
207
|
-
result3.first['bool_cast_test'].
|
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
|
208
218
|
|
209
219
|
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
|
210
220
|
end
|
@@ -217,55 +227,55 @@ describe Mysql2::Result do
|
|
217
227
|
|
218
228
|
result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true
|
219
229
|
result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true
|
220
|
-
result1.first['single_bit_test'].
|
221
|
-
result2.first['single_bit_test'].
|
230
|
+
expect(result1.first['single_bit_test']).to be true
|
231
|
+
expect(result2.first['single_bit_test']).to be false
|
222
232
|
|
223
233
|
@client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})"
|
224
234
|
end
|
225
235
|
|
226
236
|
it "should return Fixnum for a SMALLINT value" do
|
227
|
-
[Fixnum, Bignum].
|
228
|
-
@test_result['small_int_test'].
|
237
|
+
expect([Fixnum, Bignum]).to include(@test_result['small_int_test'].class)
|
238
|
+
expect(@test_result['small_int_test']).to eql(10)
|
229
239
|
end
|
230
240
|
|
231
241
|
it "should return Fixnum for a MEDIUMINT value" do
|
232
|
-
[Fixnum, Bignum].
|
233
|
-
@test_result['medium_int_test'].
|
242
|
+
expect([Fixnum, Bignum]).to include(@test_result['medium_int_test'].class)
|
243
|
+
expect(@test_result['medium_int_test']).to eql(10)
|
234
244
|
end
|
235
245
|
|
236
246
|
it "should return Fixnum for an INT value" do
|
237
|
-
[Fixnum, Bignum].
|
238
|
-
@test_result['int_test'].
|
247
|
+
expect([Fixnum, Bignum]).to include(@test_result['int_test'].class)
|
248
|
+
expect(@test_result['int_test']).to eql(10)
|
239
249
|
end
|
240
250
|
|
241
251
|
it "should return Fixnum for a BIGINT value" do
|
242
|
-
[Fixnum, Bignum].
|
243
|
-
@test_result['big_int_test'].
|
252
|
+
expect([Fixnum, Bignum]).to include(@test_result['big_int_test'].class)
|
253
|
+
expect(@test_result['big_int_test']).to eql(10)
|
244
254
|
end
|
245
255
|
|
246
256
|
it "should return Fixnum for a YEAR value" do
|
247
|
-
[Fixnum, Bignum].
|
248
|
-
@test_result['year_test'].
|
257
|
+
expect([Fixnum, Bignum]).to include(@test_result['year_test'].class)
|
258
|
+
expect(@test_result['year_test']).to eql(2009)
|
249
259
|
end
|
250
260
|
|
251
261
|
it "should return BigDecimal for a DECIMAL value" do
|
252
|
-
@test_result['decimal_test'].class.
|
253
|
-
@test_result['decimal_test'].
|
262
|
+
expect(@test_result['decimal_test'].class).to eql(BigDecimal)
|
263
|
+
expect(@test_result['decimal_test']).to eql(10.3)
|
254
264
|
end
|
255
265
|
|
256
266
|
it "should return Float for a FLOAT value" do
|
257
|
-
@test_result['float_test'].class.
|
258
|
-
@test_result['float_test'].
|
267
|
+
expect(@test_result['float_test'].class).to eql(Float)
|
268
|
+
expect(@test_result['float_test']).to eql(10.3)
|
259
269
|
end
|
260
270
|
|
261
271
|
it "should return Float for a DOUBLE value" do
|
262
|
-
@test_result['double_test'].class.
|
263
|
-
@test_result['double_test'].
|
272
|
+
expect(@test_result['double_test'].class).to eql(Float)
|
273
|
+
expect(@test_result['double_test']).to eql(10.3)
|
264
274
|
end
|
265
275
|
|
266
276
|
it "should return Time for a DATETIME value when within the supported range" do
|
267
|
-
@test_result['date_time_test'].class.
|
268
|
-
@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S").
|
277
|
+
expect(@test_result['date_time_test'].class).to eql(Time)
|
278
|
+
expect(@test_result['date_time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
|
269
279
|
end
|
270
280
|
|
271
281
|
if 1.size == 4 # 32bit
|
@@ -278,158 +288,153 @@ describe Mysql2::Result do
|
|
278
288
|
it "should return DateTime when timestamp is < 1901-12-13 20:45:52" do
|
279
289
|
# 1901-12-13T20:45:52 is the min for 32bit Ruby 1.8
|
280
290
|
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
|
281
|
-
r.first['test'].class.
|
291
|
+
expect(r.first['test'].class).to eql(klass)
|
282
292
|
end
|
283
293
|
|
284
294
|
it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
|
285
295
|
# 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
|
286
296
|
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
287
|
-
r.first['test'].class.
|
297
|
+
expect(r.first['test'].class).to eql(klass)
|
288
298
|
end
|
289
299
|
elsif 1.size == 8 # 64bit
|
290
300
|
unless RUBY_VERSION =~ /1.8/
|
291
301
|
it "should return Time when timestamp is < 1901-12-13 20:45:52" do
|
292
302
|
r = @client.query("SELECT CAST('1901-12-13 20:45:51' AS DATETIME) as test")
|
293
|
-
r.first['test'].class.
|
303
|
+
expect(r.first['test'].class).to eql(Time)
|
294
304
|
end
|
295
305
|
|
296
306
|
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
297
307
|
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
298
|
-
r.first['test'].class.
|
308
|
+
expect(r.first['test'].class).to eql(Time)
|
299
309
|
end
|
300
310
|
else
|
301
311
|
it "should return Time when timestamp is > 0138-12-31 11:59:59" do
|
302
312
|
r = @client.query("SELECT CAST('0139-1-1 00:00:00' AS DATETIME) as test")
|
303
|
-
r.first['test'].class.
|
313
|
+
expect(r.first['test'].class).to eql(Time)
|
304
314
|
end
|
305
315
|
|
306
316
|
it "should return DateTime when timestamp is < 0139-1-1T00:00:00" do
|
307
317
|
r = @client.query("SELECT CAST('0138-12-31 11:59:59' AS DATETIME) as test")
|
308
|
-
r.first['test'].class.
|
318
|
+
expect(r.first['test'].class).to eql(DateTime)
|
309
319
|
end
|
310
320
|
|
311
321
|
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
|
312
322
|
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
|
313
|
-
r.first['test'].class.
|
323
|
+
expect(r.first['test'].class).to eql(Time)
|
314
324
|
end
|
315
325
|
end
|
316
326
|
end
|
317
327
|
|
318
328
|
it "should return Time for a TIMESTAMP value when within the supported range" do
|
319
|
-
@test_result['timestamp_test'].class.
|
320
|
-
@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S").
|
329
|
+
expect(@test_result['timestamp_test'].class).to eql(Time)
|
330
|
+
expect(@test_result['timestamp_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2010-04-04 11:44:00')
|
321
331
|
end
|
322
332
|
|
323
333
|
it "should return Time for a TIME value" do
|
324
|
-
@test_result['time_test'].class.
|
325
|
-
@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S").
|
334
|
+
expect(@test_result['time_test'].class).to eql(Time)
|
335
|
+
expect(@test_result['time_test'].strftime("%Y-%m-%d %H:%M:%S")).to eql('2000-01-01 11:44:00')
|
326
336
|
end
|
327
337
|
|
328
338
|
it "should return Date for a DATE value" do
|
329
|
-
@test_result['date_test'].class.
|
330
|
-
@test_result['date_test'].strftime("%Y-%m-%d").
|
339
|
+
expect(@test_result['date_test'].class).to eql(Date)
|
340
|
+
expect(@test_result['date_test'].strftime("%Y-%m-%d")).to eql('2010-04-04')
|
331
341
|
end
|
332
342
|
|
333
343
|
it "should return String for an ENUM value" do
|
334
|
-
@test_result['enum_test'].class.
|
335
|
-
@test_result['enum_test'].
|
344
|
+
expect(@test_result['enum_test'].class).to eql(String)
|
345
|
+
expect(@test_result['enum_test']).to eql('val1')
|
336
346
|
end
|
337
347
|
|
338
348
|
it "should raise an error given an invalid DATETIME" do
|
339
|
-
|
340
|
-
|
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")
|
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")
|
346
351
|
end
|
347
352
|
|
348
|
-
|
349
|
-
|
350
|
-
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'))
|
353
|
+
context "string encoding for ENUM values" do
|
354
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
354
355
|
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
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
|
360
365
|
end
|
366
|
+
end
|
361
367
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
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
|
367
373
|
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
end
|
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)
|
372
377
|
end
|
373
378
|
end
|
374
379
|
end
|
375
380
|
|
376
381
|
it "should return String for a SET value" do
|
377
|
-
@test_result['set_test'].class.
|
378
|
-
@test_result['set_test'].
|
382
|
+
expect(@test_result['set_test'].class).to eql(String)
|
383
|
+
expect(@test_result['set_test']).to eql('val1,val2')
|
379
384
|
end
|
380
385
|
|
381
|
-
|
382
|
-
|
383
|
-
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'))
|
386
|
+
context "string encoding for SET values" do
|
387
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
387
388
|
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
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
|
393
398
|
end
|
399
|
+
end
|
394
400
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
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
|
400
406
|
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
end
|
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)
|
405
410
|
end
|
406
411
|
end
|
407
412
|
end
|
408
413
|
|
409
414
|
it "should return String for a BINARY value" do
|
410
|
-
@test_result['binary_test'].class.
|
411
|
-
@test_result['binary_test'].
|
415
|
+
expect(@test_result['binary_test'].class).to eql(String)
|
416
|
+
expect(@test_result['binary_test']).to eql("test#{"\000"*6}")
|
412
417
|
end
|
413
418
|
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
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)
|
421
426
|
end
|
427
|
+
end
|
422
428
|
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
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
|
428
434
|
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
end
|
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)
|
433
438
|
end
|
434
439
|
end
|
435
440
|
end
|
@@ -448,54 +453,54 @@ describe Mysql2::Result do
|
|
448
453
|
'long_text_test' => 'LONGTEXT'
|
449
454
|
}.each do |field, type|
|
450
455
|
it "should return a String for #{type}" do
|
451
|
-
@test_result[field].class.
|
452
|
-
@test_result[field].
|
456
|
+
expect(@test_result[field].class).to eql(String)
|
457
|
+
expect(@test_result[field]).to eql("test")
|
453
458
|
end
|
454
459
|
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
460
|
+
context "string encoding for #{type} values" do
|
461
|
+
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
462
|
+
|
463
|
+
if ['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)
|
463
468
|
end
|
469
|
+
end
|
464
470
|
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
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
|
470
476
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
end
|
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)
|
475
480
|
end
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
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
|
487
492
|
end
|
493
|
+
end
|
488
494
|
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
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
|
494
500
|
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
end
|
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)
|
499
504
|
end
|
500
505
|
end
|
501
506
|
end
|