Files
spa-api/system/db.php

58 lines
1.4 KiB
PHP
Raw Normal View History

<?php namespace System;
2011-08-01 17:58:20 -05:00
class DB {
2011-08-01 17:58:20 -05:00
/**
* The established database connections.
*
* @var array
*/
public static $connections = array();
/**
* Get a database connection. If no database name is specified, the default
* connection will be returned as defined in the db configuration file.
*
* Note: Database connections are managed as singletons.
*
* @param string $connection
* @return DB\Connection
2011-08-01 17:58:20 -05:00
*/
public static function connection($connection = null)
{
if (is_null($connection)) $connection = Config::get('db.default');
2011-08-01 17:58:20 -05:00
if ( ! array_key_exists($connection, static::$connections))
{
2011-08-02 13:49:17 -05:00
if (is_null($config = Config::get('db.connections.'.$connection)))
2011-08-01 17:58:20 -05:00
{
throw new \Exception("Database connection [$connection] is not defined.");
}
static::$connections[$connection] = new DB\Connection($connection, (object) $config, new DB\Connector);
2011-08-01 17:58:20 -05:00
}
return static::$connections[$connection];
}
/**
* Begin a fluent query against a table.
*
* @param string $table
* @param string $connection
* @return DB\Query
2011-08-01 17:58:20 -05:00
*/
public static function table($table, $connection = null)
{
return static::connection($connection)->table($table);
}
/**
* Magic Method for calling methods on the default database connection.
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::connection(), $method), $parameters);
}
}