static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) { VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client)); VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client)); VALUE e; #ifdef HAVE_RUBY_ENCODING_H if (wrapper->server_version < 50500) { /* MySQL < 5.5 uses mixed encoding, just call it binary. */ int err_enc = rb_ascii8bit_encindex(); rb_enc_associate_index(rb_error_msg, err_enc); rb_enc_associate_index(rb_sql_state, err_enc); } else { /* MySQL >= 5.5 uses UTF-8 errors internally and converts them to the connection encoding. */ rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); rb_enc_associate(rb_error_msg, conn_enc); rb_enc_associate(rb_sql_state, conn_enc); if (default_internal_enc) { rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc); rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc); } } #endif e = rb_exc_new3(cMysql2Error, rb_error_msg); rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client))); rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state); rb_exc_raise(e); return Qnil; }
static size_t data_handler(char * stream, size_t size, size_t nmemb, VALUE handler) { size_t str_len = size * nmemb; if(TYPE(handler) == T_STRING) { #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); if (default_internal_enc) { handler = rb_str_export_to_enc(handler, default_internal_enc); } else { handler = rb_str_export_to_enc(handler, utf8Encoding); } #endif rb_str_buf_cat(handler, stream, str_len); } else { VALUE chunk = rb_str_new(stream, str_len); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); if (default_internal_enc) { chunk = rb_str_export_to_enc(chunk, default_internal_enc); } else { chunk = rb_str_export_to_enc(chunk, utf8Encoding); } #endif rb_funcall(handler, rb_intern("call"), 1, chunk); } return str_len; }
static VALUE rb_mysql_client_escape(VALUE self, VALUE str) { VALUE newStr; unsigned long newLen, oldLen; GET_CLIENT(self); REQUIRE_OPEN_DB(wrapper); Check_Type(str, T_STRING); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); // ensure the string is in the encoding the connection is expecting str = rb_str_export_to_enc(str, conn_enc); #endif oldLen = RSTRING_LEN(str); newStr = rb_str_new(0, oldLen*2+1); newLen = mysql_real_escape_string(wrapper->client, RSTRING_PTR(newStr), StringValuePtr(str), oldLen); if (newLen == oldLen) { // no need to return a new ruby string if nothing changed return str; } else { rb_str_resize(newStr, newLen); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(newStr, conn_enc); if (default_internal_enc) { newStr = rb_str_export_to_enc(newStr, default_internal_enc); } #endif return newStr; } }
static VALUE rb_mysql_client_escape(VALUE self, VALUE str) { MYSQL * client; VALUE newStr; unsigned long newLen, oldLen; Check_Type(str, T_STRING); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding")); // ensure the string is in the encoding the connection is expecting str = rb_str_export_to_enc(str, conn_enc); #endif oldLen = RSTRING_LEN(str); char escaped[(oldLen*2)+1]; Data_Get_Struct(self, MYSQL, client); REQUIRE_OPEN_DB(client); newLen = mysql_real_escape_string(client, escaped, StringValuePtr(str), RSTRING_LEN(str)); if (newLen == oldLen) { // no need to return a new ruby string if nothing changed return str; } else { newStr = rb_str_new(escaped, newLen); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(newStr, conn_enc); if (default_internal_enc) { newStr = rb_str_export_to_enc(newStr, default_internal_enc); } #endif return newStr; } }
int rb_comparator_func(void * ctx, int a_len, const void * a, int b_len, const void * b) { VALUE comparator; VALUE a_str; VALUE b_str; VALUE comparison; #ifdef HAVE_RUBY_ENCODING_H rb_encoding * internal_encoding; internal_encoding = rb_default_internal_encoding(); #endif comparator = (VALUE)ctx; a_str = rb_str_new((const char *)a, a_len); b_str = rb_str_new((const char *)b, b_len); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate_index(a_str, rb_utf8_encindex()); rb_enc_associate_index(b_str, rb_utf8_encindex()); if(internal_encoding) { a_str = rb_str_export_to_enc(a_str, internal_encoding); b_str = rb_str_export_to_enc(b_str, internal_encoding); } #endif comparison = rb_funcall(comparator, rb_intern("compare"), 2, a_str, b_str); return NUM2INT(comparison); }
void rb_raise_mysql2_stmt_error(mysql_stmt_wrapper *stmt_wrapper) { VALUE e; GET_CLIENT(stmt_wrapper->client); VALUE rb_error_msg = rb_str_new2(mysql_stmt_error(stmt_wrapper->stmt)); VALUE rb_sql_state = rb_tainted_str_new2(mysql_stmt_sqlstate(stmt_wrapper->stmt)); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *conn_enc; conn_enc = rb_to_encoding(wrapper->encoding); rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_enc_associate(rb_error_msg, conn_enc); rb_enc_associate(rb_sql_state, conn_enc); if (default_internal_enc) { rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc); rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc); } #endif e = rb_funcall(cMysql2Error, intern_new_with_args, 4, rb_error_msg, LONG2FIX(wrapper->server_version), UINT2NUM(mysql_stmt_errno(stmt_wrapper->stmt)), rb_sql_state); rb_exc_raise(e); }
/* * Document-method: new * * call-seq: initialize([:pretty => false[, :indent => ' '][, :terminator => "\n"]]) * * :pretty will enable/disable beautifying or "pretty priting" the output string. * * :indent is the character(s) used to indent the output string. * * :terminator allows you to specify a character to be used as the termination character after a full JSON string has been generated by * the encoder. This would be especially useful when encoding in chunks (via a block or callback during the encode process), to be able to * determine when the last chunk of the current encode is sent. * If you specify this option to be nil, it will be ignored if encoding directly to an IO or simply returning a string. But if a block is used, * the encoder will still pass it - I hope that makes sense ;). */ static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) { yajl_encoder_wrapper * wrapper; yajl_gen_config cfg; VALUE opts, obj, indent; unsigned char *indentString = NULL, *actualIndent = NULL; int beautify = 0, htmlSafe = 0; /* Scan off config vars */ if (rb_scan_args(argc, argv, "01", &opts) == 1) { Check_Type(opts, T_HASH); if (rb_hash_aref(opts, sym_pretty) == Qtrue) { beautify = 1; indent = rb_hash_aref(opts, sym_indent); if (indent != Qnil) { #ifdef HAVE_RUBY_ENCODING_H indent = rb_str_export_to_enc(indent, utf8Encoding); #endif Check_Type(indent, T_STRING); indentString = (unsigned char*)malloc(RSTRING_LEN(indent)+1); memcpy(indentString, RSTRING_PTR(indent), RSTRING_LEN(indent)); indentString[RSTRING_LEN(indent)] = '\0'; actualIndent = indentString; } } if (rb_hash_aref(opts, sym_html_safe) == Qtrue) { htmlSafe = 1; } } if (!indentString) { indentString = defaultIndentString; } cfg = (yajl_gen_config){beautify, (const char *)indentString, htmlSafe}; obj = Data_Make_Struct(klass, yajl_encoder_wrapper, yajl_encoder_wrapper_mark, yajl_encoder_wrapper_free, wrapper); wrapper->indentString = actualIndent; wrapper->encoder = yajl_gen_alloc(&cfg, NULL); wrapper->on_progress_callback = Qnil; if (opts != Qnil && rb_funcall(opts, intern_has_key, 1, sym_terminator) == Qtrue) { wrapper->terminator = rb_hash_aref(opts, sym_terminator); #ifdef HAVE_RUBY_ENCODING_H if (TYPE(wrapper->terminator) == T_STRING) { wrapper->terminator = rb_str_export_to_enc(wrapper->terminator, utf8Encoding); } #endif } else { wrapper->terminator = 0; } rb_obj_call_init(obj, 0, 0); return obj; }
static VALUE rb_yajl_encoder_new(int argc, VALUE * argv, VALUE klass) { yajl_encoder_wrapper * wrapper; yajl_gen_config cfg; VALUE opts, obj, indent; const char * indentString = " "; int beautify = 0; int ascii_only = default_to_ascii_only == Qtrue ? 1 : 0; /* Scan off config vars */ if (rb_scan_args(argc, argv, "01", &opts) == 1) { Check_Type(opts, T_HASH); if (rb_hash_aref(opts, sym_pretty) == Qtrue) { beautify = 1; indent = rb_hash_aref(opts, sym_indent); if (indent != Qnil) { #ifdef HAVE_RUBY_ENCODING_H indent = rb_str_export_to_enc(indent, utf8Encoding); #endif Check_Type(indent, T_STRING); indentString = RSTRING_PTR(indent); } } if (rb_hash_aref(opts, sym_ascii_only) == Qtrue) { ascii_only= 1; } if (rb_hash_aref(opts, sym_ascii_only) == Qfalse) { ascii_only= 0; } } cfg = (yajl_gen_config){beautify, indentString, ascii_only}; obj = Data_Make_Struct(klass, yajl_encoder_wrapper, yajl_encoder_wrapper_mark, yajl_encoder_wrapper_free, wrapper); wrapper->encoder = yajl_gen_alloc(&cfg, NULL); wrapper->on_progress_callback = Qnil; if (opts != Qnil && rb_funcall(opts, intern_has_key, 1, sym_terminator) == Qtrue) { wrapper->terminator = rb_hash_aref(opts, sym_terminator); #ifdef HAVE_RUBY_ENCODING_H if (TYPE(wrapper->terminator) == T_STRING) { wrapper->terminator = rb_str_export_to_enc(wrapper->terminator, utf8Encoding); } #endif } else { wrapper->terminator = 0; } rb_obj_call_init(obj, 0, 0); return obj; }
/* call-seq: emitter.scalar(value, anchor, tag, plain, quoted, style) * * Emit a scalar with +value+, +anchor+, +tag+, and a +plain+ or +quoted+ * string type with +style+. * * See Psych::Handler#scalar */ static VALUE scalar( VALUE self, VALUE value, VALUE anchor, VALUE tag, VALUE plain, VALUE quoted, VALUE style ) { yaml_emitter_t * emitter; yaml_event_t event; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *encoding; #endif Data_Get_Struct(self, yaml_emitter_t, emitter); Check_Type(value, T_STRING); #ifdef HAVE_RUBY_ENCODING_H encoding = rb_utf8_encoding(); value = rb_str_export_to_enc(value, encoding); if(!NIL_P(anchor)) { Check_Type(anchor, T_STRING); anchor = rb_str_export_to_enc(anchor, encoding); } if(!NIL_P(tag)) { Check_Type(tag, T_STRING); tag = rb_str_export_to_enc(tag, encoding); } #endif yaml_scalar_event_initialize( &event, (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)), (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)), (yaml_char_t*)StringValuePtr(value), (int)RSTRING_LEN(value), plain ? 1 : 0, quoted ? 1 : 0, (yaml_scalar_style_t)NUM2INT(style) ); emit(emitter, &event); return self; }
int map_key_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) { VALUE key; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc; #endif if ( ((CTX *)ctx)->symbolizeKeys ) { #ifdef HAVE_RUBY_ENCODING_H ID id = rb_intern3((const char *)stringVal, stringLen, utf8Encoding); key = ID2SYM(id); #else VALUE str = rb_str_new((const char *)stringVal, stringLen); key = rb_str_intern(str); #endif } else { key = rb_str_new((const char *)stringVal, stringLen); #ifdef HAVE_RUBY_ENCODING_H default_internal_enc = rb_default_internal_encoding(); rb_enc_associate(key, utf8Encoding); if (default_internal_enc) { key = rb_str_export_to_enc(key, default_internal_enc); } #endif } set_key(ctx, key); return 1; }
static VALUE rb_mysql_client_server_info(VALUE self) { VALUE version, server_info; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc; rb_encoding *conn_enc; #endif GET_CLIENT(self); REQUIRE_OPEN_DB(wrapper); #ifdef HAVE_RUBY_ENCODING_H default_internal_enc = rb_default_internal_encoding(); conn_enc = rb_to_encoding(wrapper->encoding); #endif version = rb_hash_new(); rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_server_version(wrapper->client))); server_info = rb_str_new2(mysql_get_server_info(wrapper->client)); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(server_info, conn_enc); if (default_internal_enc) { server_info = rb_str_export_to_enc(server_info, default_internal_enc); } #endif rb_hash_aset(version, sym_version, server_info); return version; }
/* * @overload write(data) * * Writes +data+ to LOB. * * @param [String] data * @return [Integer] number of characters written if +self+ is a {CLOB} or a {NCLOB}. * number of bytes written if +self+ is a {BLOB} or a {BFILE}. */ static VALUE oci8_lob_write(VALUE self, VALUE data) { oci8_lob_t *lob = TO_LOB(self); oci8_svcctx_t *svcctx = check_svcctx(lob); volatile VALUE str; ub8 byte_amt; ub8 char_amt; lob_open(lob); if (TYPE(data) != T_STRING) { str = rb_obj_as_string(data); } else { str = data; } if (lob->lobtype == OCI_TEMP_CLOB) { str = rb_str_export_to_enc(str, oci8_encoding); } byte_amt = RSTRING_LEN(str); if (byte_amt == 0) { /* to avoid ORA-24801: illegal parameter value in OCI lob function */ return INT2FIX(0); } char_amt = 0; chker2(OCILobWrite2_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &byte_amt, &char_amt, lob->pos + 1, RSTRING_PTR(str), byte_amt, OCI_ONE_PIECE, NULL, NULL, 0, lob->csfrm), &svcctx->base); RB_GC_GUARD(str); if (lob->lobtype == OCI_TEMP_CLOB) { lob->pos += char_amt; return UINT2NUM(char_amt); } else { lob->pos += byte_amt; return UINT2NUM(byte_amt); } }
static VALUE sdl2r_ttf_render_utf8_blended(VALUE klass, VALUE vfont, VALUE vtext, VALUE vcolor) { struct SDL2RFont *fnt = SDL2R_GET_FONT_STRUCT(vfont); SDL_Color col; VALUE vsurface = sdl2r_surface_alloc(cSurface); struct SDL2RSurface *sur = SDL2R_GET_STRUCT(Surface, vsurface); Check_Type(vtext, T_STRING); if (rb_enc_get_index(vtext) != 0) { vtext = rb_str_export_to_enc(vtext, g_enc_utf8); } Check_Type(vcolor, T_ARRAY); col.r = NUM2INT(rb_ary_entry(vcolor, 0)); col.g = NUM2INT(rb_ary_entry(vcolor, 1)); col.b = NUM2INT(rb_ary_entry(vcolor, 2)); col.a = NUM2INT(rb_ary_entry(vcolor, 3)); sur->surface = TTF_RenderUTF8_Blended(fnt->font, RSTRING_PTR(vtext), col); if (!sur->surface) { rb_raise(eSDLError, TTF_GetError()); } return vsurface; }
static int yajl_found_hash_key(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { yajl_parser_wrapper * wrapper; GetParser((VALUE)ctx, wrapper); VALUE keyStr; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); #endif if (wrapper->symbolizeKeys) { char buf[stringLen+1]; memcpy(buf, stringVal, stringLen); buf[stringLen] = 0; yajl_set_static_value(ctx, ID2SYM(rb_intern(buf))); } else { keyStr = rb_str_new((const char *)stringVal, stringLen); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(keyStr, utf8Encoding); if (default_internal_enc) { keyStr = rb_str_export_to_enc(keyStr, default_internal_enc); } #endif yajl_set_static_value(ctx, keyStr); } yajl_check_and_fire_callback(ctx); return 1; }
static VALUE rb_mysql_client_info(VALUE self) { VALUE version, client_info; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc; rb_encoding *conn_enc; #endif GET_CLIENT(self); version = rb_hash_new(); #ifdef HAVE_RUBY_ENCODING_H default_internal_enc = rb_default_internal_encoding(); conn_enc = rb_to_encoding(wrapper->encoding); #endif rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version())); client_info = rb_str_new2(mysql_get_client_info()); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(client_info, conn_enc); if (default_internal_enc) { client_info = rb_str_export_to_enc(client_info, default_internal_enc); } #endif rb_hash_aset(version, sym_version, client_info); return version; }
static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_encoding *default_internal_enc, rb_encoding *conn_enc) { /* if binary flag is set, respect its wishes */ if (field.flags & BINARY_FLAG && field.charsetnr == 63) { rb_enc_associate(val, binaryEncoding); } else if (!field.charsetnr) { /* MySQL 4.x may not provide an encoding, binary will get the bytes through */ rb_enc_associate(val, binaryEncoding); } else { /* lookup the encoding configured on this field */ const char *enc_name; int enc_index; enc_name = mysql2_mysql_enc_to_rb[field.charsetnr-1]; if (enc_name != NULL) { /* use the field encoding we were able to match */ enc_index = rb_enc_find_index(enc_name); rb_enc_set_index(val, enc_index); } else { /* otherwise fall-back to the connection's encoding */ rb_enc_associate(val, conn_enc); } if (default_internal_enc) { val = rb_str_export_to_enc(val, default_internal_enc); } } return val; }
static VALUE transcode_string(VALUE src, int * parser_encoding) { int utf8 = rb_utf8_encindex(); int utf16le = rb_enc_find_index("UTF16_LE"); int utf16be = rb_enc_find_index("UTF16_BE"); int source_encoding = rb_enc_get_index(src); if (source_encoding == utf8) { *parser_encoding = YAML_UTF8_ENCODING; return src; } if (source_encoding == utf16le) { *parser_encoding = YAML_UTF16LE_ENCODING; return src; } if (source_encoding == utf16be) { *parser_encoding = YAML_UTF16BE_ENCODING; return src; } src = rb_str_export_to_enc(src, rb_utf8_encoding()); RB_GC_GUARD(src); *parser_encoding = YAML_UTF8_ENCODING; return src; }
static VALUE rb_mysql_result_fetch_field(VALUE self, unsigned int idx, int symbolize_keys) { VALUE rb_field; GET_RESULT(self); if (wrapper->fields == Qnil) { wrapper->numberOfFields = mysql_num_fields(wrapper->result); wrapper->fields = rb_ary_new2(wrapper->numberOfFields); } rb_field = rb_ary_entry(wrapper->fields, idx); if (rb_field == Qnil) { MYSQL_FIELD *field = NULL; rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); field = mysql_fetch_field_direct(wrapper->result, idx); if (symbolize_keys) { rb_field = rb_intern3(field->name, field->name_length, rb_utf8_encoding()); rb_field = ID2SYM(rb_field); } else { rb_field = rb_str_new(field->name, field->name_length); rb_enc_associate(rb_field, conn_enc); if (default_internal_enc) { rb_field = rb_str_export_to_enc(rb_field, default_internal_enc); } } rb_ary_store(wrapper->fields, idx, rb_field); } return rb_field; }
/* call-seq: SQLite3::Database.new(file, options = {}) * * Create a new Database object that opens the given file. If utf16 * is +true+, the filename is interpreted as a UTF-16 encoded string. * * By default, the new database will return result rows as arrays * (#results_as_hash) and has type translation disabled (#type_translation=). */ static VALUE initialize(int argc, VALUE *argv, VALUE self) { sqlite3RubyPtr ctx; VALUE file; VALUE opts; VALUE zvfs; int status; Data_Get_Struct(self, sqlite3Ruby, ctx); rb_scan_args(argc, argv, "12", &file, &opts, &zvfs); if(NIL_P(opts)) opts = rb_hash_new(); #ifdef HAVE_RUBY_ENCODING_H if(UTF16_LE_P(file)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #endif if(Qtrue == rb_hash_aref(opts, sym_utf16)) { status = sqlite3_open16(utf16_string_value_ptr(file), &ctx->db); } else { #ifdef HAVE_RUBY_ENCODING_H if(!UTF8_P(file)) { file = rb_str_export_to_enc(file, rb_utf8_encoding()); } #endif status = sqlite3_open_v2( StringValuePtr(file), &ctx->db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NIL_P(zvfs) ? NULL : StringValuePtr(zvfs) ); } #ifdef HAVE_RUBY_ENCODING_H } #endif CHECK(ctx->db, status) rb_iv_set(self, "@tracefunc", Qnil); rb_iv_set(self, "@authorizer", Qnil); rb_iv_set(self, "@encoding", Qnil); rb_iv_set(self, "@busy_handler", Qnil); rb_iv_set(self, "@results_as_hash", rb_hash_aref(opts, sym_results_as_hash)); rb_iv_set(self, "@type_translation", rb_hash_aref(opts, sym_type_translation)); if(rb_block_given_p()) { rb_yield(self); rb_funcall(self, rb_intern("close"), 0); } return self; }
/* call-seq: * client.escape(string) * * Escape +string+ so that it may be used in a SQL statement. */ static VALUE rb_mysql_client_real_escape(VALUE self, VALUE str) { unsigned char *newStr; VALUE rb_str; unsigned long newLen, oldLen; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc; rb_encoding *conn_enc; #endif GET_CLIENT(self); REQUIRE_CONNECTED(wrapper); Check_Type(str, T_STRING); #ifdef HAVE_RUBY_ENCODING_H default_internal_enc = rb_default_internal_encoding(); conn_enc = rb_to_encoding(wrapper->encoding); /* ensure the string is in the encoding the connection is expecting */ str = rb_str_export_to_enc(str, conn_enc); #endif oldLen = RSTRING_LEN(str); newStr = xmalloc(oldLen*2+1); newLen = mysql_real_escape_string(wrapper->client, (char *)newStr, RSTRING_PTR(str), oldLen); if (newLen == oldLen) { /* no need to return a new ruby string if nothing changed */ #ifdef HAVE_RUBY_ENCODING_H if (default_internal_enc) { str = rb_str_export_to_enc(str, default_internal_enc); } #endif xfree(newStr); return str; } else { rb_str = rb_str_new((const char*)newStr, newLen); #ifdef HAVE_RUBY_ENCODING_H rb_enc_associate(rb_str, conn_enc); if (default_internal_enc) { rb_str = rb_str_export_to_enc(rb_str, default_internal_enc); } #endif xfree(newStr); return rb_str; } }
VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql) { mysql_stmt_wrapper *stmt_wrapper; VALUE rb_stmt; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *conn_enc; #endif Check_Type(sql, T_STRING); rb_stmt = Data_Make_Struct(cMysql2Statement, mysql_stmt_wrapper, rb_mysql_stmt_mark, rb_mysql_stmt_free, stmt_wrapper); { stmt_wrapper->client = rb_client; stmt_wrapper->refcount = 1; stmt_wrapper->closed = 0; stmt_wrapper->stmt = NULL; } // instantiate stmt { GET_CLIENT(rb_client); stmt_wrapper->stmt = mysql_stmt_init(wrapper->client); #ifdef HAVE_RUBY_ENCODING_H conn_enc = rb_to_encoding(wrapper->encoding); #endif } if (stmt_wrapper->stmt == NULL) { rb_raise(cMysql2Error, "Unable to initialize prepared statement: out of memory"); } // set STMT_ATTR_UPDATE_MAX_LENGTH attr { my_bool truth = 1; if (mysql_stmt_attr_set(stmt_wrapper->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &truth)) { rb_raise(cMysql2Error, "Unable to initialize prepared statement: set STMT_ATTR_UPDATE_MAX_LENGTH"); } } // call mysql_stmt_prepare w/o gvl { struct nogvl_prepare_statement_args args; args.stmt = stmt_wrapper->stmt; args.sql = sql; #ifdef HAVE_RUBY_ENCODING_H // ensure the string is in the encoding the connection is expecting args.sql = rb_str_export_to_enc(args.sql, conn_enc); #endif args.sql_ptr = RSTRING_PTR(sql); args.sql_len = RSTRING_LEN(sql); if ((VALUE)rb_thread_call_without_gvl(nogvl_prepare_statement, &args, RUBY_UBF_IO, 0) == Qfalse) { rb_raise_mysql2_stmt_error(stmt_wrapper); } } return rb_stmt; }
static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) { struct nogvl_send_query_args args; fd_set fdset; int fd, retval; int async = 0; VALUE opts; VALUE rb_async; MYSQL * client; if (rb_scan_args(argc, argv, "11", &args.sql, &opts) == 2) { if ((rb_async = rb_hash_aref(opts, sym_async)) != Qnil) { async = rb_async == Qtrue ? 1 : 0; } } Check_Type(args.sql, T_STRING); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *conn_enc = rb_to_encoding(rb_iv_get(self, "@encoding")); // ensure the string is in the encoding the connection is expecting args.sql = rb_str_export_to_enc(args.sql, conn_enc); #endif Data_Get_Struct(self, MYSQL, client); REQUIRE_OPEN_DB(client); args.mysql = client; if (rb_thread_blocking_region(nogvl_send_query, &args, RUBY_UBF_IO, 0) == Qfalse) { return rb_raise_mysql2_error(client); } if (!async) { // the below code is largely from do_mysql // http://github.com/datamapper/do fd = client->net.fd; for(;;) { FD_ZERO(&fdset); FD_SET(fd, &fdset); retval = rb_thread_select(fd + 1, &fdset, NULL, NULL, NULL); if (retval < 0) { rb_sys_fail(0); } if (retval > 0) { break; } } return rb_mysql_client_async_result(self); } else { return Qnil; } }
static CFDataRef rb_create_cf_data(VALUE string){ StringValue(string); if(rb_enc_get_index(rb_obj_encoding(string))== rb_ascii8bit_encindex()){ return CFDataCreate(NULL, (UInt8*)RSTRING_PTR(string), RSTRING_LEN(string)); } else{ string = rb_str_export_to_enc(string, rb_utf8_encoding()); return CFDataCreate(NULL, (UInt8*)RSTRING_PTR(string), RSTRING_LEN(string)); } }
int string_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) { VALUE str = rb_str_new((const char *)stringVal, stringLen); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_enc_associate(str, utf8Encoding); if (default_internal_enc) { str = rb_str_export_to_enc(str, default_internal_enc); } #endif set_value(ctx,str); return 1; }
static VALUE rb_raise_mysql2_error(mysql_client_wrapper *wrapper) { VALUE rb_error_msg = rb_str_new2(mysql_error(wrapper->client)); VALUE rb_sql_state = rb_tainted_str_new2(mysql_sqlstate(wrapper->client)); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_encoding *conn_enc = rb_to_encoding(wrapper->encoding); rb_enc_associate(rb_error_msg, conn_enc); rb_enc_associate(rb_sql_state, conn_enc); if (default_internal_enc) { rb_error_msg = rb_str_export_to_enc(rb_error_msg, default_internal_enc); rb_sql_state = rb_str_export_to_enc(rb_sql_state, default_internal_enc); } #endif VALUE e = rb_exc_new3(cMysql2Error, rb_error_msg); rb_funcall(e, intern_error_number_eql, 1, UINT2NUM(mysql_errno(wrapper->client))); rb_funcall(e, intern_sql_state_eql, 1, rb_sql_state); rb_exc_raise(e); return Qnil; }
/* call-seq: emitter.start_mapping(anchor, tag, implicit, style) * * Start emitting a YAML map with +anchor+, +tag+, an +implicit+ start * and end, and +style+. * * See Psych::Handler#start_mapping */ static VALUE start_mapping( VALUE self, VALUE anchor, VALUE tag, VALUE implicit, VALUE style ) { yaml_emitter_t * emitter; yaml_event_t event; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *encoding; #endif Data_Get_Struct(self, yaml_emitter_t, emitter); #ifdef HAVE_RUBY_ENCODING_H encoding = rb_utf8_encoding(); if(!NIL_P(anchor)) { Check_Type(anchor, T_STRING); anchor = rb_str_export_to_enc(anchor, encoding); } if(!NIL_P(tag)) { Check_Type(tag, T_STRING); tag = rb_str_export_to_enc(tag, encoding); } #endif yaml_mapping_start_event_initialize( &event, (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)), (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)), implicit ? 1 : 0, (yaml_mapping_style_t)NUM2INT(style) ); emit(emitter, &event); return self; }
static int yajl_found_string(void * ctx, const unsigned char * stringVal, unsigned int stringLen) { VALUE str = rb_str_new((const char *)stringVal, stringLen); #ifdef HAVE_RUBY_ENCODING_H rb_encoding *default_internal_enc = rb_default_internal_encoding(); rb_enc_associate(str, utf8Encoding); if (default_internal_enc) { str = rb_str_export_to_enc(str, default_internal_enc); } #endif yajl_set_static_value(ctx, str); yajl_check_and_fire_callback(ctx); return 1; }
static int pg_text_enc_in_ruby(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx) { int arity = rb_obj_method_arity(conv->coder_obj, s_id_encode); if( arity == 1 ){ VALUE out_str = rb_funcall( conv->coder_obj, s_id_encode, 1, value ); StringValue( out_str ); *intermediate = rb_str_export_to_enc(out_str, rb_enc_from_index(enc_idx)); }else{ VALUE enc = rb_enc_from_encoding(rb_enc_from_index(enc_idx)); VALUE out_str = rb_funcall( conv->coder_obj, s_id_encode, 2, value, enc ); StringValue( out_str ); *intermediate = out_str; } return -1; }
// read from buffer and return a ruby string // params: start=0, length=self.length, encoding=Encoding.default_internal_encoding // return: ruby string VALUE ruv_buffer_read(int argc, VALUE* argv, VALUE rb_buffer) { VALUE rb_start, rb_length, rb_intern_enc, rb_str; size_t start, length; rb_encoding *intern_enc; ruv_buffer_t *buffer; char *str_p; Data_Get_Struct(rb_buffer, ruv_buffer_t, buffer); rb_scan_args(argc, argv, "03", &rb_start, &rb_length, &rb_intern_enc); if(!NIL_P(rb_start)) { Check_Type(rb_start, T_FIXNUM); start = NUM2SIZET(rb_start); } else { start = 0; } if(start >= buffer->length) { rb_raise(rb_eArgError, "start is out of bounds."); } if(!NIL_P(rb_length)) { Check_Type(rb_length, T_FIXNUM); length = NUM2SIZET(length); } else { length = buffer->length - start; } // make sure it's not out of bound length = MIN(length, buffer->length - start); if(!NIL_P(rb_intern_enc)) { intern_enc = rb_enc_get(rb_intern_enc); } else { intern_enc = rb_default_internal_encoding(); } str_p = buffer->data + start; rb_str = rb_tainted_str_new(str_p, length); if(intern_enc) { rb_str = rb_str_export_to_enc(rb_str, intern_enc); } return rb_str; }
/* call-seq: * client.query(sql, options = {}) * * Query the database with +sql+, with optional +options+. For the possible * options, see default_query_options on the Mysql2::Client class. */ static VALUE rb_query(VALUE self, VALUE sql, VALUE current) { #ifndef _WIN32 struct async_query_args async_args; #endif struct nogvl_send_query_args args; GET_CLIENT(self); REQUIRE_CONNECTED(wrapper); args.mysql = wrapper->client; (void)RB_GC_GUARD(current); Check_Type(current, T_HASH); rb_iv_set(self, "@current_query_options", current); Check_Type(sql, T_STRING); #ifdef HAVE_RUBY_ENCODING_H /* ensure the string is in the encoding the connection is expecting */ args.sql = rb_str_export_to_enc(sql, rb_to_encoding(wrapper->encoding)); #else args.sql = sql; #endif args.sql_ptr = RSTRING_PTR(args.sql); args.sql_len = RSTRING_LEN(args.sql); args.wrapper = wrapper; rb_mysql_client_set_active_thread(self); #ifndef _WIN32 rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0); if (rb_hash_aref(current, sym_async) == Qtrue) { return Qnil; } else { async_args.fd = wrapper->client->net.fd; async_args.self = self; rb_rescue2(do_query, (VALUE)&async_args, disconnect_and_raise, self, rb_eException, (VALUE)0); return rb_mysql_client_async_result(self); } #else do_send_query(&args); /* this will just block until the result is ready */ return rb_ensure(rb_mysql_client_async_result, self, finish_and_mark_inactive, self); #endif }