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 = PGresult CONSTANTS   result status
PGRES_COMMAND_OK = INT2FIX(PGRES_COMMAND_OK)
PGRES_TUPLES_OK = INT2FIX(PGRES_TUPLES_OK)
PGRES_COPY_OUT = INT2FIX(PGRES_COPY_OUT)
PGRES_COPY_IN = INT2FIX(PGRES_COPY_IN)
PGRES_BAD_RESPONSE = INT2FIX(PGRES_BAD_RESPONSE)
PGRES_NONFATAL_ERROR = INT2FIX(PGRES_NONFATAL_ERROR)
PGRES_FATAL_ERROR = INT2FIX(PGRES_FATAL_ERROR)
PG_DIAG_SEVERITY = PGresult CONSTANTS   result error field codes
PG_DIAG_SQLSTATE = INT2FIX(PG_DIAG_SQLSTATE)
PG_DIAG_MESSAGE_PRIMARY = INT2FIX(PG_DIAG_MESSAGE_PRIMARY)
PG_DIAG_MESSAGE_DETAIL = INT2FIX(PG_DIAG_MESSAGE_DETAIL)
PG_DIAG_MESSAGE_HINT = INT2FIX(PG_DIAG_MESSAGE_HINT)
PG_DIAG_STATEMENT_POSITION = INT2FIX(PG_DIAG_STATEMENT_POSITION)
PG_DIAG_INTERNAL_POSITION = INT2FIX(PG_DIAG_INTERNAL_POSITION)
PG_DIAG_INTERNAL_QUERY = INT2FIX(PG_DIAG_INTERNAL_QUERY)
PG_DIAG_CONTEXT = INT2FIX(PG_DIAG_CONTEXT)
PG_DIAG_SOURCE_FILE = INT2FIX(PG_DIAG_SOURCE_FILE)
PG_DIAG_SOURCE_LINE = INT2FIX(PG_DIAG_SOURCE_LINE)
PG_DIAG_SOURCE_FUNCTION = INT2FIX(PG_DIAG_SOURCE_FUNCTION)

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

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 Strings representing the names of the fields in the result.

Returns the type modifier associated with column column_number.

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

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

[Validate]