Permalink
Browse files

Merge pull request #342 from todesking/mysql2_client_warning_count

Add MySQL2::Client#warning_count
  • Loading branch information...
2 parents 2d7252b + f45c0f2 commit 5cd738a1e0e0730ba82bff9fd284af5f78157a1f @brianmario committed Dec 4, 2012
Showing with 35 additions and 0 deletions.
  1. +10 −0 ext/mysql2/client.c
  2. +25 −0 spec/mysql2/client_spec.rb
View
@@ -236,6 +236,15 @@ static VALUE rb_mysql_client_escape(RB_MYSQL_UNUSED VALUE klass, VALUE str) {
}
}
+static VALUE rb_mysql_client_warning_count(VALUE self) {
+ unsigned int warning_count;
+ GET_CLIENT(self);
+
+ warning_count = mysql_warning_count(wrapper->client);
+
+ return UINT2NUM(warning_count);
+}
+
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags) {
struct nogvl_connect_args args;
VALUE rv;
@@ -1110,6 +1119,7 @@ void init_mysql2_client() {
rb_define_method(cMysql2Client, "next_result", rb_mysql_client_next_result, 0);
rb_define_method(cMysql2Client, "store_result", rb_mysql_client_store_result, 0);
rb_define_method(cMysql2Client, "reconnect=", set_reconnect, 1);
+ rb_define_method(cMysql2Client, "warning_count", rb_mysql_client_warning_count, 0);
#ifdef HAVE_RUBY_ENCODING_H
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
#endif
View
@@ -103,6 +103,31 @@ def connect *args
@client.should respond_to(:query)
end
+ it "should respond to #warning_count" do
+ @client.should respond_to(:warning_count)
+ end
+
+ context "#warning_count" do
+ context "when no warnings" do
+ before(:each) do
+ @client.query('select 1')
+ end
+ it "should 0" do
+ @client.warning_count.should == 0
+ end
+ end
+ context "when has a warnings" do
+ before(:each) do
+ # "the statement produces extra information that can be viewed by issuing a SHOW WARNINGS"
+ # http://dev.mysql.com/doc/refman/5.0/en/explain-extended.html
+ @client.query("explain extended select 1")
+ end
+ it "should > 0" do
+ @client.warning_count.should > 0
+ end
+ end
+ end
+
it "should expect connect_timeout to be a positive integer" do
lambda {
Mysql2::Client.new(:connect_timeout => -1)

0 comments on commit 5cd738a

Please sign in to comment.