From d033823b3c4cd862ea0383678f6647a2eda71473 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Mon, 14 Jun 2010 18:14:28 -0700 Subject: [PATCH] respect Encoding.default_internal for strings returned by Mysql2::Client#info and Mysql2::Client#server_info --- ext/mysql2_ext.c | 29 +++++++++++++++++++++++++---- spec/mysql2/client_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/ext/mysql2_ext.c b/ext/mysql2_ext.c index 6ddffa3..669f076 100644 --- a/ext/mysql2_ext.c +++ b/ext/mysql2_ext.c @@ -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; } diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index bbeeb61..9d022bd 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -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