Skip to content

Commit 5cd738a

Browse files
committed
Merge pull request #342 from todesking/mysql2_client_warning_count
Add MySQL2::Client#warning_count
2 parents 2d7252b + f45c0f2 commit 5cd738a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ext/mysql2/client.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ static VALUE rb_mysql_client_escape(RB_MYSQL_UNUSED VALUE klass, VALUE str) {
236236
}
237237
}
238238

239+
static VALUE rb_mysql_client_warning_count(VALUE self) {
240+
unsigned int warning_count;
241+
GET_CLIENT(self);
242+
243+
warning_count = mysql_warning_count(wrapper->client);
244+
245+
return UINT2NUM(warning_count);
246+
}
247+
239248
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags) {
240249
struct nogvl_connect_args args;
241250
VALUE rv;
@@ -1110,6 +1119,7 @@ void init_mysql2_client() {
11101119
rb_define_method(cMysql2Client, "next_result", rb_mysql_client_next_result, 0);
11111120
rb_define_method(cMysql2Client, "store_result", rb_mysql_client_store_result, 0);
11121121
rb_define_method(cMysql2Client, "reconnect=", set_reconnect, 1);
1122+
rb_define_method(cMysql2Client, "warning_count", rb_mysql_client_warning_count, 0);
11131123
#ifdef HAVE_RUBY_ENCODING_H
11141124
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
11151125
#endif

spec/mysql2/client_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ def connect *args
103103
@client.should respond_to(:query)
104104
end
105105

106+
it "should respond to #warning_count" do
107+
@client.should respond_to(:warning_count)
108+
end
109+
110+
context "#warning_count" do
111+
context "when no warnings" do
112+
before(:each) do
113+
@client.query('select 1')
114+
end
115+
it "should 0" do
116+
@client.warning_count.should == 0
117+
end
118+
end
119+
context "when has a warnings" do
120+
before(:each) do
121+
# "the statement produces extra information that can be viewed by issuing a SHOW WARNINGS"
122+
# http://dev.mysql.com/doc/refman/5.0/en/explain-extended.html
123+
@client.query("explain extended select 1")
124+
end
125+
it "should > 0" do
126+
@client.warning_count.should > 0
127+
end
128+
end
129+
end
130+
106131
it "should expect connect_timeout to be a positive integer" do
107132
lambda {
108133
Mysql2::Client.new(:connect_timeout => -1)

0 commit comments

Comments
 (0)