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