Permalink
Browse files

sqlite3 abides by default_internal encoding settings

  • Loading branch information...
1 parent 3cf7474 commit 4e4d1fc411b43ef50586f6a519e2303debbf1216 @tenderlove tenderlove committed May 4, 2010
Showing with 22 additions and 0 deletions.
  1. +4 −0 ext/sqlite3/statement.c
  2. +18 −0 test/test_encoding.rb
View
@@ -111,6 +111,7 @@ static VALUE step(VALUE self)
int value, length;
VALUE list;
#ifdef HAVE_RUBY_ENCODING_H
+ rb_encoding * internal_encoding;
int enc_index;
#endif
@@ -125,6 +126,7 @@ static VALUE step(VALUE self)
VALUE db = rb_iv_get(self, "@connection");
VALUE encoding = rb_funcall(db, rb_intern("encoding"), 0);
enc_index = NIL_P(encoding) ? rb_utf8_encindex() : rb_to_encoding_index(encoding);
+ internal_encoding = rb_default_internal_encoding();
}
#endif
@@ -154,6 +156,8 @@ static VALUE step(VALUE self)
);
#ifdef HAVE_RUBY_ENCODING_H
rb_enc_associate_index(str, enc_index);
+ if(internal_encoding)
+ str = rb_str_export_to_enc(str, internal_encoding);
#endif
rb_ary_push(list, str);
}
View
@@ -11,6 +11,24 @@ def setup
@db.execute(@create);
end
+ def test_default_internal_is_honored
+ before_enc = Encoding.default_internal
+
+ str = "壁に耳あり、障子に目あり"
+ stmt = @db.prepare('insert into ex(data) values (?)')
+ stmt.bind_param 1, str
+ stmt.step
+
+ Encoding.default_internal = 'EUC-JP'
+ string = @db.execute('select data from ex').first.first
+
+ assert_equal Encoding.default_internal, string.encoding
+ assert_equal str.encode('EUC-JP'), string
+ assert_equal str, string.encode(str.encoding)
+ ensure
+ Encoding.default_internal = before_enc
+ end
+
def test_blob_is_binary
str = "猫舌"
@db.execute('create table foo(data text)')

0 comments on commit 4e4d1fc

Please sign in to comment.