Class: PGresult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/pg.c,
ext/pg.c

Overview

******************************************************************

The class to represent the query result tuples (rows).
An instance of this class is created as the result of every query.
You may need to invoke the #clear method of the instance when finished with
the result for better memory performance.

Example:
   require 'pg'
   conn = PGconn.open(:dbname => 'test')
   res  = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')
   res.getvalue(0,0) # '1'
   res[0]['b']       # '2'
   res[0]['c']       # nil

Constant Summary collapse

PGRES_EMPTY_QUERY =

The string sent to the server was empty.

#result_status constant
PGRES_COMMAND_OK =

Successful completion of a command returning no data.

#result_status constant
PGRES_TUPLES_OK =

Successful completion of a command returning data

(such as a SELECT or SHOW).
#result_status constant
PGRES_COPY_OUT =

Copy Out (from server) data transfer started.

#result_status constant
PGRES_COPY_IN =

Copy In (to server) data transfer started.

#result_status constant
PGRES_BAD_RESPONSE =

The server’s response was not understood.

#result_status constant
PGRES_NONFATAL_ERROR =

A nonfatal error (a notice or warning) occurred.

#result_status constant
PGRES_FATAL_ERROR =

A fatal error occurred.

#result_status constant
PG_DIAG_SEVERITY =

The severity; the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a localized translation of one of these. Always present.

#result_error_field argument constant
PG_DIAG_SQLSTATE =

The SQLSTATE code for the error. The SQLSTATE code identies the type of error that has occurred; it can be used by front-end applications to perform specic operations (such as er- ror handling) in response to a particular database error. For a list of the possible SQLSTATE codes, see Appendix A. This eld is not localizable, and is always present.

#result_error_field argument constant
PG_DIAG_MESSAGE_PRIMARY =

The primary human-readable error message (typically one line). Always present.

#result_error_field argument constant
PG_DIAG_MESSAGE_DETAIL =

Detail: an optional secondary error message carrying more detail about the problem. Might run to multiple lines.

#result_error_field argument constant
PG_DIAG_MESSAGE_HINT =

Hint: an optional suggestion what to do about the problem. This is intended to differ from detail in that it offers advice (potentially inappropriate) rather than hard facts. Might run to multiple lines.

#result_error_field argument constant
PG_DIAG_STATEMENT_POSITION =

A string containing a decimal integer indicating an error cursor position as an index into the original statement string. The rst character has index 1, and positions are measured in characters not bytes.

#result_error_field argument constant
PG_DIAG_INTERNAL_POSITION =

This is dened the same as the PG_DIAG_STATEMENT_POSITION eld, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client. The PG_DIAG_INTERNAL_QUERY eld will always appear when this eld appears.

#result_error_field argument constant
PG_DIAG_INTERNAL_QUERY =

The text of a failed internally-generated command. This could be, for example, a SQL query issued by a PL/pgSQL function.

#result_error_field argument constant
PG_DIAG_CONTEXT =

An indication of the context in which the error occurred. Presently this includes a call stack traceback of active procedural language functions and internally-generated queries. The trace is one entry per line, most recent rst.

#result_error_field argument constant
PG_DIAG_SOURCE_FILE =

The le name of the source-code location where the error was reported.

#result_error_field argument constant
PG_DIAG_SOURCE_LINE =

The line number of the source-code location where the error was reported.

#result_error_field argument constant
PG_DIAG_SOURCE_FUNCTION =

The name of the source-code function reporting the error.

#result_error_field argument constant
InvalidOid =

Invalid OID constant

INT2FIX(InvalidOid)

Instance Method Summary collapse

Instance Method Details

#[](n) ⇒ Hash

Returns tuple n as a hash.

Returns:

  • (Hash)


3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
# File 'ext/pg.c', line 3649

static VALUE
pgresult_aref(VALUE self, VALUE index)
{
	PGresult *result = get_pgresult(self);
	int tuple_num = NUM2INT(index);
	int field_num;
	VALUE fname,val;
	VALUE tuple;

	if ( tuple_num < 0 || tuple_num >= PQntuples(result) )
		rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num );

	tuple = rb_hash_new();
	for ( field_num = 0; field_num < PQnfields(result); field_num++ ) {
		fname = rb_tainted_str_new2( PQfname(result,field_num) );
		ASSOCIATE_INDEX(fname, self);
		if ( PQgetisnull(result, tuple_num, field_num) ) {
			rb_hash_aset( tuple, fname, Qnil );
		}
		else {
			val = rb_tainted_str_new( PQgetvalue(result, tuple_num, field_num ),
			                          PQgetlength(result, tuple_num, field_num) );

			/* associate client encoding for text format only */
			if ( 0 == PQfformat(result, field_num) ) {
				ASSOCIATE_INDEX( val, self );
			} else {
#ifdef M17N_SUPPORTED
				rb_enc_associate( val, rb_ascii8bit_encoding() );
#endif
			}
			rb_hash_aset( tuple, fname, val );
		}
	}
	return tuple;
}

#clearnil

Clears the PGresult object as the result of the query.

Returns:

  • (nil)


3255
3256
3257
3258
3259
3260
3261
# File 'ext/pg.c', line 3255

static VALUE
pgresult_clear(VALUE self)
{
	PQclear(get_pgresult(self));
	DATA_PTR(self) = NULL;
	return Qnil;
}

#cmd_statusString

Returns the status string of the last query command.

Returns:

  • (String)


3594
3595
3596
3597
3598
3599
3600
# File 'ext/pg.c', line 3594

static VALUE
pgresult_cmd_status(VALUE self)
{
	VALUE ret = rb_tainted_str_new2(PQcmdStatus(get_pgresult(self)));
	ASSOCIATE_INDEX(ret, self);
	return ret;
}

#cmd_tuplesFixnum Also known as: cmdtuples

Returns the number of tuples (rows) affected by the SQL command.

If the SQL command that generated the PGresult was not one of:

  • INSERT

  • UPDATE

  • DELETE

  • MOVE

  • FETCH

or if no tuples were affected, 0 is returned.

Returns:

  • (Fixnum)


3616
3617
3618
3619
3620
3621
3622
# File 'ext/pg.c', line 3616

static VALUE
pgresult_cmd_tuples(VALUE self)
{
	long n;
	n = strtol(PQcmdTuples(get_pgresult(self)),NULL, 10);
	return INT2NUM(n);
}

#column_values(n) ⇒ Array

Returns an Array of the values from the nth column of each tuple in the result.

Returns:

  • (Array)


3744
3745
3746
3747
3748
3749
# File 'ext/pg.c', line 3744

static VALUE
pgresult_column_values(VALUE self, VALUE index)
{
	int col = NUM2INT( index );
	return make_column_result_array( self, col );
}

#each {|tuple| ... } ⇒ Object

Invokes block for each tuple in the result set.

Yields:

  • (tuple)


3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
# File 'ext/pg.c', line 3814

static VALUE
pgresult_each(VALUE self)
{
	PGresult *result = get_pgresult(self);
	int tuple_num;

	for(tuple_num = 0; tuple_num < PQntuples(result); tuple_num++) {
		rb_yield(pgresult_aref(self, INT2NUM(tuple_num)));
	}
	return self;
}

#error_field(fieldcode) ⇒ String Also known as: result_error_field

Returns the individual field of an error.

fieldcode is one of:

  • PG_DIAG_SEVERITY

  • PG_DIAG_SQLSTATE

  • PG_DIAG_MESSAGE_PRIMARY

  • PG_DIAG_MESSAGE_DETAIL

  • PG_DIAG_MESSAGE_HINT

  • PG_DIAG_STATEMENT_POSITION

  • PG_DIAG_INTERNAL_POSITION

  • PG_DIAG_INTERNAL_QUERY

  • PG_DIAG_CONTEXT

  • PG_DIAG_SOURCE_FILE

  • PG_DIAG_SOURCE_LINE

  • PG_DIAG_SOURCE_FUNCTION

An example:

begin
    conn.exec( "SELECT * FROM nonexistant_table" )
rescue PGError => err
    p [
        result.error_field( PGresult::PG_DIAG_SEVERITY ),
        result.error_field( PGresult::PG_DIAG_SQLSTATE ),
        result.error_field( PGresult::PG_DIAG_MESSAGE_PRIMARY ),
        result.error_field( PGresult::PG_DIAG_MESSAGE_DETAIL ),
        result.error_field( PGresult::PG_DIAG_MESSAGE_HINT ),
        result.error_field( PGresult::PG_DIAG_STATEMENT_POSITION ),
        result.error_field( PGresult::PG_DIAG_INTERNAL_POSITION ),
        result.error_field( PGresult::PG_DIAG_INTERNAL_QUERY ),
        result.error_field( PGresult::PG_DIAG_CONTEXT ),
        result.error_field( PGresult::PG_DIAG_SOURCE_FILE ),
        result.error_field( PGresult::PG_DIAG_SOURCE_LINE ),
        result.error_field( PGresult::PG_DIAG_SOURCE_FUNCTION ),
    ]
end

Outputs:

["ERROR", "42P01", "relation \"nonexistant_table\" does not exist", nil, nil,
 "15", nil, nil, nil, "path/to/parse_relation.c", "857", "parserOpenTable"]

Returns:

  • (String)


3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
# File 'ext/pg.c', line 3233

static VALUE
pgresult_error_field(VALUE self, VALUE field)
{
	PGresult *result = get_pgresult( self );
	int fieldcode = NUM2INT( field );
	char * fieldstr = PQresultErrorField( result, fieldcode );
	VALUE ret = Qnil;

	if ( fieldstr ) {
		ret = rb_tainted_str_new2( fieldstr );
		ASSOCIATE_INDEX( ret, self );
	}

	return ret;
}

#error_messageString Also known as: result_error_message

Returns the error message of the command as a string.

Returns:

  • (String)


3179
3180
3181
3182
3183
3184
3185
# File 'ext/pg.c', line 3179

static VALUE
pgresult_error_message(VALUE self)
{
	VALUE ret = rb_tainted_str_new2(PQresultErrorMessage(get_pgresult(self)));
	ASSOCIATE_INDEX(ret, self);
	return ret;
}

#fformat(column_number) ⇒ Fixnum

Returns the format (0 for text, 1 for binary) of column column_number.

Raises ArgumentError if column_number is out of range.

Returns:

  • (Fixnum)


3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
# File 'ext/pg.c', line 3390

static VALUE
pgresult_fformat(VALUE self, VALUE column_number)
{
	PGresult *result = get_pgresult(self);
	int fnumber = NUM2INT(column_number);
	if (fnumber < 0 || fnumber >= PQnfields(result)) {
		rb_raise(rb_eArgError, "Column number is out of range: %d", 
			fnumber);
	}
	return INT2FIX(PQfformat(result, fnumber));
}

#field_values(field) ⇒ Array

Returns an Array of the values from the given field of each tuple in the result.

Returns:

  • (Array)


3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
# File 'ext/pg.c', line 3759

static VALUE
pgresult_field_values( VALUE self, VALUE field )
{
	PGresult *result = get_pgresult( self );
	const char *fieldname = RSTRING_PTR( field );
	int fnum = PQfnumber( result, fieldname );

	if ( fnum < 0 )
		rb_raise( rb_eIndexError, "no such field '%s' in result", fieldname );

	return make_column_result_array( self, fnum );
}

#fieldsArray

Returns an array of Strings representing the names of the fields in the result.

Returns:

  • (Array)


3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
# File 'ext/pg.c', line 3832

static VALUE
pgresult_fields(VALUE self)
{
	PGresult *result;
	VALUE ary;
	int n, i;

	result = get_pgresult(self);
	n = PQnfields(result);
	ary = rb_ary_new2(n);
	for (i=0;i<n;i++) {
		VALUE val = rb_tainted_str_new2(PQfname(result, i));
		ASSOCIATE_INDEX(val, self);
		rb_ary_push(ary, val);
	}
	return ary;
}

#fmod(column_number) ⇒ Object

Returns the type modifier associated with column column_number. See the #ftype method for an example of how to use this.

Raises an ArgumentError if column_number is out of range.



3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
# File 'ext/pg.c', line 3440

static VALUE
pgresult_fmod(VALUE self, VALUE column_number)
{
	PGresult *result = get_pgresult(self);
	int fnumber = NUM2INT(column_number);
	int modifier;
	if (fnumber < 0 || fnumber >= PQnfields(result)) {
		rb_raise(rb_eArgError, "Column number is out of range: %d", 
			fnumber);
	}
	modifier = PQfmod(result,fnumber);

	return INT2NUM(modifier);
}

#fname(index) ⇒ String

Returns the name of the column corresponding to index.

Returns:

  • (String)


3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
# File 'ext/pg.c', line 3293

static VALUE
pgresult_fname(VALUE self, VALUE index)
{
	VALUE fname;
	PGresult *result;
	int i = NUM2INT(index);

	result = get_pgresult(self);
	if (i < 0 || i >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", i);
	}
	fname = rb_tainted_str_new2(PQfname(result, i));
	ASSOCIATE_INDEX(fname, self);
	return fname;
}

#fnumber(name) ⇒ Fixnum

Returns the index of the field specified by the string name.

Raises an ArgumentError if the specified name isn’t one of the field names; raises a TypeError if name is not a String.

Returns:

  • (Fixnum)


3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
# File 'ext/pg.c', line 3318

static VALUE
pgresult_fnumber(VALUE self, VALUE name)
{
	int n;

	Check_Type(name, T_STRING);

	n = PQfnumber(get_pgresult(self), StringValuePtr(name));
	if (n == -1) {
		rb_raise(rb_eArgError,"Unknown field: %s", StringValuePtr(name));
	}
	return INT2FIX(n);
}

#fsize(index) ⇒ Object

Returns the size of the field type in bytes. Returns -1 if the field is variable sized.

res = conn.exec("SELECT myInt, myVarChar50 FROM foo")
res.size(0) => 4
res.size(1) => -1


3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
# File 'ext/pg.c', line 3465

static VALUE
pgresult_fsize(VALUE self, VALUE index)
{
	PGresult *result;
	int i = NUM2INT(index);

	result = get_pgresult(self);
	if (i < 0 || i >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", i);
	}
	return INT2NUM(PQfsize(result, i));
}

#ftable(column_number) ⇒ Fixnum

Returns the Oid of the table from which the column column_number was fetched.

Raises ArgumentError if column_number is out of range or if the Oid is undefined for that column.

Returns:

  • (Fixnum)


3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
# File 'ext/pg.c', line 3342

static VALUE
pgresult_ftable(VALUE self, VALUE column_number)
{
	Oid n ;
	int col_number = NUM2INT(column_number);
	PGresult *pgresult = get_pgresult(self);

	if( col_number < 0 || col_number >= PQnfields(pgresult)) 
		rb_raise(rb_eArgError,"Invalid column index: %d", col_number);

	n = PQftable(pgresult, col_number);
	return INT2FIX(n);
}

#ftablecol(column_number) ⇒ Fixnum

Returns the column number (within its table) of the table from which the column column_number is made up.

Raises ArgumentError if column_number is out of range or if the column number from its table is undefined for that column.

Returns:

  • (Fixnum)


3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
# File 'ext/pg.c', line 3366

static VALUE
pgresult_ftablecol(VALUE self, VALUE column_number)
{
	int col_number = NUM2INT(column_number);
	PGresult *pgresult = get_pgresult(self);

	int n;

	if( col_number < 0 || col_number >= PQnfields(pgresult)) 
		rb_raise(rb_eArgError,"Invalid column index: %d", col_number);

	n = PQftablecol(pgresult, col_number);
	return INT2FIX(n);
}

#ftype(column_number) ⇒ Object

Returns the data type associated with column_number.

The integer returned is the internal OID number (in PostgreSQL) of the type. To get a human-readable value for the type, use the returned OID and the field’s #fmod value with the format_type() SQL function:

# Get the type of the second column of the result 'res'
typename = conn.
  exec( "SELECT format_type($1,$2)", [res.ftype(1), res.fmod(1)] ).
  getvalue( 0, 0 )

Raises an ArgumentError if column_number is out of range.



3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
# File 'ext/pg.c', line 3420

static VALUE
pgresult_ftype(VALUE self, VALUE index)
{
	PGresult* result = get_pgresult(self);
	int i = NUM2INT(index);
	if (i < 0 || i >= PQnfields(result)) {
		rb_raise(rb_eArgError, "invalid field number %d", i);
	}
	return INT2NUM(PQftype(result, i));
}

#getisnull(tuple_position, field_position) ⇒ Boolean

Returns true if the specified value is nil; false otherwise.

Returns:

  • (Boolean)


3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
# File 'ext/pg.c', line 3514

static VALUE
pgresult_getisnull(VALUE self, VALUE tup_num, VALUE field_num)
{
	PGresult *result;
	int i = NUM2INT(tup_num);
	int j = NUM2INT(field_num);

	result = get_pgresult(self);
	if (i < 0 || i >= PQntuples(result)) {
		rb_raise(rb_eArgError,"invalid tuple number %d", i);
	}
	if (j < 0 || j >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", j);
	}
	return PQgetisnull(result, i, j) ? Qtrue : Qfalse;
}

#getlength(tup_num, field_num) ⇒ Fixnum

Returns the (String) length of the field in bytes.

Equivalent to res.value(tup_num,field_num).length.

Returns:

  • (Fixnum)


3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
# File 'ext/pg.c', line 3539

static VALUE
pgresult_getlength(VALUE self, VALUE tup_num, VALUE field_num)
{
	PGresult *result;
	int i = NUM2INT(tup_num);
	int j = NUM2INT(field_num);

	result = get_pgresult(self);
	if (i < 0 || i >= PQntuples(result)) {
		rb_raise(rb_eArgError,"invalid tuple number %d", i);
	}
	if (j < 0 || j >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", j);
	}
	return INT2FIX(PQgetlength(result, i, j));
}

#getvalue(tup_num, field_num) ⇒ Object

Returns the value in tuple number tup_num, field field_num, or nil if the field is NULL.



3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
# File 'ext/pg.c', line 3485

static VALUE
pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num)
{
	VALUE ret;
	PGresult *result;
	int i = NUM2INT(tup_num);
	int j = NUM2INT(field_num);

	result = get_pgresult(self);
	if(i < 0 || i >= PQntuples(result)) {
		rb_raise(rb_eArgError,"invalid tuple number %d", i);
	}
	if(j < 0 || j >= PQnfields(result)) {
		rb_raise(rb_eArgError,"invalid field number %d", j);
	}
	if(PQgetisnull(result, i, j))
		return Qnil;
	ret = rb_tainted_str_new(PQgetvalue(result, i, j), 
				PQgetlength(result, i, j));
	ASSOCIATE_INDEX(ret, self);
	return ret;
}

#nfieldsFixnum Also known as: num_fields

Returns the number of columns in the query result.

Returns:

  • (Fixnum)


3281
3282
3283
3284
3285
# File 'ext/pg.c', line 3281

static VALUE
pgresult_nfields(VALUE self)
{
	return INT2NUM(PQnfields(get_pgresult(self)));
}

#nparamsFixnum

Returns the number of parameters of a prepared statement. Only useful for the result returned by conn.describePrepared

Returns:

  • (Fixnum)


3563
3564
3565
3566
3567
3568
3569
3570
# File 'ext/pg.c', line 3563

static VALUE
pgresult_nparams(VALUE self)
{
	PGresult *result;

	result = get_pgresult(self);
	return INT2FIX(PQnparams(result));
}

#ntuplesFixnum Also known as: num_tuples

Returns the number of tuples in the query result.

Returns:

  • (Fixnum)


3269
3270
3271
3272
3273
# File 'ext/pg.c', line 3269

static VALUE
pgresult_ntuples(VALUE self)
{
	return INT2FIX(PQntuples(get_pgresult(self)));
}

#oid_valueFixnum

Returns the oid of the inserted row if applicable, otherwise nil.

Returns:

  • (Fixnum)


3631
3632
3633
3634
3635
3636
3637
3638
3639
# File 'ext/pg.c', line 3631

static VALUE
pgresult_oid_value(VALUE self)
{
	Oid n = PQoidValue(get_pgresult(self));
	if (n == InvalidOid)
		return Qnil;
	else
		return INT2FIX(n);
}

#paramtype(param_number) ⇒ Oid

Returns the Oid of the data type of parameter param_number. Only useful for the result returned by conn.describePrepared

Returns:

  • (Oid)


3579
3580
3581
3582
3583
3584
3585
3586
# File 'ext/pg.c', line 3579

static VALUE
pgresult_paramtype(VALUE self, VALUE param_number)
{
	PGresult *result;

	result = get_pgresult(self);
	return INT2FIX(PQparamtype(result,NUM2INT(param_number)));
}

#res_status(status) ⇒ String

Returns the string representation of status status.

Returns:

  • (String)


3165
3166
3167
3168
3169
3170
3171
# File 'ext/pg.c', line 3165

static VALUE
pgresult_res_status(VALUE self, VALUE status)
{
	VALUE ret = rb_tainted_str_new2(PQresStatus(NUM2INT(status)));
	ASSOCIATE_INDEX(ret, self);
	return ret;
}

#result_statusFixnum

Returns the status of the query. The status value is one of:

  • PGRES_EMPTY_QUERY

  • PGRES_COMMAND_OK

  • PGRES_TUPLES_OK

  • PGRES_COPY_OUT

  • PGRES_COPY_IN

  • PGRES_BAD_RESPONSE

  • PGRES_NONFATAL_ERROR

  • PGRES_FATAL_ERROR

Returns:

  • (Fixnum)


3152
3153
3154
3155
3156
# File 'ext/pg.c', line 3152

static VALUE
pgresult_result_status(VALUE self)
{
	return INT2FIX(PQresultStatus(get_pgresult(self)));
}

#valuesArray

Returns all tuples as an array of arrays.

Returns:

  • (Array)


3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
# File 'ext/pg.c', line 3693

static VALUE
pgresult_values(VALUE self, VALUE index)
{
	PGresult* result = (PGresult*) get_pgresult(self);
	int row;
	int field;
	int num_rows = PQntuples(result);
	int num_fields = PQnfields(result);
	VALUE ary = rb_ary_new2(num_rows);

	for ( row = 0; row < num_rows; row++ ) {
		/* create new row */
		VALUE new_row = rb_ary_new2(num_fields);

		/* add to return array */
		rb_ary_store( ary, row, new_row );

		/* populate it */
		for ( field = 0; field < num_fields; field++ ) {
			if ( PQgetisnull(result, row, field) ) {
				rb_ary_store( new_row, field, Qnil );
			}
			else {
				VALUE val = rb_tainted_str_new( PQgetvalue(result, row, field), 
				                                PQgetlength(result, row, field) );

				/* associate client encoding for text format only */
				if ( 0 == PQfformat(result, field) ) {
					ASSOCIATE_INDEX( val, self );
				} else {
#ifdef M17N_SUPPORTED
					rb_enc_associate( val, rb_ascii8bit_encoding() );
#endif
				}

				rb_ary_store( new_row, field, val );
			}
		}
	}
	return ary;
}