Location: PHPKode > scripts > RC4PHP class > rc4php-class/rc4php_db_object.php
<?PHP
/**
*
* RC4PHP :  Raul's Classes For PHP <http://rc4php.sourceforge.net/>
* Copyright (c) 2006, Raul IONESCU
*                     Bucharest, ROMANIA
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @package      RC4PHP
* @copyright 	Copyright (c) 2006, Raul IONESCU.
* @author 	Raul IONESCU <hide@address.com>
* @license      http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 	0.6.3 (development)
* @category 	Factory class
* @access 	public
*
* PHP versions 5.1 or greater
*/
//////////////////////////////////////////////////////////////////
require_once('rc4php_autoload.php');
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
/**
* class RC4PHP_DB_OBJECT
*
* It's the class who acts as a factory method for all DB classes.
* 
* @access public
*/
class RC4PHP_DB_OBJECT extends RC4PHP
{
const DB_UNKNOWN=0;
const DB_SQLITE=1;
const DB_MYSQL=2;
const DB_MSSQL=3;
const DB_MICROSOFTSQL=3;
const DB_PGSQL=4;
const DB_POSTGRESQL=4;
//////////////////////////////////////////////////////////////////
protected static $instances=array();
//////////////////////////////////////////////////////////////////
public function __construct()
///class RC4PHP_DB_OBJECT/////////////////////////////////////////
{
parent::__construct();
}
//////////////////////////////////////////////////////////////////
/**
* Instantiate a new specific RC4PHP_DB_SQL object.
*
* Instead of using
*  $sql=new RC4PHP_DB_MSSQL('databaseName','serverNameOrIP','userName','password');
* you can also use following method
*  $sql=RC4PHP_DB_OBJECT::create(RC4PHP_DB_OBJECT::DB_MSSQL,'databaseName','serverNameOrIP','userName','password');
*
* @access public
* @param const  $dbSQLDBType
* @param string $dbSQLDBName
* @param string $dbSQLServer
* @param string $dbSQLUser
* @param string $dbSQLPassword
* @return object
*/
public static function create($dbSQLDBType=self::DB_UNKNOWN,$dbSQLDBName='',$dbSQLServer='localhost',$dbSQLUser='',$dbSQLPassword='')
///class RC4PHP_DB_OBJECT/////////////////////////////////////////
{
switch($dbSQLDBType)
        {
                case self::DB_SQLITE:
                        return new RC4PHP_DB_SQLite($dbSQLDBName);
        
                case self::DB_MYSQL:
                        return new RC4PHP_DB_MySQL($dbSQLDBName,$dbSQLServer,$dbSQLUser,$dbSQLPassword);
        
                case self::DB_MSSQL:
                        return new RC4PHP_DB_MSSQL($dbSQLDBName,$dbSQLServer,$dbSQLUser,$dbSQLPassword);
        
                case self::DB_POSTGRESQL:
                        return new RC4PHP_DB_PostgreSQL($dbSQLDBName,$dbSQLServer,$dbSQLUser,$dbSQLPassword);
                        
                default:        
                        throw new Exception('Invalid SQL DB type.',-1);
        }
}
//////////////////////////////////////////////////////////////////
/**
* Creates a new singleton instance for a specific RC4PHP_DB_SQL object.
*
* Instead of using
*  $sql=new RC4PHP_DB_MSSQL('databaseName','serverNameOrIP','userName','password');
* you can also use following method
*  $sql=RC4PHP_DB_OBJECT::create(RC4PHP_DB_OBJECT::DB_MSSQL,'databaseName','serverNameOrIP','userName','password');
*
* @access public
* @param string $instanceName
* @param const  $dbSQLDBType
* @param string $dbSQLDBName
* @param string $dbSQLServer
* @param string $dbSQLUser
* @param string $dbSQLPassword
* @return object
*/
public static function createNamedInstance($instanceName,$dbSQLDBType=self::DB_UNKNOWN,$dbSQLDBName='',$dbSQLServer='localhost',$dbSQLUser='',$dbSQLPassword='')
///class RC4PHP_DB_OBJECT/////////////////////////////////////////
{
if((!is_string($instanceName)) && (!is_integer($instanceName))) throw new Exception('Illegal offset type for instance name.',-1);
switch($dbSQLDBType)
        {
                case self::DB_SQLITE:
                        self::$instances[$instanceName] = new RC4PHP_DB_SQLite($dbSQLDBName);
                        break;
        
                case self::DB_MYSQL:
                        self::$instances[$instanceName] = new RC4PHP_DB_MySQL($dbSQLDBName,$dbSQLServer,$dbSQLUser,$dbSQLPassword);
                        break;
        
                case self::DB_MSSQL:
                        self::$instances[$instanceName] = new RC4PHP_DB_MSSQL($dbSQLDBName,$dbSQLServer,$dbSQLUser,$dbSQLPassword);
                        break;
        
                case self::DB_POSTGRESQL:
                        self::$instances[$instanceName] = new RC4PHP_DB_PostgreSQL($dbSQLDBName,$dbSQLServer,$dbSQLUser,$dbSQLPassword);
                        break;
                        
                default:        
                        throw new Exception('Invalid SQL DB type.',-1);
        }
return self::$instances[$instanceName];        
}
//////////////////////////////////////////////////////////////////
/**
* Returns the singleton instance for a specific RC4PHP_DB_SQL object, previously set by createInstance method.
*
* @access public
* @param string $instanceName
* @return object
*/
public static function getNamedInstance($instanceName)
///class RC4PHP_DB_OBJECT/////////////////////////////////////////
{
if((!is_string($instanceName)) && (!is_integer($instanceName))) throw new Exception('Illegal offset type for instance name.',-1);
if(isset(self::$instances[$instanceName])) return self::$instances[$instanceName];
throw new Exception('Instance name not found: '.$instanceName,-1);        
}
//////////////////////////////////////////////////////////////////

}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
?>
Return current item: RC4PHP class