Location: PHPKode > projects > web.framework > web.framework-1.0.0/web.framework/DB/WebDB.class.php
<?php
/**
 * Project:     web.framework: the PHP5 MVC framework
 * File:		WebDB.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
 */

if(!defined('WEBDB_DIR'))
    define('WEBDB_DIR', dirname(__FILE__).DIRECTORY_SEPARATOR);

if(!defined('WEBDB_DRIVERS_DIR'))
	define('WEBDB_DRIVERS_DIR', WEBDB_DIR.'Drivers'.DIRECTORY_SEPARATOR);

if(!defined('WEBDB_CORE_DIR'))
	define('WEBDB_CORE_DIR', WEBDB_DIR.'Core'.DIRECTORY_SEPARATOR);

/**
 * web.db's main class
 *
 * @name WebDB
 * @version 1.0.0
 * @package web.framework
 * @subpackage web.db
 *
 * @author Marcin Staniszczak
 * @copyright 2005 Marcin Staniszczak
 */
class WebDB {
	static public
		$strLanguage = 'ENGLISH';

	/**
	 * Set messages language
	 *
	 * @static
	 * @access public
	 * @param string language name
	 */
	static public function setLanguage($strLanguage) {
		self::$strLanguage = $strLanguage;
	}

	/**
	 * Connect to database
	 *
	 * @static
	 * @access public
	 * @param string driver name (MySQL, PostgreSQL, SQLite, MSSQL_ODBTP)
	 * @param array array with connection settings - array('hostname'=>host name, 'db'=>db name, 'port'=>port, 'userName'=>db user name, 'password'=>db user password)
	 * @param boolean for presistant connection set true, otherwise set false
	 * @return AWebDBDriver driver object
	 */
	static public function connect($strDriver, $arrSettings, $blnPersistent=false) {
		if (is_dir(WEBDB_DRIVERS_DIR.$strDriver)) {
			require_once(WEBDB_CORE_DIR.'WebDBDriver.abstract.class.php');
			require_once(WEBDB_DRIVERS_DIR.$strDriver.DIRECTORY_SEPARATOR.$strDriver.'Driver.class.php');

			$strDriverName = $strDriver.'Driver';
			$objDriver = new $strDriverName(self::$strLanguage, $arrSettings, $blnPersistent);

			if (!($objDriver instanceof AWebDBDriver)) {
				throw new WD_DriverType_Exception();
			}

			return $objDriver;
		} else {
			throw new WD_DriverNotFound_Exception($strDriver);
		}
	}
}

/**
 * Main web.db exceptions class
 *
 * @name WD_Exception
 * @version 1.0.0
 * @package web.framework
 * @subpackage exceptions
 *
 * @author Marcin Staniszczak
 * @copyright 2005 Marcin Staniszczak
 */
class WD_Exception extends Exception {
	protected
		$strNativeError;

	/**
	 * The class constructor
	 *
	 * @access public
	 * @param string error message
	 * @param string extended error message
	 */
	public function __construct($strMessage, $strError=null) {
		parent::__construct($strMessage);

		if ($strError !== null) {
			$this->setNativeError($strError);
		}
	}

	/**
	 * Native error message
	 *
	 * @access public
	 * @param string error message
	 */
	public function setNativeError($strMessage) {
		$this->strNativeError = $strMessage;
		$this->message .= ' [Native Error: '.$this->strNativeError.']';
	}

}

/**
 * No driver found exception
 * @package web.framework
 * @subpackage exceptions
 */
class WD_DriverNotFound_Exception extends WD_Exception {}

/**
 * Unknown driver exception (Driver must extend AWebDBDriver)
 * @package web.framework
 * @subpackage exceptions
 */
class WD_DriverType_Exception extends WD_Exception {}
?>
Return current item: web.framework