database.php 3.06 KB
Newer Older
1
<?php namespace Laravel;
Taylor Otwell committed
2

3 4
use Laravel\Database\Expression;
use Laravel\Database\Connection;
5

6
class Database {
Taylor Otwell committed
7 8 9 10 11 12

	/**
	 * The established database connections.
	 *
	 * @var array
	 */
13
	public static $connections = array();
Taylor Otwell committed
14 15

	/**
16
	 * Get a database connection.
17
	 *
18
	 * If no database name is specified, the default connection will be returned.
Taylor Otwell committed
19
	 *
Taylor Otwell committed
20 21 22 23 24 25 26 27
	 * <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>
	 *
Taylor Otwell committed
28
	 * @param  string      $connection
Phill Sparks committed
29
	 * @return Database\Connection
Taylor Otwell committed
30
	 */
31
	public static function connection($connection = null)
Taylor Otwell committed
32
	{
33
		if (is_null($connection)) $connection = Config::get('database.default');
Taylor Otwell committed
34

35
		if ( ! isset(static::$connections[$connection]))
Taylor Otwell committed
36
		{
37
			$config = Config::get("database.connections.{$connection}");
38 39

			if (is_null($config))
Taylor Otwell committed
40
			{
41
				throw new \Exception("Database connection is not defined for [$connection].");
Taylor Otwell committed
42 43
			}

44
			static::$connections[$connection] = new Connection(static::connect($config), $config);
Taylor Otwell committed
45 46
		}

47
		return static::$connections[$connection];
Taylor Otwell committed
48 49 50
	}

	/**
51 52 53 54 55
	 * Get a PDO database connection for a given database configuration.
	 *
	 * @param  array  $config
	 * @return PDO
	 */
56
	protected static function connect($config)
57
	{
Taylor Otwell committed
58 59 60 61 62 63 64
		return static::connector($config['driver'])->connect($config);
	}

	/**
	 * Create a new database connector instance.
	 *
	 * @param  string     $driver
Phill Sparks committed
65
	 * @return Database\Connectors\Connector
Taylor Otwell committed
66 67 68 69 70 71
	 */
	protected static function connector($driver)
	{
		switch ($driver)
		{
			case 'sqlite':
72
				return new Database\Connectors\SQLite;
Taylor Otwell committed
73 74

			case 'mysql':
75
				return new Database\Connectors\MySQL;
Taylor Otwell committed
76 77

			case 'pgsql':
78 79 80 81
				return new Database\Connectors\Postgres;

			case 'sqlsrv':
				return new Database\Connectors\SQLServer;
Taylor Otwell committed
82 83

			default:
84
				throw new \Exception("Database driver [$driver] is not supported.");
Taylor Otwell committed
85
		}
86 87 88
	}

	/**
Taylor Otwell committed
89 90
	 * Begin a fluent query against a table.
	 *
91 92
	 * @param  string          $table
	 * @param  string          $connection
Phill Sparks committed
93
	 * @return Database\Query
Taylor Otwell committed
94
	 */
95
	public static function table($table, $connection = null)
Taylor Otwell committed
96
	{
97
		return static::connection($connection)->table($table);
Taylor Otwell committed
98 99 100
	}

	/**
101 102 103 104 105 106 107 108 109 110 111 112 113
	 * 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);
	}

	/**
114 115 116 117 118 119 120 121 122 123
	 * Get the profiling data for all queries.
	 *
	 * @return array
	 */
	public static function profile()
	{
		return Database\Connection::$queries;
	}

	/**
Taylor Otwell committed
124
	 * Magic Method for calling methods on the default database connection.
Taylor Otwell committed
125
	 *
126 127 128 129 130 131 132
	 * <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>
Taylor Otwell committed
133
	 */
134
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
135
	{
136
		return call_user_func_array(array(static::connection(), $method), $parameters);
Taylor Otwell committed
137 138
	}

139
}