Class: PGresult

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

[] clear cmd_status cmd_tuples cmdtuples each fformat fields fmod fname fnumber fsize ftable ftablecol ftype getisnull getlength getvalue nfields nparams ntuples num_fields num_tuples oid_value paramtype res_status result_error_field result_error_message result_status

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

res[ n ] → Hash

Returns tuple n as a hash.

res.clear() → nil

Clears the PGresult object as the result of the query.

res.cmd_status() → String

Returns the status string of the last query command.

res.cmd_tuples() → Fixnum

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

res.each{ |tuple| ... }

Invokes block for each tuple in the result set.

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

res.fields() → Array

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

res.fmod( column_number )

Returns the type modifier associated with column column_number.

Raises ArgumentError if column_number is out of range.

res.fname( index ) → String

Returns the name of the column corresponding to index.

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

res.fsize( index )

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

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

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

res.ftype( column_number )

Returns the data type associated with column_number.

The integer returned is the internal `OID` number (in PostgreSQL) of the type.

res.getisnull(tuple_position, field_position) → boolean

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

res.getlength( tup_num, field_num ) → Fixnum

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

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

res.getvalue( tup_num, field_num )

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

res.nfields() → Fixnum

Returns the number of columns in the query result.

res.nparams() → Fixnum

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

res.ntuples() → Fixnum

Returns the number of tuples in the query result.

num_fields()

Alias for nfields

num_tuples()

Alias for ntuples

res.oid_value() → Fixnum

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

res.paramtype( param_number ) → Oid

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

res.res_status( status ) → String

Returns the string representation of status `status`.

res.result_error_field(fieldcode) → String

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`

res.result_error_message() → String

Returns the error message of the command as a string.

res.result_status() → Fixnum

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]