Added connection registry
This commit is contained in:
parent
7bc8bce9fa
commit
7516b73aca
56
sys/db/dbreg.php
Normal file
56
sys/db/dbreg.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* OpenSQLManager
|
||||||
|
*
|
||||||
|
* Free Database manager for Open Source Databases
|
||||||
|
*
|
||||||
|
* @author Timothy J. Warren
|
||||||
|
* @copyright Copyright (c) 2012
|
||||||
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
||||||
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection registry
|
||||||
|
*
|
||||||
|
* Decouples the Settings class from the query builder
|
||||||
|
* and organizes database connections
|
||||||
|
*/
|
||||||
|
class DB_Reg {
|
||||||
|
|
||||||
|
private static $instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registry access method
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public static function &get_db($key)
|
||||||
|
{
|
||||||
|
if ( ! isset(self::$instance[$key]))
|
||||||
|
{
|
||||||
|
// The constructor sets the instance
|
||||||
|
new DBReg($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$instance[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
protected function __construct($key)
|
||||||
|
{
|
||||||
|
// Get the db connection parameters for the current database
|
||||||
|
$db_params = Settings::get_instance()->get_db($key);
|
||||||
|
|
||||||
|
// Set the current key in the registry
|
||||||
|
self::$instance[$key] = new Query_Builder($db_params);
|
||||||
|
}
|
||||||
|
}
|
@ -23,29 +23,14 @@ class Query_Builder {
|
|||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param mixed $conn_name - the name of the connection/parameters
|
* @param object $conn_name - the name of the connection/parameters
|
||||||
*/
|
*/
|
||||||
public function __construct($conn_name)
|
public function __construct($params)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Add some flexibility for testing
|
|
||||||
if(class_exists('settings'))
|
|
||||||
{
|
|
||||||
$this->settings = Settings::get_instance();
|
|
||||||
|
|
||||||
$params = (is_scalar($conn_name))
|
|
||||||
? $this->settings->get_db($conn_name)
|
|
||||||
: $conn_name;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$params = $conn_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
$params->type = strtolower($params->type);
|
$params->type = strtolower($params->type);
|
||||||
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
|
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
|
||||||
|
|
||||||
// Initiate the constructor for the
|
// Initiate the constructor for the selected database
|
||||||
switch($dbtype)
|
switch($dbtype)
|
||||||
{
|
{
|
||||||
default:
|
default:
|
||||||
|
Reference in New Issue
Block a user