Beispiel #1
0
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;
}
Beispiel #2
0
static VALUE template_execute_rescue_enc(VALUE data)
{
	rb_gc_enable();
	VALUE* resc = (VALUE*)data;
	rb_enc_set_index(resc[0], (int)resc[1]);
	RERAISE;
}
Beispiel #3
0
/*
 * call-seq:
 *    res.check -> nil
 *
 * Raises appropriate exception if PG::Result is in a bad state.
 */
VALUE
pg_result_check( VALUE self )
{
	VALUE error, exception, klass;
	VALUE rb_pgconn = rb_iv_get( self, "@connection" );
	PGconn *conn = pg_get_pgconn(rb_pgconn);
	PGresult *result;
#ifdef M17N_SUPPORTED
	rb_encoding *enc = pg_conn_enc_get( conn );
#endif
	char * sqlstate;

	Data_Get_Struct(self, PGresult, result);

	if(result == NULL)
	{
		error = rb_str_new2( PQerrorMessage(conn) );
	}
	else
	{
		switch (PQresultStatus(result))
		{
		case PGRES_TUPLES_OK:
		case PGRES_COPY_OUT:
		case PGRES_COPY_IN:
#ifdef HAVE_CONST_PGRES_COPY_BOTH
		case PGRES_COPY_BOTH:
#endif
#ifdef HAVE_CONST_PGRES_SINGLE_TUPLE
		case PGRES_SINGLE_TUPLE:
#endif
		case PGRES_EMPTY_QUERY:
		case PGRES_COMMAND_OK:
			return Qnil;
		case PGRES_BAD_RESPONSE:
		case PGRES_FATAL_ERROR:
		case PGRES_NONFATAL_ERROR:
			error = rb_str_new2( PQresultErrorMessage(result) );
			break;
		default:
			error = rb_str_new2( "internal error : unknown result status." );
		}
	}

#ifdef M17N_SUPPORTED
	rb_enc_set_index( error, rb_enc_to_index(enc) );
#endif

	sqlstate = PQresultErrorField( result, PG_DIAG_SQLSTATE );
	klass = lookup_error_class( sqlstate );
	exception = rb_exc_new3( klass, error );
	rb_iv_set( exception, "@connection", rb_pgconn );
	rb_iv_set( exception, "@result", self );
	rb_exc_raise( exception );

	/* Not reached */
	return Qnil;
}
Beispiel #4
0
static VALUE encoding_spec_rb_enc_set_index(VALUE self, VALUE obj, VALUE index) {
  int i = NUM2INT(index);

  rb_encoding* enc = rb_enc_from_index(i);
  rb_enc_set_index(obj, i);

  return rb_ary_new3(2, rb_str_new2(rb_enc_name(enc)),
                     rb_str_new2(rb_enc_name(rb_enc_get(obj))));
}
Beispiel #5
0
static int template_execute_wrap(msgpack_unpack_t* mp,
		VALUE str, size_t dlen, size_t* from)
{
	VALUE args[4] = {
		(VALUE)mp,
		(VALUE)RSTRING_PTR(str),
		(VALUE)dlen,
		(VALUE)from,
	};

#ifdef HAVE_RUBY_ENCODING_H
	int enc_orig = rb_enc_get_index(str);
	rb_enc_set_index(str, s_ascii_8bit);
#endif

	// FIXME execute実行中はmp->topが更新されないのでGC markが機能しない
	rb_gc_disable();

	mp->user.source = str;

#ifdef HAVE_RUBY_ENCODING_H
	VALUE resc[2] = {str, enc_orig};
	int ret = (int)rb_rescue(template_execute_do, (VALUE)args,
			template_execute_rescue_enc, (VALUE)resc);
#else
	int ret = (int)rb_rescue(template_execute_do, (VALUE)args,
			template_execute_rescue, Qnil);
#endif

	rb_gc_enable();

#ifdef HAVE_RUBY_ENCODING_H
	rb_enc_set_index(str, enc_orig);
#endif

	return ret;
}
Beispiel #6
0
VALUE
rb_enc_associate_index(VALUE obj, int idx)
{
/*    enc_check_capable(obj);*/
    if (rb_enc_get_index(obj) == idx)
	return obj;
    if (SPECIAL_CONST_P(obj)) {
	rb_raise(rb_eArgError, "cannot set encoding");
    }
    if (!ENC_CODERANGE_ASCIIONLY(obj) ||
	!rb_enc_asciicompat(rb_enc_from_index(idx))) {
	ENC_CODERANGE_CLEAR(obj);
    }
    rb_enc_set_index(obj, idx);
    return obj;
}