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