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;
|
|
|
|
|
|
|
|
use \Query\Driver;
|
|
|
|
|
2014-03-31 12:47:07 -04:00
|
|
|
/**
|
|
|
|
* Generic exception for bad drivers
|
|
|
|
*
|
|
|
|
* @package Query
|
2014-03-31 16:01:58 -04:00
|
|
|
* @subpackage Core
|
2014-03-31 12:47:07 -04:00
|
|
|
*/
|
2014-04-02 17:08:50 -04:00
|
|
|
class BadDBDriverException extends \InvalidArgumentException {}
|
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
|
|
|
/**
|
|
|
|
* Make sure serialize/deseriaze doesn't work
|
2014-04-01 14:54:38 -04:00
|
|
|
* @codeCoverageIgnore
|
2014-03-31 12:47:07 -04:00
|
|
|
* @throws DomainException
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @param mixed $name
|
|
|
|
* @return Query_Builder
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function get_connection($name = '')
|
|
|
|
{
|
|
|
|
// If the paramater is a string, use it as an array index
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param array|object $params
|
|
|
|
* @return Query_Builder
|
|
|
|
* @throws BadConnectionException
|
|
|
|
*/
|
|
|
|
public function connect($params)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Save connection
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Set the table prefix, if it exists
|
|
|
|
if (isset($params->prefix))
|
|
|
|
{
|
|
|
|
$db->table_prefix = $params->prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Query Builder object
|
2014-04-02 11:52:55 -04:00
|
|
|
$conn = new Query_Builder($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
|
|
|
|
*
|
|
|
|
* @param ArrayObject $params
|
|
|
|
* @throws BadDBDriverException
|
|
|
|
*/
|
|
|
|
private function parse_params($params)
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// Parse argument array / object
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Convert array to object
|
2014-04-02 17:08:50 -04:00
|
|
|
$params = new \ArrayObject($params, \ArrayObject::STD_PROP_LIST | \ArrayObject::ARRAY_AS_PROPS);
|
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-02 17:08:50 -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
|
|
|
|
* @param array|object $params
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function create_dsn($dbtype, $params)
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
'database' => 'database'
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach($params as $key => $val)
|
|
|
|
{
|
|
|
|
if ( ! isset($skip[$key]))
|
|
|
|
{
|
|
|
|
$dsn .= ";{$key}={$val}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dsn;
|
|
|
|
}
|
|
|
|
}
|