Query/src/ConnectionManager.php

239 lines
5.0 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
*
2018-01-19 13:43:19 -05:00
* PHP version 7.1
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-19 13:43:19 -05:00
* @copyright 2012 - 2018 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
*/
2014-04-02 17:08:50 -04:00
namespace Query;
2016-10-12 20:32:23 -04:00
use DomainException;
/**
* Connection manager class to manage connections for the
* Query method
*/
2015-11-10 10:12:23 -05:00
final class ConnectionManager {
/**
* Map of named database connections
* @var array
*/
2016-09-07 13:10:03 -04:00
private $connections = [];
/**
* Class instance variable
* @var ConnectionManager|null
*/
private static $instance;
/**
2014-03-31 16:01:58 -04:00
* Private constructor to prevent multiple instances
* @codeCoverageIgnore
*/
2016-10-12 22:12:25 -04:00
private function __construct()
2016-10-12 20:32:23 -04:00
{
}
2014-03-31 16:01:58 -04:00
/**
* Private clone method to prevent cloning
2016-09-07 14:22:52 -04:00
*
2016-10-12 20:32:23 -04:00
* @throws DomainException
2016-09-07 14:22:52 -04:00
* @return void
2014-03-31 16:01:58 -04:00
*/
public function __clone()
{
2016-10-12 20:32:23 -04:00
throw new DomainException("Can't clone singleton");
}
/**
* Prevent serialization of this object
2016-09-07 14:22:52 -04:00
*
2016-10-12 20:32:23 -04:00
* @throws DomainException
2016-09-07 14:22:52 -04:00
* @return void
*/
public function __sleep()
{
throw new DomainException('No serializing of singleton');
}
/**
2014-04-22 14:02:54 -04:00
* Make sure serialize/deserialize doesn't work
2016-09-07 14:22:52 -04:00
*
2016-10-12 20:32:23 -04:00
* @throws DomainException
2016-09-07 14:22:52 -04:00
* @return void
*/
public function __wakeup()
{
2016-10-12 20:32:23 -04:00
throw new DomainException("Can't unserialize singleton");
}
/**
* Return a connection manager instance
*
* @staticvar null $instance
2015-11-10 20:58:32 -05:00
* @return ConnectionManager
*/
2016-10-13 21:55:23 -04:00
public static function getInstance(): ConnectionManager
{
2016-09-07 13:10:03 -04:00
if (self::$instance === NULL)
2015-11-11 09:25:21 -05:00
{
self::$instance = new self();
}
return self::$instance;
}
/**
* Returns the connection specified by the name given
*
* @param string|array|object $name
2016-10-13 21:55:23 -04:00
* @return QueryBuilderInterface
* @throws Exception\NonExistentConnectionException
*/
2016-10-13 21:55:23 -04:00
public function getConnection($name = ''): QueryBuilderInterface
{
2014-04-22 14:02:54 -04:00
// If the parameter is a string, use it as an array index
if (is_scalar($name) && isset($this->connections[$name]))
{
return $this->connections[$name];
}
2018-01-23 13:49:51 -05:00
else if (empty($name) && ! empty($this->connections)) // Otherwise, return the last one
{
return end($this->connections);
}
// You should actually connect before trying to get a connection...
throw new Exception\NonExistentConnectionException('The specified connection does not exist');
}
/**
* Parse the passed parameters and return a connection
*
2018-01-26 08:39:30 -05:00
* @param object|array $params
* @throws Exception\BadDBDriverException
2016-10-13 21:55:23 -04:00
* @return QueryBuilderInterface
*/
2018-01-26 08:39:30 -05:00
public function connect($params): QueryBuilderInterface
{
2018-01-26 08:39:30 -05:00
[$dsn, $dbtype, $params, $options] = $this->parseParams($params);
$dbtype = ucfirst($dbtype);
$driver = "\\Query\\Drivers\\{$dbtype}\\Driver";
2014-04-02 17:08:50 -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);
// Set the table prefix, if it exists
if (isset($params->prefix))
{
2016-10-13 21:55:23 -04:00
$db->setTablePrefix($params->prefix);
}
// Create Query Builder object
2015-11-10 10:12:23 -05:00
$conn = new QueryBuilder($db, new QueryParser($db));
// 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
*
2015-07-17 16:01:41 -04:00
* @param \stdClass $params
2018-01-26 08:39:30 -05:00
* @return object|array
* @throws Exception\BadDBDriverException
*/
2018-01-26 08:39:30 -05:00
public function parseParams($params): array
{
2018-01-26 08:39:30 -05:00
$params = (object) $params;
$params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
$dbtype = ucfirst($dbtype);
// Make sure the class exists
if ( ! class_exists("\\Query\\Drivers\\{$dbtype}\\Driver"))
{
throw new Exception\BadDBDriverException('Database driver does not exist, or is not supported');
}
// Set additional PDO options
2016-09-07 13:10:03 -04:00
$options = [];
if (isset($params->options))
{
$options = (array) $params->options;
}
// Create the dsn for the database to connect to
if(strtolower($dbtype) === 'sqlite')
2015-07-17 16:01:41 -04:00
{
$dsn = $params->file;
}
else
{
2016-10-13 21:55:23 -04:00
$dsn = $this->createDsn($dbtype, $params);
}
2016-09-07 13:10:03 -04:00
return [$dsn, $dbtype, $params, $options];
}
/**
* Create the dsn from the db type and params
*
2018-01-24 13:14:03 -05:00
* @codeCoverageIgnore
* @param string $dbtype
2018-01-26 08:39:30 -05:00
* @param array|object $params
* @return string
*/
2018-01-26 08:39:30 -05:00
private function createDsn(string $dbtype, $params): string
{
2016-09-07 13:10:03 -04:00
$pairs = [];
2015-07-17 16:01:41 -04:00
if ( ! empty($params->database))
{
2016-09-07 13:10:03 -04:00
$pairs[] = implode('=', ['dbname', $params->database]);
2015-07-17 16:01:41 -04:00
}
2016-09-07 13:10:03 -04:00
$skip = [
2015-07-17 16:01:41 -04:00
'name' => 'name',
'pass' => 'pass',
'user' => 'user',
'type' => 'type',
'prefix' => 'prefix',
'options' => 'options',
'database' => 'database',
'alias' => 'alias'
2016-09-07 13:10:03 -04:00
];
2015-07-17 16:01:41 -04:00
foreach($params as $key => $val)
{
if (( ! array_key_exists($key, $skip)) && ! empty($val))
{
2016-09-07 13:10:03 -04:00
$pairs[] = implode('=', [$key, $val]);
}
}
2015-07-20 15:24:21 -04:00
return strtolower($dbtype) . ':' . implode(';', $pairs);
}
2016-10-13 21:55:23 -04:00
}