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

Commit f8c2402

Browse files
takatoshionosodabrew
authored andcommitted
Fix encoding if nothing was escaped
1 parent ef7db07 commit f8c2402

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

ext/mysql2/client.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,11 @@ static VALUE rb_mysql_client_real_escape(VALUE self, VALUE str) {
739739
newLen = mysql_real_escape_string(wrapper->client, (char *)newStr, RSTRING_PTR(str), oldLen);
740740
if (newLen == oldLen) {
741741
/* no need to return a new ruby string if nothing changed */
742+
#ifdef HAVE_RUBY_ENCODING_H
743+
if (default_internal_enc) {
744+
str = rb_str_export_to_enc(str, default_internal_enc);
745+
}
746+
#endif
742747
xfree(newStr);
743748
return str;
744749
} else {

spec/mysql2/client_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,19 @@ def connect *args
716716
@client.escape ""
717717
}.should raise_error(Mysql2::Error)
718718
end
719+
720+
context 'when mysql encoding is not utf8' do
721+
before { pending('Encoding is undefined') unless defined?(Encoding) }
722+
723+
let(:client) { Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "ujis")) }
724+
725+
it 'should return a internal encoding string if Encoding.default_internal is set' do
726+
with_internal_encoding Encoding::UTF_8 do
727+
client.escape("\u{30C6}\u{30B9}\u{30C8}").should eql("\u{30C6}\u{30B9}\u{30C8}")
728+
client.escape("\u{30C6}'\u{30B9}\"\u{30C8}").should eql("\u{30C6}\\'\u{30B9}\\\"\u{30C8}")
729+
end
730+
end
731+
end
719732
end
720733

721734
it "should respond to #info" do

0 commit comments

Comments
 (0)