Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make server_status available #755

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ext/mysql2/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,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, Qnil);

rb_mysql_set_server_query_flags(wrapper->client, resultObj);

return resultObj;
}

Expand Down Expand Up @@ -1465,3 +1467,30 @@ 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, ID2SYM(rb_intern("no_good_index_used")), flag_to_bool(SERVER_QUERY_NO_GOOD_INDEX_USED));
Copy link
Collaborator

@sodabrew sodabrew May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add these symbols as static VALUEs at the top of the file and in the module init? You should see a laundry list of sym_foo values initialized around line 1350.

#else
rb_hash_aset(server_flags, ID2SYM(rb_intern("no_good_index_used")), Qnil);
#endif

#ifdef 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_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

2 changes: 2 additions & 0 deletions ext/mysql2/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,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: 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