Source for file BitDbPear.php
Documentation is available at BitDbPear.php
* ADOdb Library interface Class
* Copyright (c) 2004 bitweaver.org
* Copyright (c) 2003 tikwiki.org
* Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
* @author spider <spider@steelsun.com>
* ensure your AdoDB install is a subdirectory off your include path
require_once( KERNEL_PKG_PATH. 'BitDbBase.php' );
* This class is used for database access and provides a number of functions to help
* with database portability.
* Currently used as a base class, this class should be optional to ensure bitweaver
* continues to function correctly, without a valid database connection.
function __construct( $pPearDsn= NULL, $pPearOptions = NULL )
if( empty( $pPearDsn ) ) {
global $gBitDbType, $gBitDbUser, $gBitDbPassword, $gBitDbHost, $gBitDbName;
'phptype' => $gBitDbType,
'username' => $gBitDbUser,
'password' => $gBitDbPassword ,
'database' => $gBitDbHost. '/'. $gBitDbName,
if( empty( $pPearOptions ) ) {
'portability' => DB_PORTABILITY_ALL,
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'bit_pear_login_error' );
$this->mDb = DB::connect($pPearDsn, $pPearOptions);
if( PEAR::isError( $this->mDb ) ) {
$this->mErrors['db_connect'] = $this->mDb->getDebugInfo();
$this->mDb->setFetchMode( DB_FETCHMODE_ASSOC );
// Default to autocommit unless StartTrans is called
$this->mDb->autoCommit( TRUE );
$this->mType = $pPearDsn['phptype'];
$this->mName = $pPearDsn['database'];
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'bit_pear_error_handler');
* Quotes a string to be sent to the database which is
* passed to function on to AdoDB->qstr().
* @todo not sure what its supposed to do
* @param pStr string to be quotes
* @return quoted string using AdoDB->qstr()
return $this->mDb->quote($pStr);
/** Queries the database, returning an error if one occurs, rather
* than exiting while printing the error. -rlpowell
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pError the error string to modify and return
* @param pValues an array of values used in a parameterised query
* @param pNumRows the number of rows (LIMIT) to return in this query
* @param pOffset the row number to begin returning rows from. Used in
* @return an AdoDB RecordSet object
* conjunction with $pNumRows
* @todo currently not used anywhere.
function queryError( $pQuery, &$pError, $pValues = NULL, $pNumRows = - 1, $pOffset = - 1 )
if ($pNumRows == - 1 && $pOffset == - 1)
$result = $this->mDb->Execute($pQuery, $pValues);
$result = $this->mDb->SelectLimit($pQuery, $pNumRows, $pOffset, $pValues);
$pError = $this->mDb->ErrorMsg();
//count the number of queries made
//$this->debugger_log($pQuery, $pValues);
/** Queries the database reporting an error if detected
* than exiting while printing the error. -rlpowell
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pNumRows the number of rows (LIMIT) to return in this query
* @param pOffset the row number to begin returning rows from. Used in
* conjunction with $pNumRows
* @return an AdoDB RecordSet object
function query($query, $values = null, $numrows = BIT_QUERY_DEFAULT, $offset = BIT_QUERY_DEFAULT, $pCacheTime= BIT_QUERY_DEFAULT )
if( empty( $this->mDb ) ) {
$result = $this->mDb->query( $query, $values );
$result = $this->mDb->limitQuery( $query, $offset, $numrows, $values );
if( !empty( $this->mDebug ) ) {
print ( "<tt>\n". vsprintf( str_replace( '?', "'%s'", $this->mDb->last_query ), $this->mDb->last_parameters ). "\n</tt><br/>" );
function Execute($pQuery, $pNumRows = false, $zf_cache = false, $pCacheTime= BIT_QUERY_DEFAULT) {
/** Executes the SQL and returns all elements of the first column as a 1-dimensional array. The recordset is discarded for you automatically. If an error occurs, false is returned.
* See AdoDB GetCol() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pForceArray if set to true, when an array is created for each value
* @param pFirst2Cols if set to true, only returns the first two columns
* @return the associative array, or false if an error occurs
* @todo not currently used anywhere
function getCol( $pQuery, $pValues= array(), $pTrim= FALSE )
if( empty( $this->mDb ) ) {
$result = $this->mDb->getCol( $pQuery, 0, $pValues );
//count the number of queries made
/** Returns an associative array for the given query.
* See AdoDB GetAssoc() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pForceArray if set to true, when an array is created for each value
* @param pFirst2Cols if set to true, only returns the first two columns
* @return the associative array, or false if an error occurs
function getArray( $pQuery, $pValues= FALSE, $pForceArray= FALSE, $pFirst2Cols= FALSE, $pCacheTime= BIT_QUERY_DEFAULT )
if( empty( $this->mDb ) ) {
$result = $this->mDb->GetArray( $pQuery, $pValues, $pForceArray, $pFirst2Cols );
function getAll( $pQuery, $pValues= FALSE, $pCacheTime= BIT_QUERY_DEFAULT ) {
if( empty( $this->mDb ) ) {
$result = $this->mDb->GetAll( $pQuery, $pValues );
/** Returns an associative array for the given query.
* See AdoDB GetAssoc() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pForceArray if set to true, when an array is created for each value
* @param pFirst2Cols if set to true, only returns the first two columns
* @return the associative array, or false if an error occurs
function getAssoc( $pQuery, $pValues= array(), $pForceArray= FALSE, $pFirst2Cols= FALSE, $pCacheTime= BIT_QUERY_DEFAULT )
if( empty( $this->mDb ) ) {
$result = $this->mDb->getAssoc( $pQuery, $pForceArray, $pValues, $pFirst2Cols );
/** Executes the SQL and returns the first row as an array. The recordset and remaining rows are discarded for you automatically. If an error occurs, false is returned.
* See AdoDB GetRow() function for more detail.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @return returns the first row as an array, or false if an error occurs
function getRow( $pQuery, $pValues= FALSE, $pCacheTime= BIT_QUERY_DEFAULT )
if( empty( $this->mDb ) ) {
$result = $this->mDb->GetRow( $pQuery, $pValues );
/** Returns a single column value from the database.
* @param pQuery the SQL query. Use backticks (`) to quote all table
* and attribute names for AdoDB to quote appropriately.
* @param pValues an array of values used in a parameterised query
* @param pReportErrors report errors to STDOUT
* @param pOffset the row number to begin returning rows from.
* @return the associative array, or false if an error occurs
function getOne($pQuery, $pValues= NULL, $pNumRows= NULL, $pOffset= NULL, $pCacheTime = BIT_QUERY_DEFAULT )
if( $result = $this->query($pQuery, $pValues, 1, 0, $pCacheTime ) ) {
if( $res = $result->fetchRow() ) {
* A database portable Sequence management function.
* @param pSequenceName Name of the sequence to be used
* It will be created if it does not already exist
* @return 0 if not supported, otherwise a sequence id
function GenID( $pSequenceName, $pUseDbPrefix = true ) {
if( empty( $this->mDb ) ) {
// Pear appends _seq just to be a pain
$seqName = str_replace( '_seq', '', $prefix. $pSequenceName );
return $this->mDb->nextId( $seqName );
* A database portable Sequence management function.
* @param pSequenceName Name of the sequence to be used
* It will be created if it does not already exist
* @param pStartID Allows setting the initial value of the sequence
* @return 0 if not supported, otherwise a sequence id
* @todo To be combined with GenID
if (empty($this->mDb->_genSeqSQL)) return FALSE;
return $this->mDb->CreateSequence($seqname, $startID);
* A database portable IFNULL function.
* @param pField argument to compare to NULL
* @param pNullRepl the NULL replacement value
* @return a string that represents the function that checks whether
* $pField is NULL for the given database, and if NULL, change the
* value returned to $pNullRepl.
function ifNull($pField, $pNullRepl)
return $this->mDb->ifNull($pField, $pNullRepl);
/** Format the timestamp in the format the database accepts.
* @param pDate a Unix integer timestamp or an ISO format Y-m-d H:i:s
* @return the timestamp as a quoted string.
* @todo could be used to later convert all int timestamps into db
* timestamps. Currently not used anywhere.
// not sure what this did - maybe someone can comment why its here
//return preg_replace("/'/","", $this->mDb->DBTimeStamp($pDate));
return $this->mDb->DBTimeStamp($pDate);
* Format date column in sql string given an input format that understands Y M D
function SQLDate($pDateFormat, $pBaseDate= false) {
return $this->mDb->SQLDate($pDateFormat, $pBaseDate) ;
* Calculate the offset of a date for a particular database and generate
* appropriate SQL. Useful for calculating future/past dates and storing
* @param pDays Number of days to offset by
* If dayFraction=1.5 means 1.5 days from now, 1.0/24 for 1 hour.
* @param pColumn Value to be offset
* If NULL an offset from the current time is supplied
* @return New number of days
* @todo Not currently used - this is database specific and uses TIMESTAMP
* rather than unix seconds
return $this->mDb->OffsetDate( $pDays, $pColumn );
* Improved method of initiating a transaction. Used together with CompleteTrans().
* a. StartTrans/CompleteTrans is nestable, unlike BeginTrans/CommitTrans/RollbackTrans.
* Only the outermost block is treated as a transaction.<br>
* b. CompleteTrans auto-detects SQL errors, and will rollback on errors, commit otherwise.<br>
* c. All BeginTrans/CommitTrans/RollbackTrans inside a StartTrans/CompleteTrans block
* are disabled, making it backward compatible.
return( $this->mDb->autoCommit( FALSE ) == DB_OK);
* Used together with StartTrans() to end a transaction. Monitors connection
* for sql errors, and will commit or rollback as appropriate.
* autoComplete if true, monitor sql errors and commit and rollback as appropriate,
* and if set to false force rollback even if no SQL error detected.
* @returns true on commit, false on rollback.
$ret = $this->mDb->commit( FALSE ) == DB_OK;
$this->mDb->autoCommit( TRUE );
* If database does not support transactions, rollbacks always fail, so return false
* otherwise returns true if the Rollback was successful
$ret = $this->mDb->rollback( FALSE ) == DB_OK;
$this->mDb->autoCommit( TRUE );
* Create a list of tables available in the current database
* @param ttype can either be 'VIEW' or 'TABLE' or false.
* If false, both views and tables are returned.
* "VIEW" returns only views
* "TABLE" returns only tables
* @param showSchema returns the schema/user with the table name, eg. USER.TABLE
* @param mask is the input mask - only supported by oci8 and postgresql
* @return array of tables for current database.
function MetaTables( $ttype = false, $showSchema = false, $mask= false ) {
error_log( '$gForceAdodb = TRUE; is needed on the page: '. $_SERVER['SCRIPT_FILENAME'] );
* @return # rows affected by UPDATE/DELETE
return $this->mDb->Affected_Rows();
// This function will handle all errors
$bindVars = !empty( $error_obj->backtrace[0]['object']->backtrace[2]['object']->_data ) ? $error_obj->backtrace[0]['object']->backtrace[2]['object']->_data : NULL;
'errno' => $error_obj->getCode(),
'db_msg'=> $error_obj->getMessage(),
'sql'=> $error_obj->getDebugInfo(). " ('". implode( "','", $bindVars ). "')",
return $pErrorObj->getDebugInfo();
|