2014-03-31 12:47:07 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
|
|
|
* @link https://github.com/aviat4ion/Query
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
namespace Query;
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
/**
|
|
|
|
* Connection manager class to manage connections for the
|
|
|
|
* Query method
|
|
|
|
*
|
|
|
|
* @package Query
|
2014-03-31 16:01:58 -04:00
|
|
|
* @subpackage Core
|
2014-03-31 12:47:07 -04:00
|
|
|
*/
|
|
|
|
final class Connection_Manager {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of named database connections
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $connections = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class instance variable
|
|
|
|
* @var Connection_Manager
|
|
|
|
*/
|
|
|
|
private static $instance = null;
|
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
/**
|
2014-03-31 16:01:58 -04:00
|
|
|
* Private constructor to prevent multiple instances
|
2014-04-01 14:54:38 -04:00
|
|
|
* @codeCoverageIgnore
|
2014-03-31 12:47:07 -04:00
|
|
|
*/
|
|
|
|
private function __construct() {}
|
2014-03-31 16:01:58 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private clone method to prevent cloning
|
2014-04-01 14:54:38 -04:00
|
|
|
* @codeCoverageIgnore
|
2014-03-31 16:01:58 -04:00
|
|
|
*/
|
2014-03-31 12:47:07 -04:00
|
|
|
private function __clone() {}
|
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
/**
|
2014-04-22 14:02:54 -04:00
|
|
|
* Make sure serialize/deserialize doesn't work
|
2014-04-01 14:54:38 -04:00
|
|
|
* @codeCoverageIgnore
|
2014-04-22 14:02:54 -04:00
|
|
|
* @throws \DomainException
|
2014-03-31 12:47:07 -04:00
|
|
|
*/
|
2014-03-31 16:20:14 -04:00
|
|
|
private function __wakeup()
|
2014-03-31 12:47:07 -04:00
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
throw new \DomainException("Can't unserialize singleton");
|
2014-03-31 12:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a connection manager instance
|
|
|
|
*
|
|
|
|
* @staticvar null $instance
|
|
|
|
* @return Connection_Manager
|
|
|
|
*/
|
|
|
|
public static function get_instance()
|
|
|
|
{
|
2014-04-01 14:54:38 -04:00
|
|
|
// @codeCoverageIgnoreStart
|
2014-03-31 12:47:07 -04:00
|
|
|
if (self::$instance === null)
|
|
|
|
{
|
|
|
|
self::$instance = new self();
|
|
|
|
}
|
2014-04-01 14:54:38 -04:00
|
|
|
// @codeCoverageIgnoreEnd
|
2014-03-31 12:47:07 -04:00
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the connection specified by the name given
|
|
|
|
*
|
2014-04-02 18:53:48 -04:00
|
|
|
* @param string|array|object $name
|
2014-03-31 12:47:07 -04:00
|
|
|
* @return Query_Builder
|
2014-04-22 14:02:54 -04:00
|
|
|
* @throws \InvalidArgumentException
|
2014-03-31 12:47:07 -04:00
|
|
|
*/
|
|
|
|
public function get_connection($name = '')
|
|
|
|
{
|
2014-04-22 14:02:54 -04:00
|
|
|
// If the parameter is a string, use it as an array index
|
2014-03-31 12:47:07 -04:00
|
|
|
if (is_scalar($name) && isset($this->connections[$name]))
|
|
|
|
{
|
|
|
|
return $this->connections[$name];
|
|
|
|
}
|
|
|
|
elseif (empty($name) && ! empty($this->connections)) // Otherwise, return the last one
|
|
|
|
{
|
|
|
|
return end($this->connections);
|
|
|
|
}
|
2014-04-24 14:50:53 -04:00
|
|
|
|
|
|
|
// You should actually connect before trying to get a connection...
|
|
|
|
throw new \InvalidArgumentException("The specified connection does not exist");
|
2014-03-31 12:47:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the passed parameters and return a connection
|
|
|
|
*
|
2014-06-09 17:02:14 -04:00
|
|
|
* @param \stdClass $params
|
2014-03-31 12:47:07 -04:00
|
|
|
* @return Query_Builder
|
|
|
|
*/
|
2014-06-09 17:02:14 -04:00
|
|
|
public function connect(\stdClass $params)
|
2014-03-31 12:47:07 -04:00
|
|
|
{
|
|
|
|
list($dsn, $dbtype, $params, $options) = $this->parse_params($params);
|
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
$driver = "\\Query\\Driver\\{$dbtype}";
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
// Create the database connection
|
|
|
|
$db = ( ! empty($params->user))
|
2014-04-02 17:08:50 -04:00
|
|
|
? new $driver($dsn, $params->user, $params->pass, $options)
|
|
|
|
: new $driver($dsn, '', '', $options);
|
2014-03-31 12:47:07 -04:00
|
|
|
|
|
|
|
// Set the table prefix, if it exists
|
|
|
|
if (isset($params->prefix))
|
|
|
|
{
|
|
|
|
$db->table_prefix = $params->prefix;
|
|
|
|
}
|
|
|
|
|
2014-04-23 15:53:16 -04:00
|
|
|
// Create Query Builder object
|
|
|
|
$conn = new Query_Builder($db, new Query_Parser($db));
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
|
|
|
|
// Save it for later
|
|
|
|
if (isset($params->alias))
|
|
|
|
{
|
|
|
|
$this->connections[$params->alias] = $conn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->connections[] = $conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses params into a dsn and option array
|
|
|
|
*
|
2014-06-09 17:02:14 -04:00
|
|
|
* @param \stdClass $params
|
2014-04-22 14:02:54 -04:00
|
|
|
* @return array
|
2014-03-31 12:47:07 -04:00
|
|
|
* @throws BadDBDriverException
|
|
|
|
*/
|
2014-06-09 17:02:14 -04:00
|
|
|
private function parse_params(\stdClass $params)
|
2014-04-03 13:28:30 -04:00
|
|
|
{
|
2014-03-31 12:47:07 -04:00
|
|
|
$params->type = strtolower($params->type);
|
|
|
|
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
|
|
|
|
|
|
|
|
// Make sure the class exists
|
2014-04-22 14:02:54 -04:00
|
|
|
if ( ! class_exists("Query\\Driver\\{$dbtype}"))
|
2014-03-31 12:47:07 -04:00
|
|
|
{
|
|
|
|
throw new BadDBDriverException('Database driver does not exist, or is not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set additional PDO options
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
if (isset($params->options))
|
|
|
|
{
|
|
|
|
$options = (array) $params->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the dsn for the database to connect to
|
|
|
|
$dsn = $this->create_dsn($dbtype, $params);
|
|
|
|
|
|
|
|
return array($dsn, $dbtype, $params, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the dsn from the db type and params
|
|
|
|
*
|
|
|
|
* @param string $dbtype
|
2014-06-09 17:02:14 -04:00
|
|
|
* @param \stdClass $params
|
2014-03-31 12:47:07 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-06-09 17:02:14 -04:00
|
|
|
private function create_dsn($dbtype, \stdClass $params)
|
2014-03-31 12:47:07 -04:00
|
|
|
{
|
|
|
|
if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}";
|
|
|
|
elseif ($dbtype === 'sqlite') $dsn = $params->file;
|
|
|
|
else
|
|
|
|
{
|
2014-04-01 15:41:38 -04:00
|
|
|
$dsn = strtolower($dbtype) . ':';
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
if ( ! empty($params->database))
|
|
|
|
{
|
|
|
|
$dsn .= "dbname={$params->database}";
|
|
|
|
}
|
|
|
|
|
|
|
|
$skip = array(
|
|
|
|
'name' => 'name',
|
|
|
|
'pass' => 'pass',
|
|
|
|
'user' => 'user',
|
|
|
|
'file' => 'file',
|
|
|
|
'type' => 'type',
|
|
|
|
'prefix' => 'prefix',
|
|
|
|
'options' => 'options',
|
2014-06-09 17:02:14 -04:00
|
|
|
'database' => 'database',
|
|
|
|
'alias' => 'alias'
|
2014-03-31 12:47:07 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach($params as $key => $val)
|
|
|
|
{
|
|
|
|
if ( ! isset($skip[$key]))
|
|
|
|
{
|
|
|
|
$dsn .= ";{$key}={$val}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dsn;
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 14:44:03 -04:00
|
|
|
// End of connection_manager.php
|