mysql2 0.3.12 → 0.3.13
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mysql2/client.rb +2 -1
- data/lib/mysql2/version.rb +1 -1
- data/spec/em/em_spec.rb +6 -2
- data/spec/mysql2/client_spec.rb +48 -59
- data/spec/mysql2/error_spec.rb +14 -10
- data/spec/mysql2/result_spec.rb +12 -13
- data/spec/spec_helper.rb +8 -0
- metadata +2 -2
data/lib/mysql2/client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Mysql2
|
2
2
|
class Client
|
3
|
-
attr_reader :query_options
|
3
|
+
attr_reader :query_options, :read_timeout
|
4
4
|
@@default_query_options = {
|
5
5
|
:as => :hash, # the type of object you want each row back as; also supports :array (an array of values)
|
6
6
|
:async => false, # don't wait for a result after sending the query, you'll have to monitor the socket yourself then eventually call Mysql2::Client#async_result
|
@@ -15,6 +15,7 @@ module Mysql2
|
|
15
15
|
|
16
16
|
def initialize(opts = {})
|
17
17
|
opts = Mysql2::Util.key_hash_as_symbols( opts )
|
18
|
+
@read_timeout = nil
|
18
19
|
@query_options = @@default_query_options.dup
|
19
20
|
@query_options.merge! opts
|
20
21
|
|
data/lib/mysql2/version.rb
CHANGED
data/spec/em/em_spec.rb
CHANGED
@@ -12,6 +12,7 @@ begin
|
|
12
12
|
defer1 = client1.query "SELECT sleep(0.1) as first_query"
|
13
13
|
defer1.callback do |result|
|
14
14
|
results << result.first
|
15
|
+
client1.close
|
15
16
|
EM.stop_event_loop
|
16
17
|
end
|
17
18
|
|
@@ -19,6 +20,7 @@ begin
|
|
19
20
|
defer2 = client2.query "SELECT sleep(0.025) second_query"
|
20
21
|
defer2.callback do |result|
|
21
22
|
results << result.first
|
23
|
+
client2.close
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
@@ -34,8 +36,9 @@ begin
|
|
34
36
|
defer1.callback do |result|
|
35
37
|
results << result.first
|
36
38
|
defer2 = client.query "SELECT sleep(0.025) as second_query"
|
37
|
-
defer2.callback do |
|
38
|
-
results <<
|
39
|
+
defer2.callback do |r|
|
40
|
+
results << r.first
|
41
|
+
client.close
|
39
42
|
EM.stop_event_loop
|
40
43
|
end
|
41
44
|
end
|
@@ -51,6 +54,7 @@ begin
|
|
51
54
|
client = Mysql2::EM::Client.new DatabaseCredentials['root']
|
52
55
|
defer = client.query "SELECT sleep(0.1) as first_query"
|
53
56
|
defer.callback do |result|
|
57
|
+
client.close
|
54
58
|
raise 'some error'
|
55
59
|
end
|
56
60
|
defer.errback do |err|
|
data/spec/mysql2/client_spec.rb
CHANGED
@@ -2,10 +2,6 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Mysql2::Client do
|
5
|
-
before(:each) do
|
6
|
-
@client = Mysql2::Client.new DatabaseCredentials['root']
|
7
|
-
end
|
8
|
-
|
9
5
|
it "should raise an exception upon connection failure" do
|
10
6
|
lambda {
|
11
7
|
# The odd local host IP address forces the mysql client library to
|
@@ -17,17 +13,17 @@ describe Mysql2::Client do
|
|
17
13
|
if defined? Encoding
|
18
14
|
it "should raise an exception on create for invalid encodings" do
|
19
15
|
lambda {
|
20
|
-
|
16
|
+
Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "fake"))
|
21
17
|
}.should raise_error(Mysql2::Error)
|
22
18
|
end
|
23
19
|
|
24
20
|
it "should not raise an exception on create for a valid encoding" do
|
25
21
|
lambda {
|
26
|
-
|
22
|
+
Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
|
27
23
|
}.should_not raise_error(Mysql2::Error)
|
28
24
|
|
29
25
|
lambda {
|
30
|
-
|
26
|
+
Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
|
31
27
|
}.should_not raise_error(Mysql2::Error)
|
32
28
|
end
|
33
29
|
end
|
@@ -88,6 +84,8 @@ describe Mysql2::Client do
|
|
88
84
|
results[1]['Value'].should_not be_nil
|
89
85
|
results[1]['Value'].should be_kind_of(String)
|
90
86
|
results[1]['Value'].should_not be_empty
|
87
|
+
|
88
|
+
ssl_client.close
|
91
89
|
end
|
92
90
|
|
93
91
|
it "should respond to #close" do
|
@@ -111,20 +109,16 @@ describe Mysql2::Client do
|
|
111
109
|
|
112
110
|
context "#warning_count" do
|
113
111
|
context "when no warnings" do
|
114
|
-
before(:each) do
|
115
|
-
@client.query('select 1')
|
116
|
-
end
|
117
112
|
it "should 0" do
|
113
|
+
@client.query('select 1')
|
118
114
|
@client.warning_count.should == 0
|
119
115
|
end
|
120
116
|
end
|
121
117
|
context "when has a warnings" do
|
122
|
-
|
118
|
+
it "should > 0" do
|
123
119
|
# "the statement produces extra information that can be viewed by issuing a SHOW WARNINGS"
|
124
120
|
# http://dev.mysql.com/doc/refman/5.0/en/explain-extended.html
|
125
121
|
@client.query("explain extended select 1")
|
126
|
-
end
|
127
|
-
it "should > 0" do
|
128
122
|
@client.warning_count.should > 0
|
129
123
|
end
|
130
124
|
end
|
@@ -136,32 +130,25 @@ describe Mysql2::Client do
|
|
136
130
|
|
137
131
|
context "#query_info" do
|
138
132
|
context "when no info present" do
|
139
|
-
before(:each) do
|
140
|
-
@client.query('select 1')
|
141
|
-
end
|
142
133
|
it "should 0" do
|
134
|
+
@client.query('select 1')
|
143
135
|
@client.query_info.should be_empty
|
144
136
|
@client.query_info_string.should be_nil
|
145
137
|
end
|
146
138
|
end
|
147
139
|
context "when has some info" do
|
148
|
-
|
140
|
+
it "should retrieve it" do
|
149
141
|
@client.query "USE test"
|
150
142
|
@client.query "CREATE TABLE IF NOT EXISTS infoTest (`id` int(11) NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))"
|
151
|
-
end
|
152
|
-
|
153
|
-
after(:each) do
|
154
|
-
@client.query "DROP TABLE infoTest"
|
155
|
-
end
|
156
143
|
|
157
|
-
before(:each) do
|
158
144
|
# http://dev.mysql.com/doc/refman/5.0/en/mysql-info.html says
|
159
145
|
# # Note that mysql_info() returns a non-NULL value for INSERT ... VALUES only for the multiple-row form of the statement (that is, only if multiple value lists are specified).
|
160
146
|
@client.query("INSERT INTO infoTest (blah) VALUES (1234),(4535)")
|
161
|
-
|
162
|
-
|
163
|
-
@client.
|
164
|
-
|
147
|
+
|
148
|
+
@client.query_info.should eql({:records => 2, :duplicates => 0, :warnings => 0})
|
149
|
+
@client.query_info_string.should eq('Records: 2 Duplicates: 0 Warnings: 0')
|
150
|
+
|
151
|
+
@client.query "DROP TABLE infoTest"
|
165
152
|
end
|
166
153
|
end
|
167
154
|
end
|
@@ -186,7 +173,7 @@ describe Mysql2::Client do
|
|
186
173
|
|
187
174
|
context "#query" do
|
188
175
|
it "should let you query again if iterating is finished when streaming" do
|
189
|
-
@client.query("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false).each
|
176
|
+
@client.query("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false).each.to_a
|
190
177
|
|
191
178
|
expect {
|
192
179
|
@client.query("SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false)
|
@@ -277,30 +264,32 @@ describe Mysql2::Client do
|
|
277
264
|
}.should raise_error(Mysql2::Error)
|
278
265
|
end
|
279
266
|
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
267
|
+
if !defined? Rubinius
|
268
|
+
# XXX this test is not deterministic (because Unix signal handling is not)
|
269
|
+
# and may fail on a loaded system
|
270
|
+
it "should run signal handlers while waiting for a response" do
|
271
|
+
mark = {}
|
272
|
+
trap(:USR1) { mark[:USR1] = Time.now }
|
273
|
+
begin
|
274
|
+
mark[:START] = Time.now
|
275
|
+
pid = fork do
|
276
|
+
sleep 1 # wait for client "SELECT sleep(2)" query to start
|
277
|
+
Process.kill(:USR1, Process.ppid)
|
278
|
+
sleep # wait for explicit kill to prevent GC disconnect
|
279
|
+
end
|
280
|
+
@client.query("SELECT sleep(2)")
|
281
|
+
mark[:END] = Time.now
|
282
|
+
mark.include?(:USR1).should be_true
|
283
|
+
(mark[:USR1] - mark[:START]).should >= 1
|
284
|
+
(mark[:USR1] - mark[:START]).should < 1.3
|
285
|
+
(mark[:END] - mark[:USR1]).should > 0.9
|
286
|
+
(mark[:END] - mark[:START]).should >= 2
|
287
|
+
(mark[:END] - mark[:START]).should < 2.3
|
288
|
+
Process.kill(:TERM, pid)
|
289
|
+
Process.waitpid2(pid)
|
290
|
+
ensure
|
291
|
+
trap(:USR1, 'DEFAULT')
|
291
292
|
end
|
292
|
-
@client.query("SELECT sleep(2)")
|
293
|
-
mark[:END] = Time.now
|
294
|
-
mark.include?(:USR1).should be_true
|
295
|
-
(mark[:USR1] - mark[:START]).should >= 1
|
296
|
-
(mark[:USR1] - mark[:START]).should < 1.3
|
297
|
-
(mark[:END] - mark[:USR1]).should > 0.9
|
298
|
-
(mark[:END] - mark[:START]).should >= 2
|
299
|
-
(mark[:END] - mark[:START]).should < 2.3
|
300
|
-
Process.kill(:TERM, pid)
|
301
|
-
Process.waitpid2(pid)
|
302
|
-
ensure
|
303
|
-
trap(:USR1, 'DEFAULT')
|
304
293
|
end
|
305
294
|
end
|
306
295
|
|
@@ -419,12 +408,12 @@ describe Mysql2::Client do
|
|
419
408
|
end
|
420
409
|
|
421
410
|
it "returns multiple result sets" do
|
422
|
-
@multi_client.query( "select 1 as 'set_1'; select 2 as 'set_2'").first.should
|
411
|
+
@multi_client.query( "select 1 as 'set_1'; select 2 as 'set_2'").first.should eql({ 'set_1' => 1 })
|
423
412
|
|
424
|
-
@multi_client.next_result.should
|
425
|
-
@multi_client.store_result.first.should
|
413
|
+
@multi_client.next_result.should be_true
|
414
|
+
@multi_client.store_result.first.should eql({ 'set_2' => 2 })
|
426
415
|
|
427
|
-
@multi_client.next_result.should
|
416
|
+
@multi_client.next_result.should be_false
|
428
417
|
end
|
429
418
|
|
430
419
|
it "does not interfere with other statements" do
|
@@ -453,12 +442,12 @@ describe Mysql2::Client do
|
|
453
442
|
|
454
443
|
it "#more_results? should work" do
|
455
444
|
@multi_client.query( "select 1 as 'set_1'; select 2 as 'set_2'")
|
456
|
-
@multi_client.more_results?.should
|
445
|
+
@multi_client.more_results?.should be_true
|
457
446
|
|
458
447
|
@multi_client.next_result
|
459
448
|
@multi_client.store_result
|
460
449
|
|
461
|
-
@multi_client.more_results?.should
|
450
|
+
@multi_client.more_results?.should be_false
|
462
451
|
end
|
463
452
|
end
|
464
453
|
end
|
@@ -621,11 +610,11 @@ describe Mysql2::Client do
|
|
621
610
|
|
622
611
|
it "should raise a Mysql2::Error exception upon connection failure" do
|
623
612
|
lambda {
|
624
|
-
|
613
|
+
Mysql2::Client.new :host => "localhost", :username => 'asdfasdf8d2h', :password => 'asdfasdfw42'
|
625
614
|
}.should raise_error(Mysql2::Error)
|
626
615
|
|
627
616
|
lambda {
|
628
|
-
|
617
|
+
Mysql2::Client.new DatabaseCredentials['root']
|
629
618
|
}.should_not raise_error(Mysql2::Error)
|
630
619
|
end
|
631
620
|
|
data/spec/mysql2/error_spec.rb
CHANGED
@@ -3,18 +3,22 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe Mysql2::Error do
|
5
5
|
before(:each) do
|
6
|
-
@client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
|
7
6
|
begin
|
8
|
-
@
|
7
|
+
@err_client = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "utf8"))
|
8
|
+
@err_client.query("HAHAHA")
|
9
9
|
rescue Mysql2::Error => e
|
10
10
|
@error = e
|
11
|
+
ensure
|
12
|
+
@err_client.close
|
11
13
|
end
|
12
14
|
|
13
|
-
@client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
|
14
15
|
begin
|
15
|
-
@
|
16
|
+
@err_client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "big5"))
|
17
|
+
@err_client2.query("HAHAHA")
|
16
18
|
rescue Mysql2::Error => e
|
17
19
|
@error2 = e
|
20
|
+
ensure
|
21
|
+
@err_client2.close
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
@@ -38,8 +42,8 @@ describe Mysql2::Error do
|
|
38
42
|
unless RUBY_VERSION =~ /1.8/
|
39
43
|
it "#message encoding should match the connection's encoding, or Encoding.default_internal if set" do
|
40
44
|
if Encoding.default_internal.nil?
|
41
|
-
@error.message.encoding.should eql(@
|
42
|
-
@error2.message.encoding.should eql(@
|
45
|
+
@error.message.encoding.should eql(@err_client.encoding)
|
46
|
+
@error2.message.encoding.should eql(@err_client2.encoding)
|
43
47
|
else
|
44
48
|
@error.message.encoding.should eql(Encoding.default_internal)
|
45
49
|
@error2.message.encoding.should eql(Encoding.default_internal)
|
@@ -48,8 +52,8 @@ describe Mysql2::Error do
|
|
48
52
|
|
49
53
|
it "#error encoding should match the connection's encoding, or Encoding.default_internal if set" do
|
50
54
|
if Encoding.default_internal.nil?
|
51
|
-
@error.error.encoding.should eql(@
|
52
|
-
@error2.error.encoding.should eql(@
|
55
|
+
@error.error.encoding.should eql(@err_client.encoding)
|
56
|
+
@error2.error.encoding.should eql(@err_client2.encoding)
|
53
57
|
else
|
54
58
|
@error.error.encoding.should eql(Encoding.default_internal)
|
55
59
|
@error2.error.encoding.should eql(Encoding.default_internal)
|
@@ -58,8 +62,8 @@ describe Mysql2::Error do
|
|
58
62
|
|
59
63
|
it "#sql_state encoding should match the connection's encoding, or Encoding.default_internal if set" do
|
60
64
|
if Encoding.default_internal.nil?
|
61
|
-
@error.sql_state.encoding.should eql(@
|
62
|
-
@error2.sql_state.encoding.should eql(@
|
65
|
+
@error.sql_state.encoding.should eql(@err_client.encoding)
|
66
|
+
@error2.sql_state.encoding.should eql(@err_client2.encoding)
|
63
67
|
else
|
64
68
|
@error.sql_state.encoding.should eql(Encoding.default_internal)
|
65
69
|
@error2.sql_state.encoding.should eql(Encoding.default_internal)
|
data/spec/mysql2/result_spec.rb
CHANGED
@@ -2,10 +2,6 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Mysql2::Result do
|
5
|
-
before(:each) do
|
6
|
-
@client = Mysql2::Client.new DatabaseCredentials['root']
|
7
|
-
end
|
8
|
-
|
9
5
|
before(:each) do
|
10
6
|
@result = @client.query "SELECT 1"
|
11
7
|
end
|
@@ -14,7 +10,7 @@ describe Mysql2::Result do
|
|
14
10
|
result = @client.query('SELECT 1')
|
15
11
|
|
16
12
|
result.count.should eql(1)
|
17
|
-
result.each
|
13
|
+
result.each.to_a
|
18
14
|
result.count.should eql(1)
|
19
15
|
end
|
20
16
|
|
@@ -35,7 +31,7 @@ describe Mysql2::Result do
|
|
35
31
|
@client.query "USE test"
|
36
32
|
result = @client.query("SELECT * FROM mysql2_test WHERE null_test IS NOT NULL", :stream => true, :cache_rows => false)
|
37
33
|
result.count.should eql(0)
|
38
|
-
result.each
|
34
|
+
result.each.to_a
|
39
35
|
result.count.should eql(0)
|
40
36
|
end
|
41
37
|
|
@@ -117,8 +113,8 @@ describe Mysql2::Result do
|
|
117
113
|
result = @client.query "SELECT 1 UNION SELECT 2", :stream => true, :cache_rows => false
|
118
114
|
|
119
115
|
expect {
|
120
|
-
result.each
|
121
|
-
result.each
|
116
|
+
result.each.to_a
|
117
|
+
result.each.to_a
|
122
118
|
}.to raise_exception(Mysql2::Error)
|
123
119
|
end
|
124
120
|
end
|
@@ -148,11 +144,11 @@ describe Mysql2::Result do
|
|
148
144
|
it "should return nil values for NULL and strings for everything else when :cast is false" do
|
149
145
|
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
|
150
146
|
result["null_test"].should be_nil
|
151
|
-
result["tiny_int_test"].should
|
152
|
-
result["bool_cast_test"].should
|
153
|
-
result["int_test"].should
|
154
|
-
result["date_test"].should
|
155
|
-
result["enum_test"].should
|
147
|
+
result["tiny_int_test"].should eql("1")
|
148
|
+
result["bool_cast_test"].should eql("1")
|
149
|
+
result["int_test"].should eql("10")
|
150
|
+
result["date_test"].should eql("2010-04-04")
|
151
|
+
result["enum_test"].should eql("val1")
|
156
152
|
end
|
157
153
|
|
158
154
|
it "should return nil for a NULL value" do
|
@@ -330,6 +326,7 @@ describe Mysql2::Result do
|
|
330
326
|
client2.query "USE test"
|
331
327
|
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
332
328
|
result['enum_test'].encoding.should eql(Encoding.find('us-ascii'))
|
329
|
+
client2.close
|
333
330
|
end
|
334
331
|
|
335
332
|
it "should use Encoding.default_internal" do
|
@@ -359,6 +356,7 @@ describe Mysql2::Result do
|
|
359
356
|
client2.query "USE test"
|
360
357
|
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
361
358
|
result['set_test'].encoding.should eql(Encoding.find('us-ascii'))
|
359
|
+
client2.close
|
362
360
|
end
|
363
361
|
|
364
362
|
it "should use Encoding.default_internal" do
|
@@ -441,6 +439,7 @@ describe Mysql2::Result do
|
|
441
439
|
client2.query "USE test"
|
442
440
|
result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first
|
443
441
|
result[field].encoding.should eql(Encoding.find('us-ascii'))
|
442
|
+
client2.close
|
444
443
|
end
|
445
444
|
|
446
445
|
it "should use Encoding.default_internal" do
|
data/spec/spec_helper.rb
CHANGED
@@ -7,6 +7,14 @@ require 'yaml'
|
|
7
7
|
DatabaseCredentials = YAML.load_file('spec/configuration.yml')
|
8
8
|
|
9
9
|
RSpec.configure do |config|
|
10
|
+
config.before :each do
|
11
|
+
@client = Mysql2::Client.new DatabaseCredentials['root']
|
12
|
+
end
|
13
|
+
|
14
|
+
config.after :each do
|
15
|
+
@client.close
|
16
|
+
end
|
17
|
+
|
10
18
|
config.before(:all) do
|
11
19
|
client = Mysql2::Client.new DatabaseCredentials['root']
|
12
20
|
client.query %[
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.13
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|