// Convert C-string to a Ruby instance of Ruby type "type" static VALUE typecast(const char* value, unsigned long length, char* type) { if (NULL == value) return Qnil; if ( strcmp(type, "Class") == 0) { return rb_funcall(mDO, rb_intern("find_const"), 1, TAINTED_STRING(value, length)); } else if ( strcmp(type, "Integer") == 0 || strcmp(type, "Fixnum") == 0 || strcmp(type, "Bignum") == 0 ) { return rb_cstr2inum(value, 10); } else if (0 == strcmp("String", type)) { return TAINTED_STRING(value, length); } else if (0 == strcmp("Float", type) ) { return rb_float_new(rb_cstr_to_dbl(value, Qfalse)); } else if (0 == strcmp("BigDecimal", type) ) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, TAINTED_STRING(value, length)); } else if (0 == strcmp("TrueClass", type) || 0 == strcmp("FalseClass", type)) { return (0 == value || 0 == strcmp("0", value)) ? Qfalse : Qtrue; } else if (0 == strcmp("Date", type)) { return parse_date(value); } else if (0 == strcmp("DateTime", type)) { return parse_date_time(value); } else if (0 == strcmp("Time", type)) { return parse_time(value); } else { return TAINTED_STRING(value, length); } }
static VALUE typecast(sqlite3_stmt *stmt, int i, VALUE ruby_class) { const char *ruby_type; VALUE ruby_value = Qnil; int original_type = sqlite3_column_type(stmt, i); int length = sqlite3_column_bytes(stmt, i); if ( original_type == SQLITE_NULL ) { return ruby_value; } if ( original_type == SQLITE_BLOB ) { return TAINTED_STRING((char*)sqlite3_column_blob(stmt, i), length); } if(ruby_class == Qnil) { switch(original_type) { case SQLITE_INTEGER: { ruby_type = "Integer"; break; } case SQLITE_FLOAT: { ruby_type = "Float"; break; } default: { ruby_type = "String"; break; } } } else { ruby_type = rb_class2name(ruby_class); } if ( strcmp(ruby_type, "Class") == 0) { return rb_funcall(rb_cObject, rb_intern("full_const_get"), 1, TAINTED_STRING((char*)sqlite3_column_text(stmt, i), length)); } else if ( strcmp(ruby_type, "Object") == 0 ) { return rb_marshal_load(rb_str_new2((char*)sqlite3_column_text(stmt, i))); } else if ( strcmp(ruby_type, "TrueClass") == 0 ) { return strcmp((char*)sqlite3_column_text(stmt, i), "t") == 0 ? Qtrue : Qfalse; } else if ( strcmp(ruby_type, "Integer") == 0 || strcmp(ruby_type, "Fixnum") == 0 || strcmp(ruby_type, "Bignum") == 0 ) { return LL2NUM(sqlite3_column_int64(stmt, i)); } else if ( strcmp(ruby_type, "BigDecimal") == 0 ) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, TAINTED_STRING((char*)sqlite3_column_text(stmt, i), length)); } else if ( strcmp(ruby_type, "Float") == 0 ) { return rb_float_new(sqlite3_column_double(stmt, i)); } else if ( strcmp(ruby_type, "Date") == 0 ) { return parse_date((char*)sqlite3_column_text(stmt, i)); } else if ( strcmp(ruby_type, "DateTime") == 0 ) { return parse_date_time((char*)sqlite3_column_text(stmt, i)); } else if ( strcmp(ruby_type, "Time") == 0 ) { return parse_time((char*)sqlite3_column_text(stmt, i)); } else { return TAINTED_STRING((char*)sqlite3_column_text(stmt, i), length); } }