Skip to content

Commit 7940665

Browse files
committed
Merge pull request #395 from sodabrew/libc_query_info
Add Client#query_info that calls mysql_info.
2 parents a2fc987 + 41f8b32 commit 7940665

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

ext/mysql2/client.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,25 @@ static VALUE rb_mysql_client_warning_count(VALUE self) {
259259
return UINT2NUM(warning_count);
260260
}
261261

262+
static VALUE rb_mysql_info(VALUE self) {
263+
const char *info;
264+
VALUE rb_str;
265+
GET_CLIENT(self);
266+
267+
info = mysql_info(wrapper->client);
268+
269+
if (info == NULL) {
270+
return Qnil;
271+
}
272+
273+
rb_str = rb_str_new2(info);
274+
#ifdef HAVE_RUBY_ENCODING_H
275+
rb_enc_associate(rb_str, rb_utf8_encoding());
276+
#endif
277+
278+
return rb_str;
279+
}
280+
262281
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags) {
263282
struct nogvl_connect_args args;
264283
VALUE rv;
@@ -1124,6 +1143,7 @@ void init_mysql2_client() {
11241143
rb_define_method(cMysql2Client, "store_result", rb_mysql_client_store_result, 0);
11251144
rb_define_method(cMysql2Client, "reconnect=", set_reconnect, 1);
11261145
rb_define_method(cMysql2Client, "warning_count", rb_mysql_client_warning_count, 0);
1146+
rb_define_method(cMysql2Client, "query_info_string", rb_mysql_info, 0);
11271147
#ifdef HAVE_RUBY_ENCODING_H
11281148
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
11291149
#endif

lib/mysql2/client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ def self.default_query_options
6161
@@default_query_options
6262
end
6363

64+
def query_info
65+
info = query_info_string
66+
return {} unless info
67+
info_hash = {}
68+
info.split.each_slice(2) { |s| info_hash[s[0].downcase.delete(':').to_sym] = s[1].to_i }
69+
info_hash
70+
end
71+
6472
private
6573
def self.local_offset
6674
::Time.local(2010).utc_offset.to_r / 86400

spec/mysql2/client_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,42 @@ def connect *args
130130
end
131131
end
132132

133+
it "should respond to #query_info" do
134+
@client.should respond_to(:query_info)
135+
end
136+
137+
context "#query_info" do
138+
context "when no info present" do
139+
before(:each) do
140+
@client.query('select 1')
141+
end
142+
it "should 0" do
143+
@client.query_info.should be_empty
144+
@client.query_info_string.should be_nil
145+
end
146+
end
147+
context "when has some info" do
148+
before(:each) do
149+
@client.query "USE test"
150+
@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+
157+
before(:each) do
158+
# http://dev.mysql.com/doc/refman/5.0/en/mysql-info.html says
159+
# # 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+
@client.query("INSERT INTO infoTest (blah) VALUES (1234),(4535)")
161+
end
162+
it "should retrieve it" do
163+
@client.query_info.should == {:records => 2, :duplicates => 0, :warnings => 0}
164+
@client.query_info_string.should eq 'Records: 2 Duplicates: 0 Warnings: 0'
165+
end
166+
end
167+
end
168+
133169
it "should expect connect_timeout to be a positive integer" do
134170
lambda {
135171
Mysql2::Client.new(:connect_timeout => -1)

0 commit comments

Comments
 (0)