respect Encoding.default_internal for strings returned by Mysql2::Client#info and Mysql2::Client#server_info

This commit is contained in:
Brian Lopez 2010-06-14 18:14:28 -07:00
parent d1379d2fdd
commit d033823b3c
2 changed files with 57 additions and 4 deletions

View File

@ -331,15 +331,29 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
}
static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE self) {
VALUE version = rb_hash_new();
VALUE version = rb_hash_new(), client_info;
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
#endif
rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_client_version()));
rb_hash_aset(version, sym_version, rb_str_new2(mysql_get_client_info()));
client_info = rb_str_new2(mysql_get_client_info());
#ifdef HAVE_RUBY_ENCODING_H
rb_enc_associate(client_info, utf8Encoding);
if (default_internal_enc) {
client_info = rb_str_export_to_enc(client_info, default_internal_enc);
}
#endif
rb_hash_aset(version, sym_version, client_info);
return version;
}
static VALUE rb_mysql_client_server_info(VALUE self) {
MYSQL * client;
VALUE version;
VALUE version, server_info;
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *default_internal_enc = rb_default_internal_encoding();
#endif
GetMysql2Client(self, client);
if (!client) {
@ -348,7 +362,14 @@ static VALUE rb_mysql_client_server_info(VALUE self) {
}
version = rb_hash_new();
rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_server_version(client)));
rb_hash_aset(version, sym_version, rb_str_new2(mysql_get_server_info(client)));
server_info = rb_str_new2(mysql_get_server_info(client));
#ifdef HAVE_RUBY_ENCODING_H
rb_enc_associate(server_info, utf8Encoding);
if (default_internal_enc) {
server_info = rb_str_export_to_enc(server_info, default_internal_enc);
}
#endif
rb_hash_aset(version, sym_version, server_info);
return version;
}

View File

@ -74,6 +74,22 @@ describe Mysql2::Client do
info[:version].class.should eql(String)
end
if RUBY_VERSION =~ /^1.9/
context "strings returned by #info" do
it "should default to utf-8 if Encoding.default_internal is nil" do
Encoding.default_internal = nil
@client.info[:version].encoding.should eql(Encoding.find('utf-8'))
end
it "should use Encoding.default_internal" do
Encoding.default_internal = Encoding.find('utf-8')
@client.info[:version].encoding.should eql(Encoding.default_internal)
Encoding.default_internal = Encoding.find('us-ascii')
@client.info[:version].encoding.should eql(Encoding.default_internal)
end
end
end
it "should respond to #server_info" do
@client.should respond_to :server_info
end
@ -87,6 +103,22 @@ describe Mysql2::Client do
server_info[:version].class.should eql(String)
end
if RUBY_VERSION =~ /^1.9/
context "strings returned by #server_info" do
it "should default to utf-8 if Encoding.default_internal is nil" do
Encoding.default_internal = nil
@client.server_info[:version].encoding.should eql(Encoding.find('utf-8'))
end
it "should use Encoding.default_internal" do
Encoding.default_internal = Encoding.find('utf-8')
@client.server_info[:version].encoding.should eql(Encoding.default_internal)
Encoding.default_internal = Encoding.find('us-ascii')
@client.server_info[:version].encoding.should eql(Encoding.default_internal)
end
end
end
it "should respond to #socket" do
@client.should respond_to :socket
end