Skip to content

Commit

Permalink
Backport changes from brianmario#755
Browse files Browse the repository at this point in the history
  • Loading branch information
Cees de Groot committed May 25, 2016
1 parent 329b63b commit e84fb5f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
33 changes: 33 additions & 0 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
VALUE cMysql2Client;
extern VALUE mMysql2, cMysql2Error;
static VALUE sym_id, sym_version, sym_header_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
static VALUE sym_no_good_index_used, sym_no_index_used, sym_query_was_slow;
static ID intern_merge, intern_merge_bang, intern_error_number_eql, intern_sql_state_eql;

#ifndef HAVE_RB_HASH_DUP
Expand Down Expand Up @@ -511,6 +512,8 @@ static VALUE rb_mysql_client_async_result(VALUE self) {
Check_Type(current, T_HASH);
resultObj = rb_mysql_result_to_obj(self, wrapper->encoding, current, result);

rb_mysql_set_server_query_flags(wrapper->client, resultObj);

return resultObj;
}

Expand Down Expand Up @@ -1289,6 +1292,10 @@ void init_mysql2_client() {
sym_array = ID2SYM(rb_intern("array"));
sym_stream = ID2SYM(rb_intern("stream"));

sym_no_good_index_used = ID2SYM(rb_intern("no_good_index_used"));
sym_no_index_used = ID2SYM(rb_intern("no_index_used"));
sym_query_was_slow = ID2SYM(rb_intern("query_was_slow"));

intern_merge = rb_intern("merge");
intern_merge_bang = rb_intern("merge!");
intern_error_number_eql = rb_intern("error_number=");
Expand Down Expand Up @@ -1404,3 +1411,29 @@ void init_mysql2_client() {
LONG2NUM(CLIENT_BASIC_FLAGS));
#endif
}

#define flag_to_bool(f) ((client->server_status & f) ? Qtrue : Qfalse)

void rb_mysql_set_server_query_flags(MYSQL *client, VALUE result) {
VALUE server_flags = rb_hash_new();

#ifdef SERVER_QUERY_NO_GOOD_INDEX_USED
rb_hash_aset(server_flags, sym_no_good_index_used, flag_to_bool(SERVER_QUERY_NO_GOOD_INDEX_USED));
#else
rb_hash_aset(server_flags, sym_no_good_index_used, Qnil);
#endif

#ifdef SERVER_QUERY_NO_INDEX_USED
rb_hash_aset(server_flags, sym_no_index_used, flag_to_bool(SERVER_QUERY_NO_INDEX_USED));
#else
rb_hash_aset(server_flags, sym_no_index_used, Qnil);
#endif

#ifdef SERVER_QUERY_WAS_SLOW
rb_hash_aset(server_flags, sym_query_was_slow, flag_to_bool(SERVER_QUERY_WAS_SLOW));
#else
rb_hash_aset(server_flags, sym_query_was_slow, Qnil);
#endif

rb_iv_set(result, "@server_flags", server_flags);
}
2 changes: 2 additions & 0 deletions ext/mysql2/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ typedef struct {
void init_mysql2_client();
void decr_mysql2_client(mysql_client_wrapper *wrapper);

void rb_mysql_set_server_query_flags(MYSQL *client, VALUE result);

#endif
2 changes: 2 additions & 0 deletions lib/mysql2/result.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Mysql2
class Result
attr_reader :server_flags

include Enumerable
end
end
2 changes: 1 addition & 1 deletion lib/mysql2/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Mysql2
VERSION = "0.3.21"
VERSION = "0.3.21-pd1"
end
15 changes: 15 additions & 0 deletions spec/mysql2/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,19 @@
end
end
end
context "server flags" do
before(:each) do
@test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1")
end

it "should set a definitive value for query_was_slow" do
@test_result.server_flags[:query_was_slow].should eql(false)
end
it "should set a definitive value for no_index_used" do
@test_result.server_flags[:no_index_used].should eql(true)
end
it "should set a definitive value for no_good_index_used" do
@test_result.server_flags[:no_good_index_used].should eql(false)
end
end
end

0 comments on commit e84fb5f

Please sign in to comment.