2005-05-21 22:40:51 +00:00
|
|
|
<?PHP
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// + $Id$
|
|
|
|
// +------------------------------------------------------------------+ //
|
|
|
|
// + Cake <https://developers.nextco.com/cake/> + //
|
|
|
|
// + Copyright: (c) 2005, Cake Authors/Developers + //
|
|
|
|
// + Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com> + //
|
|
|
|
// + Larry E. Masters aka PhpNut <nut@phpnut.com> + //
|
|
|
|
// + Kamil Dzielinski aka Brego <brego.dk@gmail.com> + //
|
|
|
|
// +------------------------------------------------------------------+ //
|
|
|
|
// + Licensed under The MIT License + //
|
|
|
|
// + Redistributions of files must retain the above copyright notice. + //
|
|
|
|
// + See: http://www.opensource.org/licenses/mit-license.php + //
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
2005-06-23 20:41:10 +00:00
|
|
|
* Purpose: DbFactory
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Creates DBO-descendant objects from a given db connection configuration
|
|
|
|
*
|
|
|
|
* @filesource
|
|
|
|
* @author Cake Authors/Developers
|
|
|
|
* @copyright Copyright (c) 2005, Cake Authors/Developers
|
|
|
|
* @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
2005-05-21 22:40:51 +00:00
|
|
|
|
|
|
|
/**
|
2005-06-23 20:41:10 +00:00
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
2005-06-18 23:26:35 +00:00
|
|
|
uses('object');
|
2005-05-23 23:08:43 +00:00
|
|
|
config('database');
|
2005-05-21 22:40:51 +00:00
|
|
|
|
2005-06-05 04:43:07 +00:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
2005-06-23 20:41:10 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 1.0.0.0
|
|
|
|
*
|
2005-06-05 04:43:07 +00:00
|
|
|
*/
|
2005-06-23 20:41:10 +00:00
|
|
|
class DboFactory extends Object
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A semi-singelton. Returns actual instance, or creates a new one with given config.
|
|
|
|
*
|
|
|
|
* @param string $config Name of key of $dbConfig array to be used.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function getInstance($config = null)
|
2005-06-18 23:26:35 +00:00
|
|
|
{
|
2005-06-23 20:41:10 +00:00
|
|
|
static $instance;
|
2005-05-21 22:40:51 +00:00
|
|
|
|
2005-06-23 20:41:10 +00:00
|
|
|
if (!isset($instance))
|
2005-06-18 23:26:35 +00:00
|
|
|
{
|
2005-06-23 20:41:10 +00:00
|
|
|
if ($config == null)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$configs = get_class_vars('DATABASE_CONFIG');
|
|
|
|
$config = $configs[$config];
|
|
|
|
|
|
|
|
// special case for AdoDB -- driver name in the form of 'adodb-drivername'
|
|
|
|
if (preg_match('#^adodb[\-_](.*)$#i', $config['driver'], $res))
|
|
|
|
{
|
|
|
|
uses('dbo/dbo_adodb');
|
|
|
|
$config['driver'] = $res[1];
|
2005-05-21 22:40:51 +00:00
|
|
|
|
2005-06-23 20:41:10 +00:00
|
|
|
$instance = array(DBO_AdoDB($config));
|
|
|
|
}
|
|
|
|
// special case for PEAR:DB -- driver name in the form of 'pear-drivername'
|
|
|
|
elseif (preg_match('#^pear[\-_](.*)$#i', $config['driver'], $res))
|
2005-06-18 23:26:35 +00:00
|
|
|
{
|
2005-06-23 20:41:10 +00:00
|
|
|
uses('dbo/dbo_pear');
|
|
|
|
$config['driver'] = $res[1];
|
|
|
|
|
|
|
|
$instance = array(new DBO_Pear($config));
|
2005-05-21 22:40:51 +00:00
|
|
|
}
|
2005-06-23 20:41:10 +00:00
|
|
|
// regular, Cake-native db drivers
|
2005-06-18 23:26:35 +00:00
|
|
|
else
|
|
|
|
{
|
2005-06-23 20:41:10 +00:00
|
|
|
$db_driver_class = 'DBO_'.$config['driver'];
|
|
|
|
$db_driver_fn = LIBS.strtolower('dbo'.DS.$db_driver_class.'.php');
|
|
|
|
|
|
|
|
if (file_exists($db_driver_fn))
|
|
|
|
{
|
|
|
|
uses(strtolower('dbo'.DS.$db_driver_class));
|
|
|
|
$instance = array(new $db_driver_class($config));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trigger_error(ERROR_UNKNOWN_DATABASE_DRIVER, E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
2005-05-21 22:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
2005-06-23 20:41:10 +00:00
|
|
|
|
|
|
|
return $instance[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets config to use. If there is already a connection, close it first.
|
|
|
|
*
|
|
|
|
* @param string $configName Name of the config array key to use.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function setConfig($config)
|
|
|
|
{
|
|
|
|
$db = DboFactory::getInstance();
|
|
|
|
if ($db->isConnected() === true)
|
|
|
|
{
|
|
|
|
$db->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getInstance($config);
|
2005-05-21 22:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-17 20:55:27 +00:00
|
|
|
?>
|