Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
sqlite3 abides by default_internal encoding settings
  • Loading branch information
tenderlove committed May 4, 2010
1 parent 3cf7474 commit 4e4d1fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ext/sqlite3/statement.c
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions test/test_encoding.rb
Expand Up @@ -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)')
Expand Down

0 comments on commit 4e4d1fc

Please sign in to comment.