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
The string sent to the server was empty.
Successful completion of a command returning no data.
Successful completion of a command returning data (such as a SELECT or SHOW).
Copy Out (from server) data transfer started.
Copy In (to server) data transfer started.
The server’s response was not understood.
A nonfatal error (a notice or warning) occurred.
A fatal error occurred.
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.
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.
The primary human-readable error message (typically one line). Always present.
an optional secondary error message carrying more detail about the problem. Might run to multiple lines.
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.
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.
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.
The text of a failed internally-generated command. This could be, for example, a SQL query issued by a PL/pgSQL function.
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.
The le name of the source-code location where the error was reported.
The line number of the source-code location where the error was reported.
The name of the source-code function reporting the error.
Invalid OID constant
Returns tuple n as a hash.
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; }
Clears the PGresult object as the result of the query.
static VALUE pgresult_clear(VALUE self) { PQclear(get_pgresult(self)); DATA_PTR(self) = NULL; return Qnil; }
Returns the status string of the last query command.
static VALUE pgresult_cmd_status(VALUE self) { VALUE ret = rb_tainted_str_new2(PQcmdStatus(get_pgresult(self))); ASSOCIATE_INDEX(ret, self); return ret; }
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.
static VALUE pgresult_cmd_tuples(VALUE self) { long n; n = strtol(PQcmdTuples(get_pgresult(self)),NULL, 10); return INT2NUM(n); }
Returns an Array of the values from the nth column of each tuple in the result.
static VALUE pgresult_column_values(VALUE self, VALUE index) { int col = NUM2INT( index ); return make_column_result_array( self, col ); }
Invokes block for each tuple in the result set.
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; }
Returns the format (0 for text, 1 for binary) of column column_number.
Raises ArgumentError if column_number is out of range.
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)); }
Returns an Array of the values from the given field of each tuple in the result.
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 ); }
Returns an array of Strings representing the names of the fields in the result.
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; }
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.
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); }
Returns the name of the column corresponding to index.
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; }
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.
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); }
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
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)); }
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.
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); }
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.
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); }
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.
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)); }
Returns true if the specified value is nil; false otherwise.
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; }
Returns the (String) length of the field in bytes.
Equivalent to res.value(tup_num,field_num).length.
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)); }
Returns the value in tuple number tup_num, field field_num, or nil if the field is NULL.
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; }
Returns the number of columns in the query result.
static VALUE pgresult_nfields(VALUE self) { return INT2NUM(PQnfields(get_pgresult(self))); }
Returns the number of parameters of a prepared statement. Only useful for the result returned by conn.describePrepared
static VALUE pgresult_nparams(VALUE self) { PGresult *result; result = get_pgresult(self); return INT2FIX(PQnparams(result)); }
Returns the number of tuples in the query result.
static VALUE pgresult_ntuples(VALUE self) { return INT2FIX(PQntuples(get_pgresult(self))); }
Returns the oid of the inserted row if applicable, otherwise nil.
static VALUE pgresult_oid_value(VALUE self) { Oid n = PQoidValue(get_pgresult(self)); if (n == InvalidOid) return Qnil; else return INT2FIX(n); }
Returns the Oid of the data type of parameter param_number. Only useful for the result returned by conn.describePrepared
static VALUE pgresult_paramtype(VALUE self, VALUE param_number) { PGresult *result; result = get_pgresult(self); return INT2FIX(PQparamtype(result,NUM2INT(param_number))); }
Returns the string representation of status status.
static VALUE pgresult_res_status(VALUE self, VALUE status) { VALUE ret = rb_tainted_str_new2(PQresStatus(NUM2INT(status))); ASSOCIATE_INDEX(ret, self); return ret; }
Returns the individual field of an error.
fieldcode is one of:
static VALUE pgresult_result_error_field(VALUE self, VALUE field) { PGresult *result = get_pgresult(self); int fieldcode = NUM2INT(field); VALUE ret = rb_tainted_str_new2(PQresultErrorField(result,fieldcode)); ASSOCIATE_INDEX(ret, self); return ret; }
Returns the error message of the command as a string.
static VALUE pgresult_result_error_message(VALUE self) { VALUE ret = rb_tainted_str_new2(PQresultErrorMessage(get_pgresult(self))); ASSOCIATE_INDEX(ret, self); return ret; }
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.