Back to index

awl  0.53
Public Member Functions | Public Attributes
PgQuery Class Reference

List of all members.

Public Member Functions

 __construct ()
 

@-


 SetConnection ($new_connection)
 Use a different database connection for this query.
 _log_error ($locn, $tag, $string, $line=0, $file="")
 Log error, optionally with file and line location of the caller.
 rows ()
 Provide a rows() method for forward compatibility with AwlQuery.
 quote ($str=null)
 Quote the given string so it can be safely used within string delimiters in a query.
 Plain ($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.
 Exec ($location= '', $line=0, $file= '')
 Execute the query, logging any debugging.
 Fetch ($as_array=false)
 Fetch the next row from the query results.
 UnFetch ()
 Set row counter back one.
 FetchBackwards ($as_array=false)
 Fetch backwards from the result resource.
 BuildOptionList ($current= '', $location= 'options', $parameters=false)
 Build an option list from the query.

Public Attributes

 $connection
 $querystring
 $result
 $rownum = -1
 $location
 $object
 $rows
 $errorstring
 $execution_time
 $query_time_warning = 0.3

Detailed Description

Definition at line 277 of file PgQuery.php.


Constructor & Destructor Documentation

@-

Constructor

Parameters:
stringThe query string with replacable '?' characters.
mixedThe values to replace into the SQL string.
Returns:
The PgQuery object

Definition at line 367 of file PgQuery.php.

                         {
    global $dbconn;
    $this->result = 0;
    $this->rows = 0;
    $this->execution_time = 0;
    $this->rownum = -1;
    $this->connection = $dbconn;

    $argc = func_num_args();

    if ( 1 < $argc ) {
      $this->querystring = awl_replace_sql_args( func_get_args() );
    }
    else {
      // If we are only called with a single argument, we do
      // nothing special with any question marks.
      $this->querystring = func_get_arg(0);
    }

    return $this;
  }

Here is the call graph for this function:


Member Function Documentation

PgQuery::_log_error ( locn,
tag,
string,
line = 0,
file = "" 
)

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:
string$locnA string identifying the calling location.
string$tagA tag string, e.g. identifying the type of event.
string$stringThe information to be logged.
int$lineThe line number where the logged event occurred.
string$fileThe file name where the logged event occurred.

Definition at line 412 of file PgQuery.php.

                                                                    {
    // replace more than one space with one space
    $string = preg_replace('/\s+/', ' ', $string);

    if ( ($tag == 'QF' || $tag == 'SQ') && ( $line != 0 && $file != "" ) ) {
      dbg_error_log( "LOG-$locn", " Query: %s: Error in '%s' on line %d", $tag, $file, $line );
    }

    while( strlen( $string ) > 0 )  {
      dbg_error_log( "LOG-$locn", " Query: %s: %s", $tag, substr( $string, 0, 240) );
      $string = substr( "$string", 240 );
    }
  }

Here is the caller graph for this function:

PgQuery::BuildOptionList ( current = '',
location = 'options',
parameters = false 
)

Build an option list from the query.

Parameters:
string$currentDefault selection of drop down box (optional)
string$locationfor debugging purposes
array$parametersan array further parameters, including 'maxwidth' => 20 to set a maximum width
Returns:
string Select box HTML

Definition at line 615 of file PgQuery.php.

                                                                                        {
    global $debuggroups;
    $result = '';
    $translate = false;

    if ( isset($maxwidth) ) unset($maxwidth);
    if ( is_array($parameters) ) {
      if ( isset($parameters['maxwidth']) ) $maxwidth = max(4,intval($parameters['maxwidth']));
      if ( isset($parameters['translate']) ) $translate = true;
    }

    // The query may already have been executed
    if ( $this->rows > 0 || $this->Exec($location) ) {
      $this->rownum = -1;
      while( $row = $this->Fetch(true) )
      {
        if (is_array($current)) {
          $selected = ( ( in_array($row[0],$current,true) || in_array($row[1],$current,true)) ? ' selected="selected"' : '' );
        }
        else {
          $selected = ( ( "$row[0]" == "$current" || "$row[1]" == "$current" ) ? ' selected="selected"' : '' );
        }
        $display_value = $row[1];
        if ( isset($translate) ) $display_value = translate( $display_value );
        if ( isset($maxwidth) ) $display_value = substr( $display_value, 0, $maxwidth);
        $nextrow = "<option value=\"".htmlspecialchars($row[0])."\"$selected>".htmlspecialchars($display_value)."</option>";
        $result .= $nextrow;
      }
    }
    return $result;
   }

Here is the call graph for this function:

PgQuery::Exec ( location = '',
line = 0,
file = '' 
)

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:
string$locationThe name of the location for enabling debugging or just to help our children find the source of a problem.
int$lineThe line number where Exec was called
string$fileThe file where Exec was called
Returns:
resource The actual result of the query (FWIW)

Definition at line 478 of file PgQuery.php.

                                                         {
    global $debuggroups, $c, $dbconn;
    if ( !isset($this->connection) ) {
      if ( !isset($dbconn) ) {
        connect_configured_database();
      }
      $this->connection = $dbconn;
    }
    $this->location = trim($location);
    if ( $this->location == "" ) $this->location = substr($_SERVER['PHP_SELF'],1);

    if ( isset($debuggroups['querystring']) || isset($c->dbg['querystring']) || isset($c->dbg['ALL']) ) {
      $this->_log_error( $this->location, 'DBGQ', $this->querystring, $line, $file );
    }

    $t1 = microtime(); // get start time
    $this->result = @pg_exec( $this->connection, $this->querystring ); // execute the query
    $this->rows = ($this->result ? pg_numrows($this->result) : -1); // number of rows returned
    $t2 = microtime(); // get end time
    $i_took = duration( $t1, $t2 );   // calculate difference
    $c->total_query_time += $i_took;
    $this->execution_time = sprintf( "%2.06lf", $i_took);

    if ( !$this->result ) {
     // query simply failed
      $this->errorstring = @pg_errormessage(); // returns database error message
      $this->_log_error( $this->location, 'QF', $this->querystring, $line, $file );
      $this->_log_error( $this->location, 'QF', $this->errorstring, $line, $file );
    }
    elseif ( $this->execution_time > $this->query_time_warning ) {
     // if execution time is too long
      $this->_log_error( $this->location, 'SQ', "Took: $this->execution_time for $this->querystring", $line, $file ); // SQ == Slow Query :-)
    }
    elseif ( isset($debuggroups[$this->location]) || isset($c->dbg[strtolower($this->location)]) || isset($c->dbg['ALL']) ) {
     // query successful, but we're debugging and want to know how long it took anyway
      $this->_log_error( $this->location, 'DBGQ', "Took: $this->execution_time for $this->querystring to find $this->rows rows.", $line, $file );
    }

    return $this->result;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

PgQuery::Fetch ( as_array = false)

Fetch the next row from the query results.

Parameters:
boolean$as_arrayTrue if thing to be returned is array
Returns:
mixed query row

Definition at line 525 of file PgQuery.php.

                                    {
    global $c, $debuggroups;

    if ( ( isset($debuggroups["$this->location"]) && $debuggroups["$this->location"] > 2 )
       || (isset($c) && is_object($c) && ( isset($c->dbg[strtolower($this->location)]) && isset($c->dbg[strtolower($this->location)]) )
                                        || isset($c->dbg['ALL']) ) ) {
        $this->_log_error( $this->location, "Fetch", "$this->result Rows: $this->rows, Rownum: $this->rownum");
    }
    if ( ! $this->result ) return false; // no results
    if ( ($this->rownum + 1) >= $this->rows ) return false; // reached the end of results

    $this->rownum++;
    if ( isset($debuggroups["$this->location"]) && $debuggroups["$this->location"] > 1 ) {
      $this->_log_error( $this->location, "Fetch", "Fetching row $this->rownum" );
    }
    if ( $as_array )
    {
      $this->object = pg_fetch_array($this->result, $this->rownum);
    }
    else
    {
      $this->object = pg_fetch_object($this->result, $this->rownum);
    }

    return $this->object;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

PgQuery::FetchBackwards ( as_array = false)

Fetch backwards from the result resource.

Parameters:
boolean$as_arrayTrue if thing to be returned is array (default: False
Returns:
mixed query row

Definition at line 581 of file PgQuery.php.

                                             {
    global $debuggroups;

    if ( isset($debuggroups["$this->location"]) && $debuggroups["$this->location"] > 2 ) {
      $this->_log_error( $this->location, "FetchBackwards", "$this->result Rows: $this->rows, Rownum: $this->rownum");
    }
    if ( ! $this->result ) return false;
    if ( ($this->rownum - 1) == -1 ) return false;
    if ( $this->rownum == -1 ) $this->rownum = $this->rows;

    $this->rownum--;

    if ( isset($debuggroups["$this->location"]) && $debuggroups["$this->location"] > 1 ) {
      $this->_log_error( $this->location, "Fetch", "Fetching row $this->rownum" );
    }
    if ( $as_array )
    {
      $this->object = pg_fetch_array($this->result, $this->rownum);
    }
    else
    {
      $this->object = pg_fetch_object($this->result, $this->rownum);
    }

    return $this->object;
  }

Here is the call graph for this function:

PgQuery::Plain ( 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!

Parameters:
string$fieldThe value which has alread been quoted and escaped.
Returns:
array An array with the value associated with a key of 'plain'

Definition at line 455 of file PgQuery.php.

                           {
    // Abuse the array type to extend our ability to avoid \\ and ' replacement
    $rv = array( 'plain' => $field );
    return $rv;
  }
PgQuery::quote ( str = null)

Quote the given string so it can be safely used within string delimiters in a query.

See also:
qpg() which is where this is really done.
Parameters:
mixed$strData 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

Definition at line 443 of file PgQuery.php.

                              {
    return qpg($str);
  }

Here is the call graph for this function:

Provide a rows() method for forward compatibility with AwlQuery.

Definition at line 429 of file PgQuery.php.

                  {
    return $this->rows;
  }

Here is the caller graph for this function:

PgQuery::SetConnection ( new_connection)

Use a different database connection for this query.

Parameters:
resource$new_connectionThe database connection to use.

Definition at line 394 of file PgQuery.php.

                                            {
    $this->connection = $new_connection;
  }

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); }

Definition at line 570 of file PgQuery.php.

                     {
    global $debuggroups;
    $this->rownum--;
    if ( $this->rownum < -1 ) $this->rownum = -1;
  }

Member Data Documentation

Definition at line 287 of file PgQuery.php.

Definition at line 341 of file PgQuery.php.

Definition at line 348 of file PgQuery.php.

Definition at line 315 of file PgQuery.php.

Definition at line 322 of file PgQuery.php.

Definition at line 357 of file PgQuery.php.

Definition at line 294 of file PgQuery.php.

Definition at line 301 of file PgQuery.php.

Definition at line 308 of file PgQuery.php.

Definition at line 334 of file PgQuery.php.


The documentation for this class was generated from the following file: