Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit ef7db07

Browse files
brianmariosodabrew
authored andcommitted
move Mysql2::Client#info to a class method for easier debugging
1 parent 1a4da92 commit ef7db07

File tree

3 files changed

+32
-39
lines changed

3 files changed

+32
-39
lines changed

ext/mysql2/client.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
VALUE cMysql2Client;
1818
extern VALUE mMysql2, cMysql2Error;
19-
static VALUE sym_id, sym_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
19+
static VALUE sym_id, sym_version, sym_header_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
2020
static ID intern_merge, intern_merge_bang, intern_error_number_eql, intern_sql_state_eql;
2121

2222
#ifndef HAVE_RB_HASH_DUP
@@ -843,30 +843,23 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
843843
*
844844
* Returns a string that represents the client library version.
845845
*/
846-
static VALUE rb_mysql_client_info(VALUE self) {
847-
VALUE version, client_info;
848-
#ifdef HAVE_RUBY_ENCODING_H
849-
rb_encoding *default_internal_enc;
850-
rb_encoding *conn_enc;
851-
GET_CLIENT(self);
852-
#endif
853-
version = rb_hash_new();
846+
static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE klass) {
847+
VALUE version_info, version, header_version;
848+
version_info = rb_hash_new();
854849

855-
#ifdef HAVE_RUBY_ENCODING_H
856-
default_internal_enc = rb_default_internal_encoding();
857-
conn_enc = rb_to_encoding(wrapper->encoding);
858-
#endif
850+
version = rb_str_new2(mysql_get_client_info());
851+
header_version = rb_str_new2(MYSQL_LINK_VERSION);
859852

860-
rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version()));
861-
client_info = rb_str_new2(mysql_get_client_info());
862853
#ifdef HAVE_RUBY_ENCODING_H
863-
rb_enc_associate(client_info, conn_enc);
864-
if (default_internal_enc) {
865-
client_info = rb_str_export_to_enc(client_info, default_internal_enc);
866-
}
854+
rb_enc_associate(version, rb_usascii_encoding());
855+
rb_enc_associate(header_version, rb_usascii_encoding());
867856
#endif
868-
rb_hash_aset(version, sym_version, client_info);
869-
return version;
857+
858+
rb_hash_aset(version_info, sym_id, LONG2NUM(mysql_get_client_version()));
859+
rb_hash_aset(version_info, sym_version, version);
860+
rb_hash_aset(version_info, sym_header_version, header_version);
861+
862+
return version_info;
870863
}
871864

872865
/* call-seq:
@@ -1250,12 +1243,12 @@ void init_mysql2_client() {
12501243
rb_define_alloc_func(cMysql2Client, allocate);
12511244

12521245
rb_define_singleton_method(cMysql2Client, "escape", rb_mysql_client_escape, 1);
1246+
rb_define_singleton_method(cMysql2Client, "info", rb_mysql_client_info, 0);
12531247

12541248
rb_define_method(cMysql2Client, "close", rb_mysql_client_close, 0);
12551249
rb_define_method(cMysql2Client, "query", rb_mysql_client_query, -1);
12561250
rb_define_method(cMysql2Client, "abandon_results!", rb_mysql_client_abandon_results, 0);
12571251
rb_define_method(cMysql2Client, "escape", rb_mysql_client_real_escape, 1);
1258-
rb_define_method(cMysql2Client, "info", rb_mysql_client_info, 0);
12591252
rb_define_method(cMysql2Client, "server_info", rb_mysql_client_server_info, 0);
12601253
rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 0);
12611254
rb_define_method(cMysql2Client, "async_result", rb_mysql_client_async_result, 0);
@@ -1289,6 +1282,7 @@ void init_mysql2_client() {
12891282

12901283
sym_id = ID2SYM(rb_intern("id"));
12911284
sym_version = ID2SYM(rb_intern("version"));
1285+
sym_header_version = ID2SYM(rb_intern("header_version"));
12921286
sym_async = ID2SYM(rb_intern("async"));
12931287
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
12941288
sym_as = ID2SYM(rb_intern("as"));

lib/mysql2/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def query_info
8282
info_hash
8383
end
8484

85+
def info
86+
self.class.info
87+
end
88+
8589
private
8690
def self.local_offset
8791
::Time.local(2010).utc_offset.to_r / 86400

spec/mysql2/client_spec.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -731,26 +731,21 @@ def connect *args
731731
info[:version].class.should eql(String)
732732
end
733733

734-
if defined? Encoding
735-
context "strings returned by #info" do
736-
it "should default to the connection's encoding if Encoding.default_internal is nil" do
737-
with_internal_encoding nil do
738-
@client.info[:version].encoding.should eql(Encoding.find('utf-8'))
734+
context "strings returned by #info" do
735+
before { pending('Encoding is undefined') unless defined?(Encoding) }
739736

740-
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
741-
client2.info[:version].encoding.should eql(Encoding.find('us-ascii'))
742-
end
743-
end
737+
it "should be tagged as ascii" do
738+
@client.info[:version].encoding.should eql(Encoding::US_ASCII)
739+
@client.info[:header_version].encoding.should eql(Encoding::US_ASCII)
740+
end
741+
end
744742

745-
it "should use Encoding.default_internal" do
746-
with_internal_encoding 'utf-8' do
747-
@client.info[:version].encoding.should eql(Encoding.default_internal)
748-
end
743+
context "strings returned by .info" do
744+
before { pending('Encoding is undefined') unless defined?(Encoding) }
749745

750-
with_internal_encoding 'us-ascii' do
751-
@client.info[:version].encoding.should eql(Encoding.default_internal)
752-
end
753-
end
746+
it "should be tagged as ascii" do
747+
Mysql2::Client.info[:version].encoding.should eql(Encoding::US_ASCII)
748+
Mysql2::Client.info[:header_version].encoding.should eql(Encoding::US_ASCII)
754749
end
755750
end
756751

0 commit comments

Comments
 (0)