2012-01-16 13:59:24 -06:00
|
|
|
<?php namespace Laravel;
|
2011-08-01 17:58:20 -05:00
|
|
|
|
2012-07-31 09:55:45 +02:00
|
|
|
use Closure;
|
2012-01-16 13:59:24 -06:00
|
|
|
use Laravel\Database\Expression;
|
|
|
|
|
use Laravel\Database\Connection;
|
2011-09-17 23:46:24 -05:00
|
|
|
|
2012-01-16 13:59:24 -06:00
|
|
|
class Database {
|
2011-08-01 17:58:20 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The established database connections.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2012-01-16 13:59:24 -06:00
|
|
|
public static $connections = array();
|
2011-08-01 17:58:20 -05:00
|
|
|
|
2012-04-30 22:40:18 -05:00
|
|
|
/**
|
|
|
|
|
* The third-party driver registrar.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public static $registrar = array();
|
|
|
|
|
|
2011-08-01 17:58:20 -05:00
|
|
|
/**
|
2011-09-16 19:59:20 -05:00
|
|
|
* Get a database connection.
|
2011-09-12 23:14:24 -05:00
|
|
|
*
|
2011-09-16 19:59:20 -05:00
|
|
|
* If no database name is specified, the default connection will be returned.
|
2011-08-01 17:58:20 -05:00
|
|
|
*
|
2011-09-27 21:10:32 -05:00
|
|
|
* <code>
|
|
|
|
|
* // Get the default database connection for the application
|
|
|
|
|
* $connection = DB::connection();
|
|
|
|
|
*
|
|
|
|
|
* // Get a specific connection by passing the connection name
|
|
|
|
|
* $connection = DB::connection('mysql');
|
|
|
|
|
* </code>
|
|
|
|
|
*
|
2011-09-15 23:03:47 -05:00
|
|
|
* @param string $connection
|
2012-02-04 21:30:52 +00:00
|
|
|
* @return Database\Connection
|
2011-08-01 17:58:20 -05:00
|
|
|
*/
|
2011-09-20 23:14:09 -05:00
|
|
|
public static function connection($connection = null)
|
2011-08-01 17:58:20 -05:00
|
|
|
{
|
2011-09-20 23:14:09 -05:00
|
|
|
if (is_null($connection)) $connection = Config::get('database.default');
|
2011-08-01 17:58:20 -05:00
|
|
|
|
2011-11-11 21:27:30 -06:00
|
|
|
if ( ! isset(static::$connections[$connection]))
|
2011-08-01 17:58:20 -05:00
|
|
|
{
|
2011-09-20 23:14:09 -05:00
|
|
|
$config = Config::get("database.connections.{$connection}");
|
2011-09-17 23:46:24 -05:00
|
|
|
|
|
|
|
|
if (is_null($config))
|
2011-08-01 17:58:20 -05:00
|
|
|
{
|
2012-01-16 13:59:24 -06:00
|
|
|
throw new \Exception("Database connection is not defined for [$connection].");
|
2011-08-01 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-20 23:14:09 -05:00
|
|
|
static::$connections[$connection] = new Connection(static::connect($config), $config);
|
2011-08-01 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-20 23:14:09 -05:00
|
|
|
return static::$connections[$connection];
|
2011-08-01 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-09-17 23:46:24 -05:00
|
|
|
/**
|
|
|
|
|
* Get a PDO database connection for a given database configuration.
|
|
|
|
|
*
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @return PDO
|
|
|
|
|
*/
|
2011-09-20 23:14:09 -05:00
|
|
|
protected static function connect($config)
|
2011-09-17 23:46:24 -05:00
|
|
|
{
|
2011-11-11 23:15:17 -06:00
|
|
|
return static::connector($config['driver'])->connect($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new database connector instance.
|
|
|
|
|
*
|
|
|
|
|
* @param string $driver
|
2012-02-04 21:30:52 +00:00
|
|
|
* @return Database\Connectors\Connector
|
2011-11-11 23:15:17 -06:00
|
|
|
*/
|
|
|
|
|
protected static function connector($driver)
|
|
|
|
|
{
|
2012-04-30 22:40:18 -05:00
|
|
|
if (isset(static::$registrar[$driver]))
|
|
|
|
|
{
|
2012-05-02 16:52:55 -05:00
|
|
|
$resolver = static::$registrar[$driver]['connector'];
|
|
|
|
|
|
|
|
|
|
return $resolver();
|
2012-04-30 22:40:18 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-11 23:15:17 -06:00
|
|
|
switch ($driver)
|
|
|
|
|
{
|
|
|
|
|
case 'sqlite':
|
2012-01-16 13:59:24 -06:00
|
|
|
return new Database\Connectors\SQLite;
|
2011-11-11 23:15:17 -06:00
|
|
|
|
|
|
|
|
case 'mysql':
|
2012-01-16 13:59:24 -06:00
|
|
|
return new Database\Connectors\MySQL;
|
2011-11-11 23:15:17 -06:00
|
|
|
|
|
|
|
|
case 'pgsql':
|
2012-01-16 13:59:24 -06:00
|
|
|
return new Database\Connectors\Postgres;
|
|
|
|
|
|
|
|
|
|
case 'sqlsrv':
|
|
|
|
|
return new Database\Connectors\SQLServer;
|
2011-11-11 23:15:17 -06:00
|
|
|
|
|
|
|
|
default:
|
2012-01-16 13:59:24 -06:00
|
|
|
throw new \Exception("Database driver [$driver] is not supported.");
|
2011-11-11 23:15:17 -06:00
|
|
|
}
|
2011-09-17 23:46:24 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-01 17:58:20 -05:00
|
|
|
/**
|
|
|
|
|
* Begin a fluent query against a table.
|
|
|
|
|
*
|
2011-09-02 19:36:19 -05:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $connection
|
2012-02-04 21:30:52 +00:00
|
|
|
* @return Database\Query
|
2011-08-01 17:58:20 -05:00
|
|
|
*/
|
2011-09-20 23:14:09 -05:00
|
|
|
public static function table($table, $connection = null)
|
2011-08-01 17:58:20 -05:00
|
|
|
{
|
2011-09-20 23:14:09 -05:00
|
|
|
return static::connection($connection)->table($table);
|
2011-08-01 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-10-12 19:55:44 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new database expression instance.
|
|
|
|
|
*
|
|
|
|
|
* Database expressions are used to inject raw SQL into a fluent query.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return Expression
|
|
|
|
|
*/
|
|
|
|
|
public static function raw($value)
|
|
|
|
|
{
|
|
|
|
|
return new Expression($value);
|
|
|
|
|
}
|
2012-10-05 14:38:13 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Escape a string for usage in a query.
|
|
|
|
|
*
|
|
|
|
|
* This uses the correct quoting mechanism for the default database connection.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function escape($value)
|
|
|
|
|
{
|
|
|
|
|
return static::connection()->pdo->quote($value);
|
|
|
|
|
}
|
2011-10-12 19:55:44 -05:00
|
|
|
|
2012-01-16 16:39:54 -06:00
|
|
|
/**
|
|
|
|
|
* Get the profiling data for all queries.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function profile()
|
|
|
|
|
{
|
|
|
|
|
return Database\Connection::$queries;
|
|
|
|
|
}
|
2012-05-30 18:44:48 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the last query that was executed.
|
|
|
|
|
*
|
|
|
|
|
* Returns false if no queries have been executed yet.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function last_query()
|
|
|
|
|
{
|
2012-06-03 17:44:53 -05:00
|
|
|
return end(Database\Connection::$queries);
|
2012-05-30 18:44:48 +03:00
|
|
|
}
|
2012-01-16 16:39:54 -06:00
|
|
|
|
2012-04-30 22:40:18 -05:00
|
|
|
/**
|
|
|
|
|
* Register a database connector and grammars.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param Closure $connector
|
|
|
|
|
* @param Closure $query
|
|
|
|
|
* @param Closure $schema
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2012-04-30 22:43:30 -05:00
|
|
|
public static function extend($name, Closure $connector, $query = null, $schema = null)
|
2012-04-30 22:40:18 -05:00
|
|
|
{
|
|
|
|
|
if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';
|
|
|
|
|
|
|
|
|
|
static::$registrar[$name] = compact('connector', 'query', 'schema');
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-01 17:58:20 -05:00
|
|
|
/**
|
|
|
|
|
* Magic Method for calling methods on the default database connection.
|
2011-08-19 20:12:39 -05:00
|
|
|
*
|
2012-01-16 13:59:24 -06:00
|
|
|
* <code>
|
|
|
|
|
* // Get the driver name for the default database connection
|
|
|
|
|
* $driver = DB::driver();
|
|
|
|
|
*
|
|
|
|
|
* // Execute a fluent query on the default database connection
|
|
|
|
|
* $users = DB::table('users')->get();
|
|
|
|
|
* </code>
|
2011-08-01 17:58:20 -05:00
|
|
|
*/
|
2011-09-20 23:14:09 -05:00
|
|
|
public static function __callStatic($method, $parameters)
|
2011-08-01 17:58:20 -05:00
|
|
|
{
|
2011-09-20 23:14:09 -05:00
|
|
|
return call_user_func_array(array(static::connection(), $method), $parameters);
|
2011-08-01 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
2011-11-23 08:35:47 -06:00
|
|
|
}
|