Location: PHPKode > projects > web.framework > web.framework-1.0.0/web.framework/DB/DBConnections.class.php
<?php
/**
 * Project:     web.framework: the PHP5 MVC framework
 * File:		DBConnections.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the authors of web.framework by e-mail at:
 * hide@address.com
 *
 * The latest version of web.framework can be obtained from:
 * http://sourceforge.net/projects/webframework
 *
 * @link http://sourceforge.net/projects/webframework
 * @copyright 2005 Marcin Staniszczak
 * @author Marcin Staniszczak <hide@address.com>
 * @version 1.0.0
 */


/**
 * This class connect to database.
 * It's singleton.
 *
 * @name DBConnections
 * @version 1.0.0
 * @package web.framework
 * @subpackage DB
 *
 * @author Marcin Staniszczak
 * @copyright 2005 Marcin Staniszczak
 */
class DBConnections {
	/**
	 * @access private
	 * @var DBConnections
	 */
	private static $objThis = null;

	private $arrDB = array();
	private $arrConfiguration = array();

	/**
	 * The class constructor (private)
	 *
	 * @access private
	 * @param array web.framework's configuration
	 */
	private function __construct($arrConfiguration) {
		$this->arrConfiguration = $arrConfiguration;
	}

	/**
	 * Make or return instance of this class
	 *
	 * @access public
	 * @param array web.framework's configuration
	 */
	public static function construct($arrConfiguration) {
		if(isset(self::$objThis)) {
			return self::$objThis;
		} else {
			self::$objThis = new self($arrConfiguration);
		}

		return self::$objThis;
	}

	/**
	 * Get DB connection
	 *
	 * @access public
	 * @param string variable name
	 * @return mixed value
	 */
	public function getDBConnection($strDB) {
		if (isset($this->arrDB[$strDB])) {
			return $this->arrDB[$strDB];
		} else {
			//make DB conection
			if (isset($this->arrConfiguration['datasources'][$strDB]['driver'])) {
				$strDBDriverName = $this->arrConfiguration['datasources'][$strDB]['driver'];
			} else {
				throw new WF_Action_getDB_Exception(sprintf(Languages::$MESSAGES[WebFramework::$strLanguage]['EXCEPTIONS']['ACTIONS']['NO_DB_DRIVER'], $strDB));
			}

			if (!isset($this->arrConfiguration['datasources'][$strDB]['driver'])) {
				throw new WF_Action_Exception(sprintf(Languages::$MESSAGES[WebFramework::$strLanguage]['EXCEPTIONS']['NO_DB_DRIVER_SPECIFIED'], $strDB));
			}

			if (strcmp($strDBDriverName, 'PDO')==0) {
				$this->arrDB[$strDB] = new PDO($this->arrConfiguration['datasources'][$strDB]['params']['DSN'],
					$this->arrConfiguration['datasources'][$strDB]['params']['user'],
					$this->arrConfiguration['datasources'][$strDB]['params']['password']);
			} elseif (strcmp($strDBDriverName, 'ADOdb')==0) {
				$this->arrDB[$strDB] = ADONewConnection(strtolower('mysql'));
				$this->arrDB[$strDB]->Connect($this->arrConfiguration['datasources'][$strDB]['params']['host'],
					$this->arrConfiguration['datasources'][$strDB]['params']['user'],
					$this->arrConfiguration['datasources'][$strDB]['params']['password'],
					$this->arrConfiguration['datasources'][$strDB]['params']['dbname']);
			} elseif (strcmp($strDBDriverName, 'web.db')==0) {
				$this->arrDB[$strDB] = WebDB::connect($strDB, array(
						'hostname' => $this->arrConfiguration['datasources'][$strDB]['params']['host'],
						'db' => $this->arrConfiguration['datasources'][$strDB]['params']['dbname'],
						'port' => $this->arrConfiguration['datasources'][$strDB]['params']['port'],
						'userName' => $this->arrConfiguration['datasources'][$strDB]['params']['user'],
						'password' => $this->arrConfiguration['datasources'][$strDB]['params']['password'],
						'server' => isset($this->arrConfiguration['datasources'][$strDB]['params']['server']) ? $this->arrConfiguration['datasources'][$strDB]['params']['server'] : '',
						'row_cache' => isset($this->arrConfiguration['datasources'][$strDB]['params']['row_cache']) ? $this->arrConfiguration['datasources'][$strDB]['params']['row_cache'] : null,
					));
			} else {
				throw new WF_Action_GetDB_Exception(sprintf(Languages::$MESSAGES[WebFramework::$strLanguage]['EXCEPTIONS']['ACTIONS']['UNKNOWN_DRIVER'], $strDB));
			}

			return $this->arrDB[$strDB];
		}
	}

}
?>
Return current item: web.framework