VALUE
pg_tmbc_result_value( t_typemap *p_typemap, VALUE result, int tuple, int field )
{
	t_pg_coder *p_coder = NULL;
	t_pg_result *p_result = pgresult_get_this(result);
	t_tmbc *this = (t_tmbc *) p_typemap;
	t_typemap *default_tm;

	if (PQgetisnull(p_result->pgresult, tuple, field)) {
		return Qnil;
	}

	p_coder = this->convs[field].cconv;

	if( p_coder ){
		char * val = PQgetvalue( p_result->pgresult, tuple, field );
		int len = PQgetlength( p_result->pgresult, tuple, field );

		if( p_coder->dec_func ){
			return p_coder->dec_func(p_coder, val, len, tuple, field, ENCODING_GET(result));
		} else {
			t_pg_coder_dec_func dec_func;
			dec_func = pg_coder_dec_func( p_coder, PQfformat(p_result->pgresult, field) );
			return dec_func(p_coder, val, len, tuple, field, ENCODING_GET(result));
		}
	}

	default_tm = DATA_PTR( this->typemap.default_typemap );
	return default_tm->funcs.typecast_result_value( default_tm, result, tuple, field );
}
Exemple #2
0
VALUE
pg_new_result_autoclear(PGresult *result, VALUE rb_pgconn)
{
	VALUE self = pg_new_result(result, rb_pgconn);
	t_pg_result *this = pgresult_get_this(self);
	this->autoclear = 1;
	return self;
}
Exemple #3
0
/*
 * call-seq:
 *    res.clear() -> nil
 *
 * Clears the PG::Result object as the result of the query.
 *
 * If PG::Result#autoclear? is true then the result is marked as cleared
 * and the underlying C struct will be cleared automatically by libpq.
 *
 */
VALUE
pg_result_clear(VALUE self)
{
	t_pg_result *this = pgresult_get_this(self);
	if( !this->autoclear )
		PQclear(pgresult_get(self));
	this->pgresult = NULL;
	return Qnil;
}
Exemple #4
0
/*
 * call-seq:
 *    res.check -> nil
 *
 * Raises appropriate exception if PG::Result is in a bad state.
 */
VALUE
pg_result_check( VALUE self )
{
	t_pg_result *this = pgresult_get_this(self);
	VALUE error, exception, klass;
	char * sqlstate;

	if(this->pgresult == NULL)
	{
		PGconn *conn = pg_get_pgconn(this->connection);
		error = rb_str_new2( PQerrorMessage(conn) );
	}
	else
	{
		switch (PQresultStatus(this->pgresult))
		{
		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 self;
		case PGRES_BAD_RESPONSE:
		case PGRES_FATAL_ERROR:
		case PGRES_NONFATAL_ERROR:
			error = rb_str_new2( PQresultErrorMessage(this->pgresult) );
			break;
		default:
			error = rb_str_new2( "internal error : unknown result status." );
		}
	}

	PG_ENCODING_SET_NOCHECK( error, ENCODING_GET(self) );

	sqlstate = PQresultErrorField( this->pgresult, PG_DIAG_SQLSTATE );
	klass = lookup_error_class( sqlstate );
	exception = rb_exc_new3( klass, error );
	rb_iv_set( exception, "@connection", this->connection );
	rb_iv_set( exception, "@result", this->pgresult ? self : Qnil );
	rb_exc_raise( exception );

	/* Not reached */
	return self;
}
static VALUE
pg_tmas_result_value( t_typemap *p_typemap, VALUE result, int tuple, int field )
{
	VALUE ret;
	char * val;
	int len;
	t_pg_result *p_result = pgresult_get_this(result);

	if (PQgetisnull(p_result->pgresult, tuple, field)) {
		return Qnil;
	}

	val = PQgetvalue( p_result->pgresult, tuple, field );
	len = PQgetlength( p_result->pgresult, tuple, field );

	if ( 0 == PQfformat(p_result->pgresult, field) ) {
		ret = pg_text_dec_string(NULL, val, len, tuple, field, ENCODING_GET(result));
	} else {
		ret = pg_bin_dec_bytea(NULL, val, len, tuple, field, ENCODING_GET(result));
	}

	return ret;
}
Exemple #6
0
/*
 * call-seq:
 *    res.autoclear?      -> boolean
 *
 * Returns +true+ if the underlying C struct will be cleared automatically by libpq.
 * Elsewise the result is cleared by PG::Result#clear or by the GC when it's no longer in use.
 *
 */
VALUE
pgresult_autoclear_p( VALUE self )
{
	t_pg_result *this = pgresult_get_this(self);
	return this->autoclear ? Qtrue : Qfalse;
}
Exemple #7
0
/*
 * call-seq:
 *    res.cleared?      -> boolean
 *
 * Returns +true+ if the backend result memory has been free'd.
 */
VALUE
pgresult_cleared_p( VALUE self )
{
	t_pg_result *this = pgresult_get_this(self);
	return this->pgresult ? Qfalse : Qtrue;
}