database.php 3.72 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 17 18 19 20 21 22
	 * The third-party driver registrar.
	 *
	 * @var array
	 */
	public static $registrar = array();

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

42
		if ( ! isset(static::$connections[$connection]))
Taylor Otwell committed
43
		{
44
			$config = Config::get("database.connections.{$connection}");
45 46

			if (is_null($config))
Taylor Otwell committed
47
			{
48
				throw new \Exception("Database connection is not defined for [$connection].");
Taylor Otwell committed
49 50
			}

51
			static::$connections[$connection] = new Connection(static::connect($config), $config);
Taylor Otwell committed
52 53
		}

54
		return static::$connections[$connection];
Taylor Otwell committed
55 56 57
	}

	/**
58 59 60 61 62
	 * Get a PDO database connection for a given database configuration.
	 *
	 * @param  array  $config
	 * @return PDO
	 */
63
	protected static function connect($config)
64
	{
Taylor Otwell committed
65 66 67 68 69 70 71
		return static::connector($config['driver'])->connect($config);
	}

	/**
	 * Create a new database connector instance.
	 *
	 * @param  string     $driver
Phill Sparks committed
72
	 * @return Database\Connectors\Connector
Taylor Otwell committed
73 74 75
	 */
	protected static function connector($driver)
	{
76 77
		if (isset(static::$registrar[$driver]))
		{
78 79 80
			$resolver = static::$registrar[$driver]['connector'];

			return $resolver();
81 82
		}

Taylor Otwell committed
83 84 85
		switch ($driver)
		{
			case 'sqlite':
86
				return new Database\Connectors\SQLite;
Taylor Otwell committed
87 88

			case 'mysql':
89
				return new Database\Connectors\MySQL;
Taylor Otwell committed
90 91

			case 'pgsql':
92 93 94 95
				return new Database\Connectors\Postgres;

			case 'sqlsrv':
				return new Database\Connectors\SQLServer;
Taylor Otwell committed
96 97

			default:
98
				throw new \Exception("Database driver [$driver] is not supported.");
Taylor Otwell committed
99
		}
100 101 102
	}

	/**
Taylor Otwell committed
103 104
	 * Begin a fluent query against a table.
	 *
105 106
	 * @param  string          $table
	 * @param  string          $connection
Phill Sparks committed
107
	 * @return Database\Query
Taylor Otwell committed
108
	 */
109
	public static function table($table, $connection = null)
Taylor Otwell committed
110
	{
111
		return static::connection($connection)->table($table);
Taylor Otwell committed
112 113 114
	}

	/**
115 116 117 118 119 120 121 122 123 124 125 126 127
	 * 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);
	}

	/**
128 129 130 131 132 133 134 135 136 137
	 * Get the profiling data for all queries.
	 *
	 * @return array
	 */
	public static function profile()
	{
		return Database\Connection::$queries;
	}

	/**
138 139 140 141 142 143 144 145
	 * Register a database connector and grammars.
	 *
	 * @param  string   $name
	 * @param  Closure  $connector
	 * @param  Closure  $query
	 * @param  Closure  $schema
	 * @return void
	 */
146
	public static function extend($name, Closure $connector, $query = null, $schema = null)
147 148 149 150 151 152 153
	{
		if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';

		static::$registrar[$name] = compact('connector', 'query', 'schema');
	}

	/**
Taylor Otwell committed
154
	 * Magic Method for calling methods on the default database connection.
Taylor Otwell committed
155
	 *
156 157 158 159 160 161 162
	 * <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
163
	 */
164
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
165
	{
166
		return call_user_func_array(array(static::connection(), $method), $parameters);
Taylor Otwell committed
167 168
	}

169
}