Skip to content

Commit 0d9c806

Browse files
libcsodabrew
authored andcommitted
Add Client#query_info that calls mysql_info.
The function is really useful if you want to retrieve the result of the LOAD DATA INFILE command.
1 parent a2fc987 commit 0d9c806

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-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", rb_mysql_info, 0);
11271147
#ifdef HAVE_RUBY_ENCODING_H
11281148
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
11291149
#endif

spec/mysql2/client_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,40 @@ 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_nil
144+
end
145+
end
146+
context "when has some info" do
147+
before(:each) do
148+
@client.query "USE test"
149+
@client.query "CREATE TABLE IF NOT EXISTS infoTest (`id` int(11) NOT NULL AUTO_INCREMENT, blah INT(11), PRIMARY KEY (`id`))"
150+
end
151+
152+
after(:each) do
153+
@client.query "DROP TABLE infoTest"
154+
end
155+
156+
before(:each) do
157+
# http://dev.mysql.com/doc/refman/5.0/en/mysql-info.html says
158+
# # 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).
159+
@client.query("INSERT INTO infoTest (blah) VALUES (1234),(4535)")
160+
end
161+
it "should retrieve it" do
162+
@client.query_info.should eq 'Records: 2 Duplicates: 0 Warnings: 0'
163+
end
164+
end
165+
end
166+
133167
it "should expect connect_timeout to be a positive integer" do
134168
lambda {
135169
Mysql2::Client.new(:connect_timeout => -1)

0 commit comments

Comments
 (0)