Enable Query() function as a alias function

This commit is contained in:
Timothy Warren 2012-11-08 20:02:31 -05:00
parent ccabf8048b
commit 5a626629d7
3 changed files with 48 additions and 2 deletions

View File

@ -89,14 +89,34 @@ function db_filter($array, $index)
return $new_array; return $new_array;
} }
// --------------------------------------------------------------------------
/** /**
* Connection function * Connection function
* *
* @param mixed $params * @param mixed $params
* @return Query_Builder * @return Query_Builder
*/ */
function Query($params) function Query($params = '')
{ {
static $connections;
// If there's existing connection(s) just return it
if ( ! empty($connections))
{
// If the paramater is a string, use it as an array index
if (is_scalar($params) && isset($connections[$params]))
{
return $connections[$params];
}
elseif (empty($params) && isset($connections[0]))
{
return end($connections);
}
}
// --------------------------------------------------------------------------
// Convert array to object // Convert array to object
if (is_array($params)) if (is_array($params))
{ {
@ -122,6 +142,8 @@ function Query($params)
{ {
throw new BadDBDriverException('Database driver does not exist, or is not supported'); throw new BadDBDriverException('Database driver does not exist, or is not supported');
} }
// --------------------------------------------------------------------------
// Create the dsn for the database to connect to // Create the dsn for the database to connect to
switch($dbtype) switch($dbtype)
@ -161,15 +183,30 @@ function Query($params)
{ {
throw new BadConnectionException('Connection failed, invalid arguments', 2); throw new BadConnectionException('Connection failed, invalid arguments', 2);
} }
// --------------------------------------------------------------------------
// Set the table prefix, if it exists // Set the table prefix, if it exists
if (isset($params->prefix)) if (isset($params->prefix))
{ {
$db->table_prefix = $params->prefix; $db->table_prefix = $params->prefix;
} }
// Create the database connection
$conn = new Query_Builder($db, $params);
// Save it for later
if (isset($params->alias))
{
$connections[$params->alias] =& $conn;
}
else
{
$connections[] =& $conn;
}
// Return the Query Builder object // Return the Query Builder object
return new Query_Builder($db, $params); return $conn;
} }
// End of common.php // End of common.php

View File

@ -21,6 +21,15 @@ abstract class QBTest extends UnitTestCase {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Get Tests // ! Get Tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestQueryFunctionAlias()
{
if (empty($this->db)) return;
$db = Query();
$this->assertReference($this->db, $db);
}
public function TestGet() public function TestGet()
{ {

Binary file not shown.