Class PGresult
In: ext/pg.c
Parent: Object

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

Methods

Included Modules

Enumerable

Constants

PGRES_EMPTY_QUERY = #result_status constant   The string sent to the server was empty.
PGRES_COMMAND_OK = #result_status constant   Successful completion of a command returning no data.
PGRES_TUPLES_OK = #result_status constant   Successful completion of a command returning data (such as a SELECT or SHOW).
PGRES_COPY_OUT = #result_status constant   Copy Out (from server) data transfer started.
PGRES_COPY_IN = #result_status constant   Copy In (to server) data transfer started.
PGRES_BAD_RESPONSE = #result_status constant   The server’s response was not understood.
PGRES_NONFATAL_ERROR = #result_status constant   A nonfatal error (a notice or warning) occurred.
PGRES_FATAL_ERROR = #result_status constant   A fatal error occurred.
PG_DIAG_SEVERITY = #result_error_field argument constant   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.
PG_DIAG_SQLSTATE = #result_error_field argument constant   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.
PG_DIAG_MESSAGE_PRIMARY = #result_error_field argument constant   The primary human-readable error message (typically one line). Always present.
PG_DIAG_MESSAGE_DETAIL = #result_error_field argument constant: Detail   an optional secondary error message carrying more detail about the problem. Might run to multiple lines.
PG_DIAG_MESSAGE_HINT = #result_error_field argument constant: 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.
PG_DIAG_STATEMENT_POSITION = #result_error_field argument constant   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.
PG_DIAG_INTERNAL_POSITION = #result_error_field argument constant   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.
PG_DIAG_INTERNAL_QUERY = #result_error_field argument constant   The text of a failed internally-generated command. This could be, for example, a SQL query issued by a PL/pgSQL function.
PG_DIAG_CONTEXT = #result_error_field argument constant   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.
PG_DIAG_SOURCE_FILE = #result_error_field argument constant   The le name of the source-code location where the error was reported.
PG_DIAG_SOURCE_LINE = #result_error_field argument constant   The line number of the source-code location where the error was reported.
PG_DIAG_SOURCE_FUNCTION = #result_error_field argument constant   The name of the source-code function reporting the error.
InvalidOid = INT2FIX(InvalidOid)   Invalid OID constant

Public Instance methods

Returns tuple n as a hash.

Clears the PGresult object as the result of the query.

Returns the status string of the last query command.

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.

cmdtuples()

Alias for cmd_tuples

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

Invokes block for each tuple in the result set.

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

Raises ArgumentError if column_number is out of range.

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

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

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.

Returns the name of the column corresponding to index.

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 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

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 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 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.

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

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

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

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

Returns the number of columns in the query result.

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

Returns the number of tuples in the query result.

num_fields()

Alias for nfields

num_tuples()

Alias for ntuples

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

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

Returns the string representation of status status.

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

Returns the error message of the command as a string.

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 all tuples as an array of arrays.

[Validate]