Source of file ConnectionManager.php

Size: 5,787 Bytes - Last Modified: 2015-11-10T10:01:22-05:00

../src/Query/ConnectionManager.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
Covered by 1 test(s):
  • Connection_Manager_Test::testNoClone
5657585960616263646566
Covered by 1 test(s):
  • Connection_Manager_Test::testNoSerialize
6768697071727374757677
Covered by 1 test(s):
  • Connection_Manager_Test::testNoUnserialize
78798081828384858687888990
Covered by 12 test(s):
  • MySQLQBTest::testInvalidConnectionName
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testInvalidConnectionName
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testQueryFunctionAlias
  • SQLiteQBTest::testInvalidConnectionName
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnectionException
  • FirebirdQBTest::testQueryFunctionAlias
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testInvalidConnectionName
  • FirebirdQBTest::testBadDriver
9192
Covered by 12 test(s):
  • MySQLQBTest::testInvalidConnectionName
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testInvalidConnectionName
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testQueryFunctionAlias
  • SQLiteQBTest::testInvalidConnectionName
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnectionException
  • FirebirdQBTest::testQueryFunctionAlias
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testInvalidConnectionName
  • FirebirdQBTest::testBadDriver
93949596979899100101102103104105106107
Covered by 10 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testInvalidConnectionName
  • PgSQLQBTest::testInvalidConnectionName
  • SQLiteQBTest::testQueryFunctionAlias
  • SQLiteQBTest::testInvalidConnectionName
  • FirebirdQBTest::testGetNamedConnectionException
  • FirebirdQBTest::testQueryFunctionAlias
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testInvalidConnectionName
108
Covered by 10 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testInvalidConnectionName
  • PgSQLQBTest::testInvalidConnectionName
  • SQLiteQBTest::testQueryFunctionAlias
  • SQLiteQBTest::testInvalidConnectionName
  • FirebirdQBTest::testGetNamedConnectionException
  • FirebirdQBTest::testQueryFunctionAlias
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testInvalidConnectionName
109
Covered by 3 test(s):
  • Connection_Manager_Test::testGetConnection
  • SQLiteQBTest::testQueryFunctionAlias
  • FirebirdQBTest::testGetNamedConnection
110111
Covered by 7 test(s):
  • Connection_Manager_Test::testConnect
  • MySQLQBTest::testInvalidConnectionName
  • PgSQLQBTest::testInvalidConnectionName
  • SQLiteQBTest::testInvalidConnectionName
  • FirebirdQBTest::testGetNamedConnectionException
  • FirebirdQBTest::testQueryFunctionAlias
  • FirebirdQBTest::testInvalidConnectionName
112113
Covered by 2 test(s):
  • Connection_Manager_Test::testConnect
  • FirebirdQBTest::testQueryFunctionAlias
114115116117
Covered by 5 test(s):
  • MySQLQBTest::testInvalidConnectionName
  • PgSQLQBTest::testInvalidConnectionName
  • SQLiteQBTest::testInvalidConnectionName
  • FirebirdQBTest::testGetNamedConnectionException
  • FirebirdQBTest::testInvalidConnectionName
118119120121122123124125126127128129130
Covered by 7 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testBadDriver
131132
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
133
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
134135136
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
137
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
138
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
139140141
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
142
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
143
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
144
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
145146147
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
148149150151
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
152
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
153
Covered by 2 test(s):
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
154
Covered by 2 test(s):
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
155156157
Covered by 1 test(s):
  • Connection_Manager_Test::testConnect
158159160
Covered by 3 test(s):
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
161162163164165166167168169170171172173174
Covered by 8 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testBadDriver
175
Covered by 8 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testBadDriver
176
Covered by 8 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testBadDriver
177178179
Covered by 8 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testBadDriver
180
Covered by 8 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testGetNamedConnection
  • FirebirdQBTest::testBadDriver
181
Covered by 4 test(s):
  • MySQLQBTest::testBadDriver
  • PgSQLQBTest::testBadDriver
  • SQLiteQBTest::testBadDriver
  • FirebirdQBTest::testBadDriver
182183184185
Covered by 4 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
186187
Covered by 4 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
188
Covered by 4 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
189
Covered by 3 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
190
Covered by 3 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
191192193
Covered by 4 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
194
Covered by 4 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
195
Covered by 1 test(s):
  • FirebirdQBTest::testGetNamedConnection
196
Covered by 1 test(s):
  • FirebirdQBTest::testGetNamedConnection
197
Covered by 3 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
198
Covered by 3 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
199
Covered by 3 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
200
Covered by 3 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
201202203204205206207
Covered by 4 test(s):
  • Connection_Manager_Test::testParseParams
  • Connection_Manager_Test::testConnect
  • Connection_Manager_Test::testGetConnection
  • FirebirdQBTest::testGetNamedConnection
208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
<?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
 */

// --------------------------------------------------------------------------

namespace Query;

/**
 * Connection manager class to manage connections for the
 * Query method
 *
 * @package Query
 * @subpackage Core
 */
final class ConnectionManager {

	/**
	 * Map of named database connections
	 * @var array
	 */
	private $connections = array();

	/**
	 * Class instance variable
	 * @var Connection_Manager
	 */
	private static $instance = null;

	// --------------------------------------------------------------------------

	/**
	 * Private constructor to prevent multiple instances
	 * @codeCoverageIgnore
	 */
	private function __construct() {}

	// --------------------------------------------------------------------------

	/**
	 * Private clone method to prevent cloning
	 * @throws \DomainException
	 */
	public function __clone()
	{
		throw new \DomainException("Can't clone singleton");
	}

	// --------------------------------------------------------------------------

	/**
	 * Prevent serialization of this object
	 * @throws \DomainException
	 */
	public function __sleep()
	{
		throw new \DomainException("No serializing of singleton");
	}

	// --------------------------------------------------------------------------

	/**
	 * Make sure serialize/deserialize doesn't work
	 * @throws \DomainException
	 */
	public function __wakeup()
	{
		throw new \DomainException("Can't unserialize singleton");
	}

	// --------------------------------------------------------------------------

	/**
	 * Return  a connection manager instance
	 *
	 * @staticvar null $instance
	 * @return Connection_Manager
	 */
	public static function get_instance()
	{
		if (self::$instance === null) self::$instance = new self();

		return self::$instance;
	}

	// --------------------------------------------------------------------------

	/**
	 * Returns the connection specified by the name given
	 *
	 * @param string|array|object $name
	 * @return Query_Builder
	 * @throws \InvalidArgumentException
	 */
	public function get_connection($name = '')
	{
		// If the parameter 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);
		}

		// You should actually connect before trying to get a connection...
		throw new \InvalidArgumentException("The specified connection does not exist");
	}

	// --------------------------------------------------------------------------

	/**
	 * Parse the passed parameters and return a connection
	 *
	 * @param \stdClass $params
	 * @return Query_Builder
	 */
	public function connect(\stdClass $params)
	{
		list($dsn, $dbtype, $params, $options) = $this->parse_params($params);

		$dbtype = ucfirst($dbtype);
		$driver = "\\Query\\Drivers\\{$dbtype}\\Driver";

		// Create the database connection
		$db = ( ! empty($params->user))
			? new $driver($dsn, $params->user, $params->pass, $options)
			: new $driver($dsn, '', '', $options);

		// Set the table prefix, if it exists
		if (isset($params->prefix))
		{
			$db->set_table_prefix($params->prefix);
		}

		// Create Query Builder object
		$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
	 *
	 * @param \stdClass $params
	 * @return array
	 * @throws BadDBDriverException
	 */
	public function parse_params(\stdClass $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 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
		if (strtolower($dbtype) === 'firebird')
		{
			$dsn = "{$params->host}:{$params->file}";
		}
		else if(strtolower($dbtype) === 'sqlite')
		{
			$dsn = $params->file;
		}
		else
		{
			$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 \stdClass $params
	 * @return string
	 */
	private function create_dsn($dbtype, \stdClass $params)
	{
		if (strtolower($dbtype) === 'pdo_firebird') $dbtype = 'firebird';

		$pairs = array();

		if ( ! empty($params->database))
		{
			$pairs[] = implode('=', array('dbname', $params->database));
		}

		$skip = array(
			'name' => 'name',
			'pass' => 'pass',
			'user' => 'user',
			'type' => 'type',
			'prefix' => 'prefix',
			'options' => 'options',
			'database' => 'database',
			'alias' => 'alias'
		);

		foreach($params as $key => $val)
		{
			if (( ! array_key_exists($key, $skip)) && ( ! empty($val)))
			{
				$pairs[] = implode('=', array($key, $val));
			}
		}

		return strtolower($dbtype) . ':' . implode(';', $pairs);
	}
}
// End of connection_manager.php