Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

utf-8 works! yay!

  • Loading branch information...
commit 8cfa9f906a920327f5413ccbd82a66bf3e3b7bee 1 parent f7580a7
@tenderlove authored
View
1  ext/deebee/deebee.h
@@ -2,6 +2,7 @@
#define DEEBEE
#include <ruby.h>
+#include <ruby/encoding.h>
#include <sqlite3.h>
extern VALUE mSqlite3;
View
11 ext/deebee/deebee_statement.c
@@ -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) {
@@ -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)));
View
16 test/test_statement.rb
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
require "test/unit"
require "deebee"
require 'tmpdir'
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.