@@ -288,7 +288,14 @@ static VALUE sqlite3val2rb(sqlite3_value * val)
288288 which is what we want, as blobs are binary
289289 */
290290 int len = sqlite3_value_bytes (val );
291+ #ifdef HAVE_RUBY_ENCODING_H
291292 return rb_tainted_str_new ((const char * )sqlite3_value_blob (val ), len );
293+ #else
294+ /* When encoding is not available, make it class SQLite3::Blob. */
295+ VALUE strargv [1 ];
296+ strargv [0 ] = rb_tainted_str_new ((const char * )sqlite3_value_blob (val ), len );
297+ return rb_class_new_instance (1 , strargv , cSqlite3Blob );
298+ #endif
292299 break ;
293300 }
294301 case SQLITE_NULL :
@@ -322,12 +329,25 @@ static void set_sqlite3_func_result(sqlite3_context * ctx, VALUE result)
322329 sqlite3_result_double (ctx , NUM2DBL (result ));
323330 break ;
324331 case T_STRING :
325- sqlite3_result_text (
326- ctx ,
327- (const char * )StringValuePtr (result ),
328- (int )RSTRING_LEN (result ),
329- SQLITE_TRANSIENT
330- );
332+ if (CLASS_OF (result ) == cSqlite3Blob
333+ #ifdef HAVE_RUBY_ENCODING_H
334+ || rb_enc_get_index (result ) == rb_ascii8bit_encindex ()
335+ #endif
336+ ) {
337+ sqlite3_result_blob (
338+ ctx ,
339+ (const void * )StringValuePtr (result ),
340+ (int )RSTRING_LEN (result ),
341+ SQLITE_TRANSIENT
342+ );
343+ } else {
344+ sqlite3_result_text (
345+ ctx ,
346+ (const char * )StringValuePtr (result ),
347+ (int )RSTRING_LEN (result ),
348+ SQLITE_TRANSIENT
349+ );
350+ }
331351 break ;
332352 default :
333353 rb_raise (rb_eRuntimeError , "can't return %s" ,
0 commit comments