Skip to content

Commit

Permalink
Move server flags to result object
Browse files Browse the repository at this point in the history
  • Loading branch information
Cees de Groot committed May 24, 2016
1 parent e7ea748 commit 57baabb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
26 changes: 19 additions & 7 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ 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, Qnil);

rb_iv_set(self, "@server_status", INT2NUM(wrapper->client->server_status));
rb_mysql_set_server_query_flags(wrapper->client, resultObj);

return resultObj;
}
Expand Down Expand Up @@ -1466,19 +1466,31 @@ void init_mysql2_client() {
rb_const_set(cMysql2Client, rb_intern("BASIC_FLAGS"),
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_const_set(cMysql2Client, rb_intern("NO_GOOD_INDEX_USED"),
LONG2NUM(SERVER_QUERY_NO_GOOD_INDEX_USED));
rb_hash_aset(server_flags, ID2SYM(rb_intern("no_good_index_used")), flag_to_bool(SERVER_QUERY_NO_GOOD_INDEX_USED));
#else
rb_hash_aset(server_flags, ID2SYM(rb_intern("no_good_index_used")), Qnil);
#endif

#ifdef SERVER_QUERY_NO_INDEX_USED
rb_const_set(cMysql2Client, rb_intern("NO_INDEX_USED"),
LONG2NUM(SERVER_QUERY_NO_INDEX_USED));
rb_hash_aset(server_flags, ID2SYM(rb_intern("no_index_used")), flag_to_bool(SERVER_QUERY_NO_INDEX_USED));
#else
rb_hash_aset(server_flags, ID2SYM(rb_intern("no_index_used")), Qnil);
#endif

#ifdef SERVER_QUERY_WAS_SLOW
rb_const_set(cMysql2Client, rb_intern("QUERY_WAS_SLOW"),
LONG2NUM(SERVER_QUERY_WAS_SLOW));
rb_hash_aset(server_flags, ID2SYM(rb_intern("query_was_slow")), flag_to_bool(SERVER_QUERY_WAS_SLOW));
#else
rb_hash_aset(server_flags, ID2SYM(rb_intern("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 @@ -58,6 +58,7 @@ typedef struct {
}

void rb_mysql_client_set_active_thread(VALUE self);
void rb_mysql_set_server_query_flags(MYSQL *client, VALUE result);

#define GET_CLIENT(self) \
mysql_client_wrapper *wrapper; \
Expand All @@ -71,3 +72,4 @@ void decr_mysql2_client(mysql_client_wrapper *wrapper);
#ifndef HAVE_RB_HASH_DUP
VALUE rb_hash_dup(VALUE other);
#endif

4 changes: 2 additions & 2 deletions ext/mysql2/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {

FREE_BINDS;

rb_iv_set(self, "@server_status", INT2NUM(wrapper->client->server_status));

metadata = mysql_stmt_result_metadata(stmt);
if (metadata == NULL) {
if (mysql_stmt_errno(stmt) != 0) {
Expand Down Expand Up @@ -381,6 +379,8 @@ static VALUE execute(int argc, VALUE *argv, VALUE self) {

resultObj = rb_mysql_result_to_obj(stmt_wrapper->client, wrapper->encoding, current, metadata, self);

rb_mysql_set_server_query_flags(wrapper->client, resultObj);

if (!is_streaming) {
// cache all result
rb_funcall(resultObj, intern_each, 0);
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql2/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Mysql2
class Client
attr_reader :query_options, :read_timeout, :server_status
attr_reader :query_options, :read_timeout

def self.default_query_options
@default_query_options ||= {
Expand Down
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
16 changes: 16 additions & 0 deletions spec/mysql2/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,20 @@
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
expect(@test_result.server_flags[:query_was_slow]).to eql(false)
end
it "should set a definitive value for no_index_used" do
expect(@test_result.server_flags[:no_index_used]).to eql(true)
end
it "should set a definitive value for no_good_index_used" do
expect(@test_result.server_flags[:no_good_index_used]).to eql(false)
end
end
end

0 comments on commit 57baabb

Please sign in to comment.