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