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

Source for file DBConnectionStack.class.php

Documentation is available at DBConnectionStack.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: DBConnectionStack.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @uses stacked connection handler
  11.  * @package Zikula_Core
  12.  */
  13.  
  14.  
  15. /**
  16.  * This class maintains a stack of database connections. Getting a connection
  17.  * will always return the connection object which is currently on top of the
  18.  * connections stack (ie: the latest added connection).
  19.  *
  20.  * @package Zikula_Core
  21.  * @subpackage DBConnectionStack
  22.  */
  23. {
  24.     /**
  25.      * Initialize a DBConnection and place it on the connection stack
  26.      *
  27.      * @param name        The database alias name in the DBInfo configuration array (optional) (default=null which then defaults to 'default')
  28.      *
  29.      * @return string The file name which was loaded
  30.      */
  31.     function init ($name=null)
  32.     {
  33.         if (!$name{
  34.             $name 'default';
  35.         }
  36.  
  37.         if (!isset($GLOBALS['PNConfig']['DBInfo'][$name]||
  38.             !is_array($GLOBALS['PNConfig']['DBInfo'][$name])) {
  39.             return pn_exit ("Invalid connection key [$name] ...");
  40.         }
  41.  
  42.         $count (isset($GLOBALS['PNRuntime']['DB']count($GLOBALS['PNRuntime']['DB']0);
  43.         if (!defined('_PNINSTALLVER'&& $count && $name == $GLOBALS['PNRuntime']['DB'][$count-1]['name']{
  44.             return pn_exit ("Attempting to init database [$name] which is the active database already ...");
  45.         }
  46.  
  47.         // Get database parameters
  48.         $dbtype   $GLOBALS['PNConfig']['DBInfo'][$name]['dbtype'];
  49.         $dbhost   $GLOBALS['PNConfig']['DBInfo'][$name]['dbhost'];
  50.         $dbname   $GLOBALS['PNConfig']['DBInfo'][$name]['dbname'];
  51.         $dbuname  $GLOBALS['PNConfig']['DBInfo'][$name]['dbuname'];
  52.         $dbpass   $GLOBALS['PNConfig']['DBInfo'][$name]['dbpass'];
  53.         $pconnect $GLOBALS['PNConfig']['DBInfo'][$name]['pconnect'];
  54.         $dbcharset$GLOBALS['PNConfig']['DBInfo'][$name]['dbcharset'];
  55.  
  56.         // Start connection
  57.         // itevo: /Go; it's more safe to use NConnect instead of Connect because of the following:
  58.         // If you create two connections, but both use the same userid and password, PHP will share the
  59.         // same connection. This can cause problems if the connections are meant to different databases.
  60.         // The solution is to always use different userid's for different databases, or use NConnect().
  61.         // NConnect: Always force a new connection. In contrast, PHP sometimes reuses connections when
  62.         // you use Connect() or PConnect(). Currently works only on mysql (PHP 4.3.0 or later), postgresql
  63.         // and oci8-derived drivers. For other drivers, NConnect() works like Connect().
  64.         // If installer, suppress errors because of installer issues when anon db user isnt available - drak
  65.         $conn ADONewConnection($dbtype);
  66.  
  67.         // debug setting for adodb sql
  68.         if (isset($GLOBALS['PNConfig']['Debug']['sql_adodb']&& $GLOBALS['PNConfig']['Debug']['sql_adodb']{
  69.             $conn->debug = (int)$GLOBALS['PNConfig']['Debug']['sql_adodb'];
  70.         }
  71.  
  72.         $func ($pconnect 'PConnect' 'NConnect');
  73.         if (defined('_PNINSTALLVER')) {
  74.             $dbh @$conn->$func ($dbhost$dbuname$dbpass$dbname);
  75.         else {
  76.             $dbh $conn->$func ($dbhost$dbuname$dbpass$dbname);
  77.         }
  78.  
  79.         if (!$dbh{
  80.             return false;
  81.         }
  82.  
  83.         global $ADODB_FETCH_MODE;
  84.         $ADODB_FETCH_MODE ADODB_FETCH_NUM;
  85.  
  86.         // force oracle to a consistent date format for comparison methods later on
  87.         $dbdriver _adodb_getdriver($conn->dataProvider$conn->databaseTypetrue);
  88.         if ($dbdriver=='oci8' || $dbdriver=='oracle'{
  89.             $res $conn->Execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'");
  90.             $res $conn->Execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'");
  91.         }
  92.         // PN .8: set db to utf8 if defined in config.php and the db is in utf8 - for future versions we need to check for inconsistent charsets (BP)
  93.         if ($dbdriver=='mysql' || $dbdriver=='mysqli'{
  94.              if (isset($dbcharset)) {
  95.                  $dbcharset strtolower($dbcharset);
  96.                  $curdbcharset $conn->Execute("SHOW VARIABLES LIKE 'character_set_database'");
  97.                 if ($dbcharset == 'utf8' && $curdbcharset->fields[1== 'utf8'{
  98.                      $conn->Execute('set names utf8');
  99.                 }
  100.              }
  101.         }
  102.  
  103.         $GLOBALS['PNRuntime']['DB'][$count]             $GLOBALS['PNConfig']['DBInfo'][$name];
  104.         $GLOBALS['PNRuntime']['DB'][$count]['alias']    $name;
  105.         $GLOBALS['PNRuntime']['DB'][$count]['dbconn']   $conn;
  106.         $GLOBALS['PNRuntime']['DB'][$count]['dbdriver'$dbdriver;
  107.         // add the table prefix as this makes integrating other applications easier
  108.         $GLOBALS['PNRuntime']['DB'][$count]['prefix']   $GLOBALS['PNConfig']['System']['prefix'].'_';
  109.         return $conn;
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Get the DB connection info structure for a connection as defined in config.php.
  115.      * If $field is supplied, the value of the specified field is retuerned, otherwise
  116.      * the entire connection info array is returned.
  117.      *
  118.      * @param  name   the name of the connection info to get. Passing null returns the current (ie: top) connection (optional) (default=null)
  119.      * @param  field  the field of the connection info record to return
  120.      *
  121.      * @return string The connection info array or the specified field value
  122.      */
  123.     function getConnectionInfo ($name=null$field=null)
  124.     {
  125.         $count (isset($GLOBALS['PNRuntime']['DB']count($GLOBALS['PNRuntime']['DB']0);
  126.         if (!$count{
  127.             return pn_exit ('Attempted to get info from empty connection stack ...');
  128.         }
  129.  
  130.         if (!$name{
  131.             $name $GLOBALS['PNRuntime']['DB'][$count-1]['alias'];
  132.         }
  133.  
  134.         if (!isset($GLOBALS['PNConfig']['DBInfo'][$name]||
  135.             !is_array($GLOBALS['PNConfig']['DBInfo'][$name])) {
  136.             return pn_exit ("Invalid connection key [$name] ...");
  137.         }
  138.  
  139.         if ($field{
  140.             if (isset($GLOBALS['PNRuntime']['DB'][$count-1][$field])) {
  141.                 return $GLOBALS['PNRuntime']['DB'][$count-1][$field];
  142.             else {
  143.                 return pn_exit("DBConnectionClass::getConnectionInfo: unknown field [$field] requested");
  144.             }
  145.         }
  146.  
  147.         return $GLOBALS['PNRuntime']['DB'][$count-1];
  148.     }
  149.  
  150.  
  151.     /**
  152.      * Get the alias name name of the currently active connection
  153.      *
  154.      * @return string the name of the currently active connection
  155.      */
  156.     function getConnectionName ()
  157.     {
  158.         return DBConnectionStack::getConnectionInfo (null'alias');
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Get the DB Alias name of the currently active connection
  164.      *
  165.      * @return string the dbname of the currently active connection
  166.      */
  167.     function getConnectionDBName ()
  168.     {
  169.         return DBConnectionStack::getConnectionInfo (null'dbname');
  170.     }
  171.  
  172.  
  173.     /**
  174.      * Get the DB Host of the currently active connection
  175.      *
  176.      * @return string the host of the currently active connection
  177.      */
  178.     function getConnectionDBHost ()
  179.     {
  180.         return DBConnectionStack::getConnectionInfo (null'dbhost');
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Get the DB Type of the currently active connection
  186.      *
  187.      * @return string the type of the currently active connection
  188.      */
  189.     function getConnectionDBType ()
  190.     {
  191.         return DBConnectionStack::getConnectionInfo (null'dbtype');
  192.     }
  193.  
  194.  
  195.     /**
  196.      * Get the DB driver of the currently active connection. This is not necessarily the same as the DB Type and
  197.      * should be used to distinguish between different database types in ADODB
  198.      * e.g. both oracle and oci8 use the oci8 driver to load the correct data dictionary include file
  199.      *
  200.      * @return string the driver of the currently active connection
  201.      */
  202.     function getConnectionDBDriver ()
  203.     {
  204.         $dbdriver 'undefined';
  205.         if (defined('_PNINSTALLVER')) {
  206.             // we are installing PN right now so we might not have valid db connection yet
  207.             $conn pnDBGetConn(true);
  208.             // hello database?
  209.             if ($conn !== false && is_object($conn)) {
  210.                 $dbdriver _adodb_getdriver($conn->dataProvider$conn->databaseTypetrue);
  211.             }
  212.         else {
  213.             $dbdriver DBConnectionStack::getConnectionInfo (null'dbdriver');
  214.         }
  215.         return $dbdriver;
  216.     }
  217.  
  218.  
  219.     /**
  220.      * Check whether the current connection is the default one
  221.      *
  222.      * @return boolean whether or not the current connection is the default one
  223.      */
  224.     function isDefaultConnection ()
  225.     {
  226.         $name DBConnectionStack::getConnectionName ();
  227.         return (strcmp ($name'default')==0);
  228.     }
  229.  
  230.  
  231.     /**
  232.      * Get the currently active connection (the connection on top of the connection stack)
  233.      *
  234.      * @param fetchmod        The fetchmode to set for the connection
  235.      *
  236.      * @return the connection object
  237.      */
  238.     function getConnection ($fetchmode=ADODB_FETCH_NUM)
  239.     {
  240.         if (!isset($GLOBALS['PNRuntime']['DB']&& defined('_PNINSTALLVER')) {
  241.             return;
  242.         }
  243.  
  244.         $count count($GLOBALS['PNRuntime']['DB']);
  245.         if (!$count{
  246.             if (defined('_PNINSTALLVER')) {
  247.                 return;
  248.             else {
  249.                 return pn_exit ('Attempted to get connection from empty connection stack ...');
  250.             }
  251.         }
  252.         $conn $GLOBALS['PNRuntime']['DB'][$count-1]['dbconn'];
  253.  
  254.         switch($fetchmode)
  255.         {
  256.             case ADODB_FETCH_NUM :
  257.                 $dbdriver _adodb_getdriver($conn->dataProvider$conn->databaseTypetrue);
  258.                 if ($dbdriver=='postgres'{
  259.                     // RNG: workwound for installer errors / adodb V4.94 pgsql driver bug: see http://phplens.com/lens/lensforum/msgs.php?id=15248&x=1
  260.                     $conn->SetFetchMode(ADODB_FETCH_BOTH);
  261.                 else {
  262.                     $conn->SetFetchMode(ADODB_FETCH_NUM);
  263.                 }
  264.                 break;
  265.             case ADODB_FETCH_ASSOC :
  266.                 $conn->SetFetchMode(ADODB_FETCH_ASSOC);
  267.                 break;
  268.             case ADODB_FETCH_DEFAULT :
  269.                 $conn->SetFetchMode(ADODB_FETCH_DEFAULT);
  270.                 break;
  271.             case ADODB_FETCH_BOTH:
  272.                 $conn->SetFetchMode(ADODB_FETCH_BOTH);
  273.                 break;
  274.         }
  275.  
  276.         return $conn;
  277.     }
  278.  
  279.  
  280.     /**
  281.      * Push a new database connection onto the connection stack
  282.      *
  283.      * @param name        The database alias name in the DBInfo configuration array
  284.      *
  285.      * @return The database connection
  286.      */
  287.     function pushConnection ($name)
  288.     {
  289.         if (DBConnectionStack::init ($name)) {
  290.             return DBConnectionStack::getConnection ();
  291.         }
  292.  
  293.         return false;
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Pop the currently active connection off the stack.
  299.      *
  300.      * @param close       Whether or not to close the connection (optional) (default=false)
  301.      *
  302.      * @return boolean The newly active connection
  303.      */
  304.     function popConnection ($close=false)
  305.     {
  306.         $count count($GLOBALS['PNRuntime']['DB']);
  307.         if (!$count){
  308.             return pn_exit ('Attempted to pop connection from empty connection stack ...');
  309.         }
  310.  
  311.         if ($close{
  312.             $conn DBConnectionStack::getConnection ();
  313.             $conn->Close();
  314.         }
  315.  
  316.         unset ($GLOBALS['PNRuntime']['DB'][$count-1]);
  317.         return DBConnectionStack::getConnection ();
  318.     }
  319. }

Documentation generated on Fri, 18 Jul 2008 21:44:37 +0200 by phpDocumentor 1.4.1