static VALUE typecast(const char *value, long length, const VALUE type, int encoding) { if (type == rb_cInteger) { return rb_cstr2inum(value, 10); } else if (type == rb_cString) { return DO_STR_NEW(value, length, encoding); } else if (type == rb_cFloat) { return rb_float_new(rb_cstr_to_dbl(value, Qfalse)); } else if (type == rb_cBigDecimal) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, rb_str_new(value, length)); } else if (type == rb_cDate) { return parse_date(value); } else if (type == rb_cDateTime) { return parse_date_time(value); } else if (type == rb_cTime) { return parse_time(value); } else if (type == rb_cTrueClass) { return *value == 't' ? Qtrue : Qfalse; } else if (type == rb_cByteArray) { size_t new_length = 0; char* unescaped = (char *)PQunescapeBytea((unsigned char*)value, &new_length); VALUE byte_array = rb_funcall(rb_cByteArray, ID_NEW, 1, rb_str_new(unescaped, new_length)); PQfreemem(unescaped); return byte_array; } else if (type == rb_cClass) { return rb_funcall(rb_cObject, rb_intern("full_const_get"), 1, rb_str_new(value, length)); } else if (type == rb_cObject) { return rb_marshal_load(rb_str_new(value, length)); } else if (type == rb_cNilClass) { return Qnil; } else { return DO_STR_NEW(value, length, encoding); } }
// Convert C-string to a Ruby instance of Ruby type "type" static VALUE typecast(const char *value, long length, const VALUE type, int encoding) { if(NULL == value) { return Qnil; } if (type == rb_cInteger) { return rb_cstr2inum(value, 10); } else if (type == rb_cString) { return DO_STR_NEW(value, length, encoding); } else if (type == rb_cFloat) { return rb_float_new(rb_cstr_to_dbl(value, Qfalse)); } else if (type == rb_cBigDecimal) { return rb_funcall(rb_cBigDecimal, ID_NEW, 1, rb_str_new(value, length)); } else if (type == rb_cDate) { return parse_date(value); } else if (type == rb_cDateTime) { return parse_date_time(value); } else if (type == rb_cTime) { return parse_time(value); } else if (type == rb_cTrueClass) { return (0 == value || 0 == strcmp("0", value)) ? Qfalse : Qtrue; } else if (type == rb_cByteArray) { return rb_funcall(rb_cByteArray, ID_NEW, 1, rb_str_new(value, length)); } else if (type == rb_cClass) { return rb_funcall(mDO, rb_intern("full_const_get"), 1, rb_str_new(value, length)); } else if (type == rb_cObject) { return rb_marshal_load(rb_str_new(value, length)); } else if (type == rb_cNilClass) { return Qnil; } else { return DO_STR_NEW(value, length, encoding); } }
static VALUE cConnection_quote_string(VALUE self, VALUE string) { PGconn *db = DATA_PTR(rb_iv_get(self, "@connection")); const char *source = RSTRING_PTR(string); int source_len = RSTRING_LEN(string); char *escaped; int quoted_length = 0; VALUE result; // Allocate space for the escaped version of 'string' // http://www.postgresql.org/docs/8.3/static/libpq-exec.html#LIBPQ-EXEC-ESCAPE-STRING escaped = (char *)calloc(source_len * 2 + 3, sizeof(char)); // Escape 'source' using the current charset in use on the conection 'db' quoted_length = PQescapeStringConn(db, escaped + 1, source, source_len, NULL); // Wrap the escaped string in single-quotes, this is DO's convention escaped[quoted_length + 1] = escaped[0] = '\''; result = DO_STR_NEW(escaped, quoted_length + 2, FIX2INT(rb_iv_get(self, "@encoding_id"))); free(escaped); return result; }
VALUE cConnection_quote_string(VALUE self, VALUE string) { MYSQL *db = DATA_PTR(rb_iv_get(self, "@connection")); const char *source = rb_str_ptr_readonly(string); unsigned long source_len = rb_str_len(string); // Allocate space for the escaped version of 'string'. Use + 3 allocate space for null term. // and the leading and trailing single-quotes. // Thanks to http://www.browardphp.com/mysql_manual_en/manual_MySQL_APIs.html#mysql_real_escape_string char *escaped = calloc((source_len * 2) + 3, sizeof(char)); if (!escaped) { return Qnil; } unsigned long quoted_length; VALUE result; // Escape 'source' using the current encoding in use on the conection 'db' quoted_length = mysql_real_escape_string(db, escaped + 1, source, source_len); // Wrap the escaped string in single-quotes, this is DO's convention escaped[0] = escaped[quoted_length + 1] = '\''; // We don't want to use the internal encoding, because this needs // to go into the database in the connection encoding result = DO_STR_NEW(escaped, quoted_length + 2, FIX2INT(rb_iv_get(self, "@encoding_id")), NULL); free(escaped); return result; }
static VALUE cConnection_quote_string(VALUE self, VALUE string) { MYSQL *db = DATA_PTR(rb_iv_get(self, "@connection")); const char *source = rb_str_ptr_readonly(string); size_t source_len = rb_str_len(string); char *escaped; VALUE result; size_t quoted_length = 0; // Allocate space for the escaped version of 'string'. Use + 3 allocate space for null term. // and the leading and trailing single-quotes. // Thanks to http://www.browardphp.com/mysql_manual_en/manual_MySQL_APIs.html#mysql_real_escape_string escaped = (char *)calloc(source_len * 2 + 3, sizeof(char)); // Escape 'source' using the current encoding in use on the conection 'db' quoted_length = mysql_real_escape_string(db, escaped + 1, source, source_len); // Wrap the escaped string in single-quotes, this is DO's convention escaped[0] = escaped[quoted_length + 1] = '\''; result = DO_STR_NEW(escaped, quoted_length + 2, FIX2INT(rb_iv_get(self, "@encoding_id"))); free(escaped); return result; }