kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file BitDbPear.php

Documentation is available at BitDbPear.php

  1. <?php
  2. /**
  3.  * ADOdb Library interface Class
  4.  *
  5.  * @package kernel
  6.  * @version $Header$
  7.  *
  8.  *  Copyright (c) 2004 bitweaver.org
  9.  *  Copyright (c) 2003 tikwiki.org
  10.  *  Copyright (c) 2002-2003, Luis Argerich, Garland Foster, Eduardo Polidor, et. al.
  11.  *  All Rights Reserved. See below for details and a complete list of authors.
  12.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  13.  *
  14.  * @author spider <spider@steelsun.com>
  15.  */
  16.  
  17. /**
  18.  * ensure your AdoDB install is a subdirectory off your include path
  19.  */
  20. require_once 'DB.php';
  21.  
  22. require_onceKERNEL_PKG_PATH.'BitDbBase.php' );
  23.  
  24. /**
  25.  * This class is used for database access and provides a number of functions to help
  26.  * with database portability.
  27.  *
  28.  * Currently used as a base class, this class should be optional to ensure bitweaver
  29.  * continues to function correctly, without a valid database connection.
  30.  *
  31.  * @package kernel
  32.  */
  33. class BitDbPear extends BitDb
  34. {
  35.     function __construct$pPearDsn=NULL$pPearOptions NULL )
  36.     {
  37.         global $gDebug;
  38.         parent::__construct();
  39.  
  40.         ifempty$pPearDsn ) ) {
  41.             global $gBitDbType$gBitDbUser$gBitDbPassword$gBitDbHost$gBitDbName;
  42.  
  43.             $pPearDsn array(
  44.                 'phptype'  => $gBitDbType,
  45.                 'username' => $gBitDbUser,
  46.                 'password' => $gBitDbPassword ,
  47.                 'database' => $gBitDbHost.'/'.$gBitDbName,
  48.             );
  49.         }
  50.  
  51.         ifempty$pPearOptions ) ) {
  52.             $pPearOptions array(
  53.                 'debug'       => 2,
  54.                 'persistent'  => false,
  55.                 'portability' => DB_PORTABILITY_ALL,
  56.             );
  57.         }
  58.  
  59.         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK'bit_pear_login_error' );
  60.  
  61.         $this->mDb = DB::connect($pPearDsn$pPearOptions);
  62.  
  63.         ifPEAR::isError$this->mDb ) ) {
  64.             $this->mErrors['db_connect'$this->mDb->getDebugInfo();
  65.         else {
  66.         
  67.             $this->mDb->setFetchModeDB_FETCHMODE_ASSOC );
  68.             // Default to autocommit unless StartTrans is called
  69.             $this->mDb->autoCommitTRUE );
  70.     
  71.             $this->mType = $pPearDsn['phptype'];
  72.             $this->mName = $pPearDsn['database'];
  73.         }
  74.  
  75.         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK'bit_pear_error_handler');
  76.     }
  77.     /**
  78.     * Quotes a string to be sent to the database which is
  79.     * passed to function on to AdoDB->qstr().
  80.     * @todo not sure what its supposed to do
  81.     * @param pStr string to be quotes
  82.     * @return quoted string using AdoDB->qstr()
  83.     */
  84.     function qstr($pStr)
  85.     {
  86.         return $this->mDb->quote($pStr);
  87.     }
  88.  
  89.     /** Queries the database, returning an error if one occurs, rather
  90.     * than exiting while printing the error. -rlpowell
  91.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  92.     *  and attribute names for AdoDB to quote appropriately.
  93.     * @param pError the error string to modify and return
  94.     * @param pValues an array of values used in a parameterised query
  95.     * @param pNumRows the number of rows (LIMIT) to return in this query
  96.     * @param pOffset the row number to begin returning rows from. Used in
  97.     * @return an AdoDB RecordSet object
  98.     *  conjunction with $pNumRows
  99.     * @todo currently not used anywhere.
  100.     */
  101.     function queryError$pQuery&$pError$pValues NULL$pNumRows = -1$pOffset = -)
  102.     {
  103.         $this->convertQuery($pQuery);
  104.         if ($pNumRows == -&& $pOffset == -1)
  105.         $result $this->mDb->Execute($pQuery$pValues);
  106.         else
  107.         $result $this->mDb->SelectLimit($pQuery$pNumRows$pOffset$pValues);
  108.         if (!$result)
  109.         {
  110.             $pError $this->mDb->ErrorMsg();
  111.             $result=false;
  112.         }
  113.         //count the number of queries made
  114.         $this->mNumQueries++;
  115.         //$this->debugger_log($pQuery, $pValues);
  116.         return $result;
  117.     }
  118.  
  119.     /** Queries the database reporting an error if detected
  120.     * than exiting while printing the error. -rlpowell
  121.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  122.     *  and attribute names for AdoDB to quote appropriately.
  123.     * @param pValues an array of values used in a parameterised query
  124.     * @param pNumRows the number of rows (LIMIT) to return in this query
  125.     * @param pOffset the row number to begin returning rows from. Used in
  126.     *  conjunction with $pNumRows
  127.     * @return an AdoDB RecordSet object
  128.     */
  129.     function query($query$values null$numrows BIT_QUERY_DEFAULT$offset BIT_QUERY_DEFAULT$pCacheTime=BIT_QUERY_DEFAULT )
  130.     {
  131.         $this->convertQuery($query);
  132.         ifempty$this->mDb ) ) {
  133.             return FALSE;
  134.         }
  135.  
  136.         $this->queryStart();
  137.  
  138.         if!is_numeric$numrows ) ) {
  139.             $numrows BIT_QUERY_DEFAULT;
  140.         }
  141.  
  142.         if!is_numeric$offset ) ) {
  143.             $offset BIT_QUERY_DEFAULT;
  144.         }
  145.  
  146.         if$numrows == BIT_QUERY_DEFAULT && $offset == BIT_QUERY_DEFAULT {
  147.             $result $this->mDb->query$query$values );
  148.         else {
  149.             $result $this->mDb->limitQuery$query$offset$numrows$values );
  150.         }
  151.  
  152.         $this->queryComplete();
  153.  
  154.         return $result;
  155.     }
  156.  
  157.     function queryComplete({
  158.         if!empty$this->mDebug ) ) {
  159.             print"<tt>\n".vsprintfstr_replace'?'"'%s'"$this->mDb->last_query )$this->mDb->last_parameters )."\n</tt><br/>" );
  160.             if$this->mDebug == 99 {
  161.                 bt();
  162.             }
  163.         }
  164.         parent::queryComplete();
  165.     }
  166.  
  167.     /**
  168.     * compatibility function
  169.     */
  170.     function Execute($pQuery$pNumRows false$zf_cache false$pCacheTime=BIT_QUERY_DEFAULT{
  171.         return $this->query$pQueryNULL$pNumRowsBIT_QUERY_DEFAULT$pCacheTime );
  172.     }
  173.  
  174.     /** 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.
  175.     * See AdoDB GetCol() function for more detail.
  176.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  177.     *  and attribute names for AdoDB to quote appropriately.
  178.     * @param pValues an array of values used in a parameterised query
  179.     * @param pForceArray if set to true, when an array is created for each value
  180.     * @param pFirst2Cols if set to true, only returns the first two columns
  181.     * @return the associative array, or false if an error occurs
  182.     * @todo not currently used anywhere
  183.     */
  184.  
  185.     function getCol$pQuery$pValues=array()$pTrim=FALSE )
  186.     {
  187.         ifempty$this->mDb ) ) {
  188.             return FALSE;
  189.         }
  190.         $this->queryStart();
  191.         $this->convertQuery($pQuery);
  192.         $execFunction !defined'IS_LIVE' || $pCacheTime == BIT_QUERY_DEFAULT 'GetAssoc' 'CacheGetAssoc' );
  193.         $result $this->mDb->getCol$pQuery0$pValues );
  194.         //count the number of queries made
  195.         $this->queryComplete();
  196.         return $result;
  197.     }
  198.  
  199.     /** Returns an associative array for the given query.
  200.     * See AdoDB GetAssoc() function for more detail.
  201.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  202.     *  and attribute names for AdoDB to quote appropriately.
  203.     * @param pValues an array of values used in a parameterised query
  204.     * @param pForceArray if set to true, when an array is created for each value
  205.     * @param pFirst2Cols if set to true, only returns the first two columns
  206.     * @return the associative array, or false if an error occurs
  207.     */
  208.     function getArray$pQuery$pValues=FALSE$pForceArray=FALSE$pFirst2Cols=FALSE$pCacheTime=BIT_QUERY_DEFAULT )
  209.     {
  210.         ifempty$this->mDb ) ) {
  211.             return FALSE;
  212.         }
  213.         $this->queryStart();
  214.         $this->convertQuery($pQuery);
  215.         $execFunction !defined'IS_LIVE' || $pCacheTime == BIT_QUERY_DEFAULT 'GetArray' 'CacheGetArray' );
  216.         $result $this->mDb->GetArray$pQuery$pValues$pForceArray$pFirst2Cols );
  217.         $this->queryComplete();
  218.         return $result;
  219.     }
  220.  
  221.     function getAll$pQuery$pValues=FALSE$pCacheTime=BIT_QUERY_DEFAULT {
  222.         ifempty$this->mDb ) ) {
  223.             return FALSE;
  224.         }
  225.         $this->queryStart();
  226.         $this->convertQuery($pQuery);
  227.         $result $this->mDb->GetAll$pQuery$pValues );
  228.         $this->queryComplete();
  229.         return $result;
  230.     }
  231.  
  232.     /** Returns an associative array for the given query.
  233.     * See AdoDB GetAssoc() function for more detail.
  234.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  235.     *  and attribute names for AdoDB to quote appropriately.
  236.     * @param pValues an array of values used in a parameterised query
  237.     * @param pForceArray if set to true, when an array is created for each value
  238.     * @param pFirst2Cols if set to true, only returns the first two columns
  239.     * @return the associative array, or false if an error occurs
  240.     */
  241.     function getAssoc$pQuery$pValues=array()$pForceArray=FALSE$pFirst2Cols=FALSE$pCacheTime=BIT_QUERY_DEFAULT )
  242.     {
  243.         ifempty$this->mDb ) ) {
  244.             return FALSE;
  245.         }
  246.         $this->queryStart();
  247.         $this->convertQuery($pQuery);
  248.         $result $this->mDb->getAssoc$pQuery$pForceArray$pValues$pFirst2Cols );
  249.         $this->queryComplete();
  250.         return $result;
  251.     }
  252.  
  253.     /** 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.
  254.     * See AdoDB GetRow() function for more detail.
  255.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  256.     *  and attribute names for AdoDB to quote appropriately.
  257.     * @param pValues an array of values used in a parameterised query
  258.     * @return returns the first row as an array, or false if an error occurs
  259.     */
  260.     function getRow$pQuery$pValues=FALSE$pCacheTime=BIT_QUERY_DEFAULT )
  261.     {
  262.         ifempty$this->mDb ) ) {
  263.             return FALSE;
  264.         }
  265.         $this->queryStart();
  266.         $this->convertQuery($pQuery);
  267.         $result $this->mDb->GetRow$pQuery$pValues );
  268.         $this->queryComplete();
  269.         return $result;
  270.     }
  271.  
  272.     /** Returns a single column value from the database.
  273.     * @param pQuery the SQL query. Use backticks (`) to quote all table
  274.     *  and attribute names for AdoDB to quote appropriately.
  275.     * @param pValues an array of values used in a parameterised query
  276.     * @param pReportErrors report errors to STDOUT
  277.     * @param pOffset the row number to begin returning rows from.
  278.     * @return the associative array, or false if an error occurs
  279.     */
  280.     function getOne($pQuery$pValues=NULL$pNumRows=NULL$pOffset=NULL$pCacheTime BIT_QUERY_DEFAULT )
  281.     {
  282.         $value NULL;
  283.         if$result $this->query($pQuery$pValues10$pCacheTime ) ) {
  284.             if$res $result->fetchRow() ) {
  285.                 reset$res );
  286.                 $value current($res);
  287.             }
  288.         }
  289.         return $value;
  290.     }
  291.  
  292.     /**
  293.     * A database portable Sequence management function.
  294.     *
  295.     * @param pSequenceName Name of the sequence to be used
  296.     *         It will be created if it does not already exist
  297.     * @return        if not supported, otherwise a sequence id
  298.     */
  299.     function GenID$pSequenceName$pUseDbPrefix true {
  300.         ifempty$this->mDb ) ) {
  301.             return FALSE;
  302.         }
  303.         // Pear appends _seq just to be a pain
  304.         if ($pUseDbPrefix{
  305.             $prefix str_replace("`","",BIT_DB_PREFIX);
  306.         }
  307.         else {
  308.             $prefix '';
  309.         }
  310.         $seqName  str_replace'_seq'''$prefix.$pSequenceName );
  311.         return $this->mDb->nextId$seqName );
  312.     }
  313.  
  314.     /**
  315.     * A database portable Sequence management function.
  316.     *
  317.     * @param pSequenceName Name of the sequence to be used
  318.     *         It will be created if it does not already exist
  319.     * @param pStartID Allows setting the initial value of the sequence
  320.     * @return        if not supported, otherwise a sequence id
  321.     * @todo    To be combined with GenID
  322.     */
  323.     function CreateSequence($seqname='adodbseq',$startID=1)
  324.     {
  325.         if (empty($this->mDb->_genSeqSQL)) return FALSE;
  326.         return $this->mDb->CreateSequence($seqname$startID);
  327.     }
  328.  
  329.     /**
  330.     * A database portable IFNULL function.
  331.     *
  332.     * @param pField argument to compare to NULL
  333.     * @param pNullRepl the NULL replacement value
  334.     * @return string that represents the function that checks whether
  335.     *  $pField is NULL for the given database, and if NULL, change the
  336.     *  value returned to $pNullRepl.
  337.     */
  338.     function ifNull($pField$pNullRepl)
  339.     {
  340.         return $this->mDb->ifNull($pField$pNullRepl);
  341.     }
  342.  
  343.     /** Format the timestamp in the format the database accepts.
  344.     * @param pDate a Unix integer timestamp or an ISO format Y-m-d H:i:s
  345.     * @return the timestamp as a quoted string.
  346.     * @todo could be used to later convert all int timestamps into db
  347.     *  timestamps. Currently not used anywhere.
  348.     */
  349.     function ls($pDate)
  350.     {
  351.         // not sure what this did - maybe someone can comment why its here
  352.         //return preg_replace("/'/","", $this->mDb->DBTimeStamp($pDate));
  353.         return $this->mDb->DBTimeStamp($pDate);
  354.     }
  355.  
  356.     /**
  357.      * Format date column in sql string given an input format that understands Y M D
  358.      */
  359.     function SQLDate($pDateFormat$pBaseDate=false{
  360.         return $this->mDb->SQLDate($pDateFormat$pBaseDate;
  361.     }
  362.  
  363.     /**
  364.      * Calculate the offset of a date for a particular database and generate
  365.      * appropriate SQL. Useful for calculating future/past dates and storing
  366.      * in a database.
  367.      * @param pDays Number of days to offset by
  368.      *         If dayFraction=1.5 means 1.5 days from now, 1.0/24 for 1 hour.
  369.      * @param pColumn Value to be offset
  370.      *         If NULL an offset from the current time is supplied
  371.      * @return New number of days
  372.      *
  373.      * @todo Not currently used - this is database specific and uses TIMESTAMP
  374.      *  rather than unix seconds
  375.      */
  376.     function OffsetDate$pDays$pColumn=NULL {
  377.         return $this->mDb->OffsetDate$pDays$pColumn );
  378.     }
  379.  
  380.     /**
  381.      *    Improved method of initiating a transaction. Used together with CompleteTrans().
  382.      *    Advantages include:
  383.      *
  384.      *    a. StartTrans/CompleteTrans is nestable, unlike BeginTrans/CommitTrans/RollbackTrans.
  385.      *       Only the outermost block is treated as a transaction.<br>
  386.      *    b. CompleteTrans auto-detects SQL errors, and will rollback on errors, commit otherwise.<br>
  387.      *    c. All BeginTrans/CommitTrans/RollbackTrans inside a StartTrans/CompleteTrans block
  388.      *       are disabled, making it backward compatible.
  389.      */
  390.     function StartTrans({
  391.          return$this->mDb->autoCommitFALSE == DB_OK);
  392.     }
  393.  
  394.     /**
  395.      *    Used together with StartTrans() to end a transaction. Monitors connection
  396.      *    for sql errors, and will commit or rollback as appropriate.
  397.      *
  398.      *    autoComplete if true, monitor sql errors and commit and rollback as appropriate,
  399.      *    and if set to false force rollback even if no SQL error detected.
  400.      *    @returns true on commit, false on rollback.
  401.      */
  402.     function CompleteTrans({
  403.         $ret $this->mDb->commitFALSE == DB_OK;
  404.         $this->mDb->autoCommitTRUE );
  405.         return$ret );
  406.     }
  407.  
  408.     /**
  409.      * If database does not support transactions, rollbacks always fail, so return false
  410.      * otherwise returns true if the Rollback was successful
  411.      *
  412.      * @return true/false. 
  413.      */
  414.     function RollbackTrans({
  415.         $ret $this->mDb->rollbackFALSE == DB_OK;
  416.         $this->mDb->autoCommitTRUE );
  417.         return$ret );
  418.     }
  419.  
  420.     /**
  421.      * Create a list of tables available in the current database
  422.      *
  423.      * @param ttype can either be 'VIEW' or 'TABLE' or false.
  424.      *          If false, both views and tables are returned.
  425.      *         "VIEW" returns only views
  426.      *         "TABLE" returns only tables
  427.      * @param showSchema returns the schema/user with the table name, eg. USER.TABLE
  428.      * @param mask  is the input mask - only supported by oci8 and postgresql
  429.      *
  430.      * @return  array of tables for current database.
  431.      */
  432.     function MetaTables$ttype false$showSchema false$mask=false {
  433.         error_log'$gForceAdodb = TRUE; is needed on the page: '.$_SERVER['SCRIPT_FILENAME');
  434.         return array();
  435.     }
  436.  
  437.     /**
  438.     * @return rows affected by UPDATE/DELETE
  439.     */
  440.     function Affected_Rows({
  441.         return $this->mDb->Affected_Rows();
  442.     }
  443. }
  444.  
  445. // This function will handle all errors
  446. function bit_pear_error_handler$error_obj {
  447.     $bindVars !empty$error_obj->backtrace[0]['object']->backtrace[2]['object']->_data $error_obj->backtrace[0]['object']->backtrace[2]['object']->_data NULL;
  448.     $dbParams array(
  449.         'errno' => $error_obj->getCode(),
  450.         'db_msg'=> $error_obj->getMessage(),
  451.         'sql'=> $error_obj->getDebugInfo()." ('".implode"','"$bindVars )."')",
  452.     );
  453.  
  454.     $logString bit_error_string$dbParams );
  455.     bit_display_error$logString$dbParams['db_msg');
  456. }
  457.  
  458. function bit_pear_login_error$pErrorObj {
  459.     return $pErrorObj->getDebugInfo();
  460. }
  461.  
  462. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:50 +0000 by phpDocumentor 1.5.0-lsces