Class PgQuery
The PgQuery Class.
This class builds and executes PostgreSQL Queries and traverses the set of
results returned from the query.
Example usage
$sql = "SELECT * FROM mytable WHERE mytype = ?";
$qry = new PgQuery( $sql, $myunsanitisedtype );
if ( $qry->Exec("typeselect", __line__, __file__ )
&& $qry->rows() > 0 )
{
while( $row = $qry->Fetch() ) {
do_something_with($row);
}
}
Methods summary
public
The
|
|
public
|
#
SetConnection( resource $new_connection )
Use a different database connection for this query
Use a different database connection for this query
Parameters
- $new_connection
resource $new_connection The database connection to use.
|
public
|
#
_log_error( string $locn, string $tag, string $string, integer $line = 0, string $file = "" )
Log error, optionally with file and line location of the caller.
Log error, optionally with file and line location of the caller.
This function should not really be used outside of PgQuery. For a more useful
generic logging interface consider calling dbg_error_log(...);
Parameters
- $locn
string $locn A string identifying the calling location.
- $tag
string $tag A tag string, e.g. identifying the type of event.
- $string
string $string The information to be logged.
- $line
integer $line The line number where the logged event occurred.
- $file
string $file The file name where the logged event occurred.
|
public
|
#
rows( )
Provide a rows() method for forward compatibility with AwlQuery.
Provide a rows() method for forward compatibility with AwlQuery.
|
public
string
|
#
quote( mixed $str = null )
Quote the given string so it can be safely used within string delimiters in a
query.
Quote the given string so it can be safely used within string delimiters in a
query.
Parameters
- $str
mixed $str Data to be converted to a string suitable for including as a value in SQL.
Returns
string NULL, TRUE, FALSE, a plain number, or the original string quoted and with ' and
\ characters escaped
See
qpg() which is where this is really done.
|
public
array
|
#
Plain( string $field )
Convert a string which has already been quoted and escaped for PostgreSQL
into a magic array so that it will be inserted unmodified into the SQL string.
Use with care!
Convert a string which has already been quoted and escaped for PostgreSQL
into a magic array so that it will be inserted unmodified into the SQL string.
Use with care!
Parameters
- $field
string $field The value which has alread been quoted and escaped.
Returns
array An array with the value associated with a key of 'plain'
|
public
resource
|
#
Exec( string $location = '', integer $line = 0, string $file = '' )
Execute the query, logging any debugging.
Execute the query, logging any debugging.
Example So that you can nicely enable/disable the queries for a
particular class, you could use some of PHPs magic constants in your call.
$qry->Exec(__CLASS__, __LINE__, __FILE__);
Parameters
- $location
string $location The name of the location for enabling debugging or just to help our
children find the source of a problem.
- $line
integer $line The line number where Exec was called
- $file
string $file The file where Exec was called
Returns
resource The actual result of the query (FWIW)
|
public
mixed
|
#
Fetch( boolean $as_array = false )
Fetch the next row from the query results
Fetch the next row from the query results
Parameters
- $as_array
boolean $as_array True if thing to be returned is array
Returns
mixed query row
|
public
|
#
UnFetch( )
Set row counter back one
In the case that you may like to fetch the same row twice, for example if
your SQL returns some columns that are the same for each row, and you want to
display them cleanly before displaying the other data repeatedly for each
row.
Example
$master_row = $qry->Fetch();
$qry->UnFetch();
do_something_first($master_row);
while( $row = $qry->Fetch() ) {
do_something_repeatedly($row);
}
|
public
mixed
|
#
FetchBackwards( boolean $as_array = false )
Fetch backwards from the result resource
Fetch backwards from the result resource
Parameters
- $as_array
boolean $as_array True if thing to be returned is array (default: False
Returns
mixed query row
|
public
string
|
#
BuildOptionList( string $current = '', string $location = 'options', array $parameters = false )
Build an option list from the query.
Build an option list from the query.
Parameters
- $current
string $current Default selection of drop down box (optional)
- $location
string $location for debugging purposes
- $parameters
array $parameters an array further parameters, including 'maxwidth' => 20 to set a
maximum width
Returns
string Select box HTML
|
Properties summary
public
resource
|
$connection
|
|
#
holds the connection to the database should be internal
holds the connection to the database should be internal
|
public
string
|
$querystring
|
|
#
stores a query string should be read-only
stores a query string should be read-only
|
public
resource
|
$result
|
|
#
stores a resource result should be internal
stores a resource result should be internal
|
public
integer
|
$rownum
|
-1 |
#
number of current row should be internal, or at least read-only
number of current row should be internal, or at least read-only
|
public
string
|
$location
|
|
#
Where we called this query from so we can find it in our code! Debugging may
also be selectively enabled for a $location.
Where we called this query from so we can find it in our code! Debugging may
also be selectively enabled for a $location.
|
public
mixed
|
$object
|
|
#
The row most recently fetched by a call to Fetch() or FetchBackwards which
will either be an array or an object (depending on the Fetch call).
The row most recently fetched by a call to Fetch() or FetchBackwards which
will either be an array or an object (depending on the Fetch call).
|
public
integer
|
$rows
|
|
#
number of rows from pg_numrows - for fetching result should be read-only
number of rows from pg_numrows - for fetching result should be read-only
|
public
string
|
$errorstring
|
|
#
The PostgreSQL error message, if the query fails. Should be read-only,
although any successful Exec should clear it
The PostgreSQL error message, if the query fails. Should be read-only,
although any successful Exec should clear it
|
public
string
|
$execution_time
|
|
#
Stores the query execution time - used to deal with long queries. should be
read-only
Stores the query execution time - used to deal with long queries. should be
read-only
|
public
float
|
$query_time_warning
|
0.3 |
#
How long the query should take before a warning is issued.
How long the query should take before a warning is issued.
This is writable, but a method to set it might be a better interface. The
default is 0.3 seconds.
|