mysql2 0.4.6 → 0.4.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +54 -42
- data/ext/mysql2/client.c +71 -24
- data/ext/mysql2/client.h +1 -2
- data/ext/mysql2/extconf.rb +8 -4
- data/ext/mysql2/mysql2_ext.h +0 -4
- data/ext/mysql2/result.c +7 -1
- data/ext/mysql2/statement.c +23 -2
- data/lib/mysql2/client.rb +5 -5
- data/lib/mysql2/version.rb +1 -1
- data/spec/em/em_spec.rb +1 -0
- data/spec/mysql2/client_spec.rb +130 -78
- data/spec/mysql2/error_spec.rb +3 -5
- data/spec/mysql2/result_spec.rb +62 -36
- data/spec/mysql2/statement_spec.rb +65 -48
- data/spec/spec_helper.rb +73 -59
- data/support/5072E1F5.asc +432 -0
- metadata +12 -11
data/ext/mysql2/result.c
CHANGED
@@ -405,6 +405,13 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
|
|
405
405
|
val = INT2NUM(*((signed char*)result_buffer->buffer));
|
406
406
|
}
|
407
407
|
break;
|
408
|
+
case MYSQL_TYPE_BIT: /* BIT field (MySQL 5.0.3 and up) */
|
409
|
+
if (args->castBool && fields[i].length == 1) {
|
410
|
+
val = (*((unsigned char*)result_buffer->buffer) != 0) ? Qtrue : Qfalse;
|
411
|
+
}else{
|
412
|
+
val = rb_str_new(result_buffer->buffer, *(result_buffer->length));
|
413
|
+
}
|
414
|
+
break;
|
408
415
|
case MYSQL_TYPE_SHORT: // short int
|
409
416
|
if (result_buffer->is_unsigned) {
|
410
417
|
val = UINT2NUM(*((unsigned short int*)result_buffer->buffer));
|
@@ -494,7 +501,6 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
|
|
494
501
|
case MYSQL_TYPE_BLOB: // char[]
|
495
502
|
case MYSQL_TYPE_MEDIUM_BLOB: // char[]
|
496
503
|
case MYSQL_TYPE_LONG_BLOB: // char[]
|
497
|
-
case MYSQL_TYPE_BIT: // char[]
|
498
504
|
case MYSQL_TYPE_SET: // char[]
|
499
505
|
case MYSQL_TYPE_ENUM: // char[]
|
500
506
|
case MYSQL_TYPE_GEOMETRY: // char[]
|
data/ext/mysql2/statement.c
CHANGED
@@ -343,6 +343,16 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {
|
|
343
343
|
#endif
|
344
344
|
set_buffer_for_string(&bind_buffers[i], &length_buffers[i], params_enc[i]);
|
345
345
|
break;
|
346
|
+
case T_TRUE:
|
347
|
+
bind_buffers[i].buffer_type = MYSQL_TYPE_TINY;
|
348
|
+
bind_buffers[i].buffer = xmalloc(sizeof(signed char));
|
349
|
+
*(signed char*)(bind_buffers[i].buffer) = 1;
|
350
|
+
break;
|
351
|
+
case T_FALSE:
|
352
|
+
bind_buffers[i].buffer_type = MYSQL_TYPE_TINY;
|
353
|
+
bind_buffers[i].buffer = xmalloc(sizeof(signed char));
|
354
|
+
*(signed char*)(bind_buffers[i].buffer) = 0;
|
355
|
+
break;
|
346
356
|
default:
|
347
357
|
// TODO: what Ruby type should support MYSQL_TYPE_TIME
|
348
358
|
if (CLASS_OF(argv[i]) == rb_cTime || CLASS_OF(argv[i]) == cDateTime) {
|
@@ -468,6 +478,7 @@ static VALUE fields(VALUE self) {
|
|
468
478
|
rb_encoding *default_internal_enc, *conn_enc;
|
469
479
|
#endif
|
470
480
|
GET_STATEMENT(self);
|
481
|
+
GET_CLIENT(stmt_wrapper->client);
|
471
482
|
stmt = stmt_wrapper->stmt;
|
472
483
|
|
473
484
|
#ifdef HAVE_RUBY_ENCODING_H
|
@@ -478,12 +489,22 @@ static VALUE fields(VALUE self) {
|
|
478
489
|
}
|
479
490
|
#endif
|
480
491
|
|
481
|
-
metadata
|
492
|
+
metadata = mysql_stmt_result_metadata(stmt);
|
493
|
+
if (metadata == NULL) {
|
494
|
+
if (mysql_stmt_errno(stmt) != 0) {
|
495
|
+
// either CR_OUT_OF_MEMORY or CR_UNKNOWN_ERROR. both fatal.
|
496
|
+
wrapper->active_thread = Qnil;
|
497
|
+
rb_raise_mysql2_stmt_error(stmt_wrapper);
|
498
|
+
}
|
499
|
+
// no data and no error, so query was not a SELECT
|
500
|
+
return Qnil;
|
501
|
+
}
|
502
|
+
|
482
503
|
fields = mysql_fetch_fields(metadata);
|
483
504
|
field_count = mysql_stmt_field_count(stmt);
|
484
505
|
field_list = rb_ary_new2((long)field_count);
|
485
506
|
|
486
|
-
for(i = 0; i < field_count; i++) {
|
507
|
+
for (i = 0; i < field_count; i++) {
|
487
508
|
VALUE rb_field;
|
488
509
|
|
489
510
|
rb_field = rb_str_new(fields[i].name, fields[i].name_length);
|
data/lib/mysql2/client.rb
CHANGED
@@ -31,10 +31,10 @@ module Mysql2
|
|
31
31
|
opts[:connect_timeout] = 120 unless opts.key?(:connect_timeout)
|
32
32
|
|
33
33
|
# TODO: stricter validation rather than silent massaging
|
34
|
-
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth, :init_command, :automatic_close].each do |key|
|
34
|
+
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :default_file, :default_group, :secure_auth, :init_command, :automatic_close, :enable_cleartext_plugin].each do |key|
|
35
35
|
next unless opts.key?(key)
|
36
36
|
case key
|
37
|
-
when :reconnect, :local_infile, :secure_auth, :automatic_close
|
37
|
+
when :reconnect, :local_infile, :secure_auth, :automatic_close, :enable_cleartext_plugin
|
38
38
|
send(:"#{key}=", !!opts[key]) # rubocop:disable Style/DoubleNegation
|
39
39
|
when :connect_timeout, :read_timeout, :write_timeout
|
40
40
|
send(:"#{key}=", Integer(opts[key])) unless opts[key].nil?
|
@@ -47,7 +47,7 @@ module Mysql2
|
|
47
47
|
self.charset_name = opts[:encoding] || 'utf8'
|
48
48
|
|
49
49
|
ssl_options = opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslcipher)
|
50
|
-
ssl_set(*ssl_options) if ssl_options.any?
|
50
|
+
ssl_set(*ssl_options) if ssl_options.any? || opts.key?(:sslverify)
|
51
51
|
self.ssl_mode = parse_ssl_mode(opts[:ssl_mode]) if opts[:ssl_mode]
|
52
52
|
|
53
53
|
case opts[:flags]
|
@@ -62,11 +62,11 @@ module Mysql2
|
|
62
62
|
end
|
63
63
|
|
64
64
|
# SSL verify is a connection flag rather than a mysql_ssl_set option
|
65
|
-
flags |= SSL_VERIFY_SERVER_CERT if opts[:sslverify]
|
65
|
+
flags |= SSL_VERIFY_SERVER_CERT if opts[:sslverify]
|
66
66
|
|
67
67
|
if [:user, :pass, :hostname, :dbname, :db, :sock].any? { |k| @query_options.key?(k) }
|
68
68
|
warn "============= WARNING FROM mysql2 ============="
|
69
|
-
warn "The options :user, :pass, :hostname, :dbname, :db, and :sock will be
|
69
|
+
warn "The options :user, :pass, :hostname, :dbname, :db, and :sock are deprecated and will be removed at some point in the future."
|
70
70
|
warn "Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options."
|
71
71
|
warn "============= END WARNING FROM mysql2 ========="
|
72
72
|
end
|
data/lib/mysql2/version.rb
CHANGED
data/spec/em/em_spec.rb
CHANGED
@@ -70,6 +70,7 @@ begin
|
|
70
70
|
let(:client) { Mysql2::EM::Client.new DatabaseCredentials['root'] }
|
71
71
|
let(:error) { StandardError.new('some error') }
|
72
72
|
before { allow(client).to receive(:async_result).and_raise(error) }
|
73
|
+
after { client.close }
|
73
74
|
|
74
75
|
it "should swallow exceptions raised in by the client" do
|
75
76
|
errors = []
|
data/spec/mysql2/client_spec.rb
CHANGED
@@ -7,14 +7,13 @@ RSpec.describe Mysql2::Client do
|
|
7
7
|
|
8
8
|
it "should not raise an exception for valid defaults group" do
|
9
9
|
expect {
|
10
|
-
|
11
|
-
@client = Mysql2::Client.new(opts)
|
10
|
+
new_client(:default_file => cnf_file, :default_group => "test")
|
12
11
|
}.not_to raise_error
|
13
12
|
end
|
14
13
|
|
15
14
|
it "should not raise an exception without default group" do
|
16
15
|
expect {
|
17
|
-
|
16
|
+
new_client(:default_file => cnf_file)
|
18
17
|
}.not_to raise_error
|
19
18
|
end
|
20
19
|
end
|
@@ -23,29 +22,29 @@ RSpec.describe Mysql2::Client do
|
|
23
22
|
expect {
|
24
23
|
# The odd local host IP address forces the mysql client library to
|
25
24
|
# use a TCP socket rather than a domain socket.
|
26
|
-
|
25
|
+
new_client('host' => '127.0.0.2', 'port' => 999999)
|
27
26
|
}.to raise_error(Mysql2::Error)
|
28
27
|
end
|
29
28
|
|
30
29
|
it "should raise an exception on create for invalid encodings" do
|
31
30
|
expect {
|
32
|
-
|
31
|
+
new_client(:encoding => "fake")
|
33
32
|
}.to raise_error(Mysql2::Error)
|
34
33
|
end
|
35
34
|
|
36
35
|
it "should raise an exception on non-string encodings" do
|
37
36
|
expect {
|
38
|
-
|
37
|
+
new_client(:encoding => :fake)
|
39
38
|
}.to raise_error(TypeError)
|
40
39
|
end
|
41
40
|
|
42
41
|
it "should not raise an exception on create for a valid encoding" do
|
43
42
|
expect {
|
44
|
-
|
43
|
+
new_client(:encoding => "utf8")
|
45
44
|
}.not_to raise_error
|
46
45
|
|
47
46
|
expect {
|
48
|
-
|
47
|
+
new_client(DatabaseCredentials['root'].merge(:encoding => "big5"))
|
49
48
|
}.not_to raise_error
|
50
49
|
end
|
51
50
|
|
@@ -88,7 +87,7 @@ RSpec.describe Mysql2::Client do
|
|
88
87
|
it "should execute init command" do
|
89
88
|
options = DatabaseCredentials['root'].dup
|
90
89
|
options[:init_command] = "SET @something = 'setting_value';"
|
91
|
-
client =
|
90
|
+
client = new_client(options)
|
92
91
|
result = client.query("SELECT @something;")
|
93
92
|
expect(result.first['@something']).to eq('setting_value')
|
94
93
|
end
|
@@ -97,7 +96,7 @@ RSpec.describe Mysql2::Client do
|
|
97
96
|
options = DatabaseCredentials['root'].dup
|
98
97
|
options[:init_command] = "SET @something = 'setting_value';"
|
99
98
|
options[:reconnect] = true
|
100
|
-
client =
|
99
|
+
client = new_client(options)
|
101
100
|
|
102
101
|
result = client.query("SELECT @something;")
|
103
102
|
expect(result.first['@something']).to eq('setting_value')
|
@@ -138,15 +137,13 @@ RSpec.describe Mysql2::Client do
|
|
138
137
|
ssl_client = nil
|
139
138
|
expect {
|
140
139
|
# rubocop:disable Style/TrailingComma
|
141
|
-
ssl_client =
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
:sslverify => true
|
149
|
-
)
|
140
|
+
ssl_client = new_client(
|
141
|
+
'host' => 'mysql2gem.example.com', # must match the certificates
|
142
|
+
:sslkey => '/etc/mysql/client-key.pem',
|
143
|
+
:sslcert => '/etc/mysql/client-cert.pem',
|
144
|
+
:sslca => '/etc/mysql/ca-cert.pem',
|
145
|
+
:sslcipher => 'DHE-RSA-AES256-SHA',
|
146
|
+
:sslverify => true
|
150
147
|
)
|
151
148
|
# rubocop:enable Style/TrailingComma
|
152
149
|
}.not_to raise_error
|
@@ -157,8 +154,6 @@ RSpec.describe Mysql2::Client do
|
|
157
154
|
|
158
155
|
expect(ssl_client.ssl_cipher).not_to be_empty
|
159
156
|
expect(results['Ssl_cipher']).to eql(ssl_client.ssl_cipher)
|
160
|
-
|
161
|
-
ssl_client.close
|
162
157
|
end
|
163
158
|
|
164
159
|
def run_gc
|
@@ -172,10 +167,21 @@ RSpec.describe Mysql2::Client do
|
|
172
167
|
|
173
168
|
it "should terminate connections when calling close" do
|
174
169
|
expect {
|
175
|
-
Mysql2::Client.new(DatabaseCredentials['root'])
|
170
|
+
client = Mysql2::Client.new(DatabaseCredentials['root'])
|
171
|
+
connection_id = client.thread_id
|
172
|
+
client.close
|
173
|
+
|
174
|
+
# mysql_close sends a quit command without waiting for a response
|
175
|
+
# so give the server some time to handle the detect the closed connection
|
176
|
+
closed = false
|
177
|
+
10.times do
|
178
|
+
closed = @client.query("SHOW PROCESSLIST").none? { |row| row['Id'] == connection_id }
|
179
|
+
break if closed
|
180
|
+
sleep(0.1)
|
181
|
+
end
|
182
|
+
expect(closed).to eq(true)
|
176
183
|
}.to_not change {
|
177
|
-
@client.query("SHOW STATUS LIKE 'Aborted_%'").to_a
|
178
|
-
@client.query("SHOW STATUS LIKE 'Threads_connected'").to_a
|
184
|
+
@client.query("SHOW STATUS LIKE 'Aborted_%'").to_a
|
179
185
|
}
|
180
186
|
end
|
181
187
|
|
@@ -199,36 +205,35 @@ RSpec.describe Mysql2::Client do
|
|
199
205
|
|
200
206
|
context "#automatic_close" do
|
201
207
|
it "is enabled by default" do
|
202
|
-
|
203
|
-
expect(client.automatic_close?).to be(true)
|
208
|
+
expect(new_client.automatic_close?).to be(true)
|
204
209
|
end
|
205
210
|
|
206
211
|
if RUBY_PLATFORM =~ /mingw|mswin/
|
207
212
|
it "cannot be disabled" do
|
208
213
|
expect do
|
209
|
-
client =
|
214
|
+
client = new_client(:automatic_close => false)
|
210
215
|
expect(client.automatic_close?).to be(true)
|
211
216
|
end.to output(/always closed by garbage collector/).to_stderr
|
212
217
|
|
213
218
|
expect do
|
214
|
-
client =
|
219
|
+
client = new_client(:automatic_close => true)
|
215
220
|
expect(client.automatic_close?).to be(true)
|
216
221
|
end.to_not output(/always closed by garbage collector/).to_stderr
|
217
222
|
|
218
223
|
expect do
|
219
|
-
client =
|
224
|
+
client = new_client(:automatic_close => true)
|
220
225
|
client.automatic_close = false
|
221
226
|
expect(client.automatic_close?).to be(true)
|
222
227
|
end.to output(/always closed by garbage collector/).to_stderr
|
223
228
|
end
|
224
229
|
else
|
225
230
|
it "can be configured" do
|
226
|
-
client =
|
231
|
+
client = new_client(:automatic_close => false)
|
227
232
|
expect(client.automatic_close?).to be(false)
|
228
233
|
end
|
229
234
|
|
230
235
|
it "can be assigned" do
|
231
|
-
client =
|
236
|
+
client = new_client
|
232
237
|
client.automatic_close = false
|
233
238
|
expect(client.automatic_close?).to be(false)
|
234
239
|
|
@@ -247,21 +252,18 @@ RSpec.describe Mysql2::Client do
|
|
247
252
|
client = Mysql2::Client.new(DatabaseCredentials['root'])
|
248
253
|
client.automatic_close = false
|
249
254
|
|
250
|
-
|
251
|
-
# `fork` call hangs forever. WTF?
|
252
|
-
fork {}
|
253
|
-
|
254
|
-
fork do
|
255
|
+
child = fork do
|
255
256
|
client.query('SELECT 1')
|
256
257
|
client = nil
|
257
258
|
run_gc
|
258
259
|
end
|
259
260
|
|
260
|
-
Process.wait
|
261
|
+
Process.wait(child)
|
261
262
|
|
262
263
|
# this will throw an error if the underlying socket was shutdown by the
|
263
264
|
# child's GC
|
264
265
|
expect { client.query('SELECT 1') }.to_not raise_exception
|
266
|
+
client.close
|
265
267
|
end
|
266
268
|
end
|
267
269
|
end
|
@@ -271,7 +273,7 @@ RSpec.describe Mysql2::Client do
|
|
271
273
|
@client.query "CREATE DATABASE IF NOT EXISTS `#{database}`"
|
272
274
|
|
273
275
|
expect {
|
274
|
-
|
276
|
+
new_client('database' => database)
|
275
277
|
}.not_to raise_error
|
276
278
|
|
277
279
|
@client.query "DROP DATABASE IF EXISTS `#{database}`"
|
@@ -288,6 +290,25 @@ RSpec.describe Mysql2::Client do
|
|
288
290
|
}.to raise_error(Mysql2::Error)
|
289
291
|
end
|
290
292
|
|
293
|
+
context "#closed?" do
|
294
|
+
it "should return false when connected" do
|
295
|
+
expect(@client.closed?).to eql(false)
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should return true after close" do
|
299
|
+
@client.close
|
300
|
+
expect(@client.closed?).to eql(true)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should not try to query closed mysql connection" do
|
305
|
+
client = new_client(:reconnect => true)
|
306
|
+
expect(client.close).to be_nil
|
307
|
+
expect {
|
308
|
+
client.query "SELECT 1"
|
309
|
+
}.to raise_error(Mysql2::Error)
|
310
|
+
end
|
311
|
+
|
291
312
|
it "should respond to #query" do
|
292
313
|
expect(@client).to respond_to(:query)
|
293
314
|
end
|
@@ -306,8 +327,8 @@ RSpec.describe Mysql2::Client do
|
|
306
327
|
context "when has a warnings" do
|
307
328
|
it "should > 0" do
|
308
329
|
# "the statement produces extra information that can be viewed by issuing a SHOW WARNINGS"
|
309
|
-
#
|
310
|
-
@client.query(
|
330
|
+
# https://dev.mysql.com/doc/refman/5.7/en/show-warnings.html
|
331
|
+
@client.query('DROP TABLE IF EXISTS test.no_such_table')
|
311
332
|
expect(@client.warning_count).to be > 0
|
312
333
|
end
|
313
334
|
end
|
@@ -344,67 +365,72 @@ RSpec.describe Mysql2::Client do
|
|
344
365
|
|
345
366
|
context ":local_infile" do
|
346
367
|
before(:all) do
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
368
|
+
new_client(:local_infile => true) do |client|
|
369
|
+
local = client.query "SHOW VARIABLES LIKE 'local_infile'"
|
370
|
+
local_enabled = local.any? { |x| x['Value'] == 'ON' }
|
371
|
+
skip("DON'T WORRY, THIS TEST PASSES - but LOCAL INFILE is not enabled in your MySQL daemon.") unless local_enabled
|
372
|
+
|
373
|
+
client.query %[
|
374
|
+
CREATE TABLE IF NOT EXISTS infileTest (
|
375
|
+
id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
376
|
+
foo VARCHAR(10),
|
377
|
+
bar MEDIUMTEXT
|
378
|
+
)
|
379
|
+
]
|
380
|
+
end
|
359
381
|
end
|
360
382
|
|
361
383
|
after(:all) do
|
362
|
-
|
384
|
+
new_client do |client|
|
385
|
+
client.query "DROP TABLE IF EXISTS infileTest"
|
386
|
+
end
|
363
387
|
end
|
364
388
|
|
365
389
|
it "should raise an error when local_infile is disabled" do
|
366
|
-
client =
|
390
|
+
client = new_client(:local_infile => false)
|
367
391
|
expect {
|
368
392
|
client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
|
369
393
|
}.to raise_error(Mysql2::Error, /command is not allowed/)
|
370
394
|
end
|
371
395
|
|
372
396
|
it "should raise an error when a non-existent file is loaded" do
|
397
|
+
client = new_client(:local_infile => true)
|
373
398
|
expect {
|
374
|
-
|
399
|
+
client.query "LOAD DATA LOCAL INFILE 'this/file/is/not/here' INTO TABLE infileTest"
|
375
400
|
}.to raise_error(Mysql2::Error, 'No such file or directory: this/file/is/not/here')
|
376
401
|
end
|
377
402
|
|
378
403
|
it "should LOAD DATA LOCAL INFILE" do
|
379
|
-
|
380
|
-
|
404
|
+
client = new_client(:local_infile => true)
|
405
|
+
client.query "LOAD DATA LOCAL INFILE 'spec/test_data' INTO TABLE infileTest"
|
406
|
+
info = client.query_info
|
381
407
|
expect(info).to eql(:records => 1, :deleted => 0, :skipped => 0, :warnings => 0)
|
382
408
|
|
383
|
-
result =
|
409
|
+
result = client.query "SELECT * FROM infileTest"
|
384
410
|
expect(result.first).to eql('id' => 1, 'foo' => 'Hello', 'bar' => 'World')
|
385
411
|
end
|
386
412
|
end
|
387
413
|
|
388
414
|
it "should expect connect_timeout to be a positive integer" do
|
389
415
|
expect {
|
390
|
-
|
416
|
+
new_client(:connect_timeout => -1)
|
391
417
|
}.to raise_error(Mysql2::Error)
|
392
418
|
end
|
393
419
|
|
394
420
|
it "should expect read_timeout to be a positive integer" do
|
395
421
|
expect {
|
396
|
-
|
422
|
+
new_client(:read_timeout => -1)
|
397
423
|
}.to raise_error(Mysql2::Error)
|
398
424
|
end
|
399
425
|
|
400
426
|
it "should expect write_timeout to be a positive integer" do
|
401
427
|
expect {
|
402
|
-
|
428
|
+
new_client(:write_timeout => -1)
|
403
429
|
}.to raise_error(Mysql2::Error)
|
404
430
|
end
|
405
431
|
|
406
432
|
it "should allow nil read_timeout" do
|
407
|
-
client =
|
433
|
+
client = new_client(:read_timeout => nil)
|
408
434
|
|
409
435
|
expect(client.read_timeout).to be_nil
|
410
436
|
end
|
@@ -474,6 +500,25 @@ RSpec.describe Mysql2::Client do
|
|
474
500
|
}.to raise_error(Mysql2::Error)
|
475
501
|
end
|
476
502
|
|
503
|
+
it "should detect closed connection on query read error" do
|
504
|
+
connection_id = @client.thread_id
|
505
|
+
Thread.new do
|
506
|
+
sleep(0.1)
|
507
|
+
Mysql2::Client.new(DatabaseCredentials['root']).tap do |supervisor|
|
508
|
+
supervisor.query("KILL #{connection_id}")
|
509
|
+
end.close
|
510
|
+
end
|
511
|
+
expect {
|
512
|
+
@client.query("SELECT SLEEP(1)")
|
513
|
+
}.to raise_error(Mysql2::Error, /Lost connection to MySQL server/)
|
514
|
+
|
515
|
+
if RUBY_PLATFORM !~ /mingw|mswin/
|
516
|
+
expect {
|
517
|
+
@client.socket
|
518
|
+
}.to raise_error(Mysql2::Error, 'MySQL client is not connected')
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
477
522
|
if RUBY_PLATFORM !~ /mingw|mswin/
|
478
523
|
it "should not allow another query to be sent without fetching a result first" do
|
479
524
|
@client.query("SELECT 1", :async => true)
|
@@ -490,7 +535,7 @@ RSpec.describe Mysql2::Client do
|
|
490
535
|
end
|
491
536
|
|
492
537
|
it "should timeout if we wait longer than :read_timeout" do
|
493
|
-
client =
|
538
|
+
client = new_client(:read_timeout => 0)
|
494
539
|
expect {
|
495
540
|
client.query('SELECT SLEEP(0.1)')
|
496
541
|
}.to raise_error(Mysql2::Error)
|
@@ -559,22 +604,22 @@ RSpec.describe Mysql2::Client do
|
|
559
604
|
end
|
560
605
|
|
561
606
|
it "should handle Timeouts without leaving the connection hanging if reconnect is true" do
|
562
|
-
if RUBY_PLATFORM.include?('darwin') &&
|
563
|
-
pending('
|
607
|
+
if RUBY_PLATFORM.include?('darwin') && @client.server_info.fetch(:version).start_with?('5.5')
|
608
|
+
pending('MySQL 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
|
564
609
|
end
|
565
610
|
|
566
|
-
client =
|
611
|
+
client = new_client(:reconnect => true)
|
567
612
|
|
568
613
|
expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
|
569
614
|
expect { client.query('SELECT 1') }.to_not raise_error
|
570
615
|
end
|
571
616
|
|
572
617
|
it "should handle Timeouts without leaving the connection hanging if reconnect is set to true after construction" do
|
573
|
-
if RUBY_PLATFORM.include?('darwin') &&
|
574
|
-
pending('
|
618
|
+
if RUBY_PLATFORM.include?('darwin') && @client.server_info.fetch(:version).start_with?('5.5')
|
619
|
+
pending('MySQL 5.5 on OSX is afflicted by an unknown bug that breaks this test. See #633 and #634.')
|
575
620
|
end
|
576
621
|
|
577
|
-
client =
|
622
|
+
client = new_client
|
578
623
|
|
579
624
|
expect { Timeout.timeout(0.1, ArgumentError) { client.query('SELECT SLEEP(1)') } }.to raise_error(ArgumentError)
|
580
625
|
expect { client.query('SELECT 1') }.to raise_error(Mysql2::Error)
|
@@ -592,8 +637,9 @@ RSpec.describe Mysql2::Client do
|
|
592
637
|
# Note that each thread opens its own database connection
|
593
638
|
threads = 5.times.map do
|
594
639
|
Thread.new do
|
595
|
-
|
596
|
-
|
640
|
+
new_client do |client|
|
641
|
+
client.query("SELECT SLEEP(#{sleep_time})")
|
642
|
+
end
|
597
643
|
Thread.current.object_id
|
598
644
|
end
|
599
645
|
end
|
@@ -606,10 +652,11 @@ RSpec.describe Mysql2::Client do
|
|
606
652
|
end
|
607
653
|
|
608
654
|
it "evented async queries should be supported" do
|
655
|
+
skip("ruby 1.8 doesn't support IO.for_fd options") if RUBY_VERSION.start_with?("1.8.")
|
609
656
|
# should immediately return nil
|
610
657
|
expect(@client.query("SELECT sleep(0.1)", :async => true)).to eql(nil)
|
611
658
|
|
612
|
-
io_wrapper = IO.for_fd(@client.socket)
|
659
|
+
io_wrapper = IO.for_fd(@client.socket, :autoclose => false)
|
613
660
|
loops = 0
|
614
661
|
loop do
|
615
662
|
if IO.select([io_wrapper], nil, nil, 0.05)
|
@@ -629,7 +676,7 @@ RSpec.describe Mysql2::Client do
|
|
629
676
|
|
630
677
|
context "Multiple results sets" do
|
631
678
|
before(:each) do
|
632
|
-
@multi_client =
|
679
|
+
@multi_client = new_client(:flags => Mysql2::Client::MULTI_STATEMENTS)
|
633
680
|
end
|
634
681
|
|
635
682
|
it "should raise an exception when one of multiple statements fails" do
|
@@ -707,7 +754,7 @@ RSpec.describe Mysql2::Client do
|
|
707
754
|
it "#socket should raise as it's not supported" do
|
708
755
|
expect {
|
709
756
|
@client.socket
|
710
|
-
}.to raise_error(Mysql2::Error)
|
757
|
+
}.to raise_error(Mysql2::Error, /Raw access to the mysql file descriptor isn't supported on Windows/)
|
711
758
|
end
|
712
759
|
end
|
713
760
|
|
@@ -786,7 +833,7 @@ RSpec.describe Mysql2::Client do
|
|
786
833
|
context 'when mysql encoding is not utf8' do
|
787
834
|
before { pending('Encoding is undefined') unless defined?(Encoding) }
|
788
835
|
|
789
|
-
let(:client) {
|
836
|
+
let(:client) { new_client(:encoding => "ujis") }
|
790
837
|
|
791
838
|
it 'should return a internal encoding string if Encoding.default_internal is set' do
|
792
839
|
with_internal_encoding Encoding::UTF_8 do
|
@@ -855,7 +902,7 @@ RSpec.describe Mysql2::Client do
|
|
855
902
|
with_internal_encoding nil do
|
856
903
|
expect(@client.server_info[:version].encoding).to eql(Encoding::UTF_8)
|
857
904
|
|
858
|
-
client2 =
|
905
|
+
client2 = new_client(:encoding => 'ascii')
|
859
906
|
expect(client2.server_info[:version].encoding).to eql(Encoding::ASCII)
|
860
907
|
end
|
861
908
|
end
|
@@ -873,11 +920,11 @@ RSpec.describe Mysql2::Client do
|
|
873
920
|
|
874
921
|
it "should raise a Mysql2::Error exception upon connection failure" do
|
875
922
|
expect {
|
876
|
-
|
923
|
+
new_client(:host => "localhost", :username => 'asdfasdf8d2h', :password => 'asdfasdfw42')
|
877
924
|
}.to raise_error(Mysql2::Error)
|
878
925
|
|
879
926
|
expect {
|
880
|
-
|
927
|
+
new_client(DatabaseCredentials['root'])
|
881
928
|
}.not_to raise_error
|
882
929
|
end
|
883
930
|
|
@@ -979,6 +1026,11 @@ RSpec.describe Mysql2::Client do
|
|
979
1026
|
expect(@client.ping).to eql(false)
|
980
1027
|
end
|
981
1028
|
|
1029
|
+
it "should be able to connect using plaintext password" do
|
1030
|
+
client = new_client(:enable_cleartext_plugin => true)
|
1031
|
+
client.query('SELECT 1')
|
1032
|
+
end
|
1033
|
+
|
982
1034
|
unless RUBY_VERSION =~ /1.8/
|
983
1035
|
it "should respond to #encoding" do
|
984
1036
|
expect(@client).to respond_to(:encoding)
|