Skip to content

Commit

Permalink
utf-8 works! yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jan 20, 2010
1 parent f7580a7 commit 8cfa9f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions ext/deebee/deebee.h
Expand Up @@ -2,6 +2,7 @@
#define DEEBEE

#include <ruby.h>
#include <ruby/encoding.h>
#include <sqlite3.h>

extern VALUE mSqlite3;
Expand Down
11 changes: 10 additions & 1 deletion ext/deebee/deebee_statement.c
Expand Up @@ -7,6 +7,11 @@ static VALUE each(VALUE self)
sqlite3_stmt *stmt;

Data_Get_Struct(self, sqlite3_stmt, stmt);

VALUE connection = rb_iv_get(self, "@connection");
VALUE encoding = rb_funcall(connection, rb_intern("encoding"), 0);
int idx = rb_to_encoding_index(encoding);

int value = sqlite3_step(stmt);
while(value != SQLITE_DONE) {
switch(value) {
Expand All @@ -25,7 +30,11 @@ static VALUE each(VALUE self)
rb_ary_push(list, rb_float_new(sqlite3_column_double(stmt, i)));
break;
case SQLITE_TEXT:
rb_ary_push(list, rb_str_new2(sqlite3_column_text(stmt, i)));
{
VALUE str = rb_str_new2(sqlite3_column_text(stmt, i));
rb_enc_associate_index(str, idx);
rb_ary_push(list, str);
}
break;
case SQLITE_BLOB:
rb_ary_push(list, rb_str_new2(sqlite3_column_blob(stmt, i)));
Expand Down
16 changes: 16 additions & 0 deletions test/test_statement.rb
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

require "test/unit"
require "deebee"
require 'tmpdir'
Expand All @@ -10,4 +12,18 @@ def test_step
stmt.each { |row| called = true }
assert called
end

def test_string_encoding
db = SQLite3::DeeBee.open(File.join(Dir.tmpdir, 'foo.db'))
string = nil
test = "日本語は楽しい"

stmt = db.prepare("select '#{test}'")
stmt.each { |row| string = row.first }

utf8 = Encoding.find('UTF-8')
assert_equal utf8, string.encoding
assert_equal test, string
assert_equal db.encoding, string.encoding
end
end

0 comments on commit 8cfa9f9

Please sign in to comment.