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

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

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

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

	/**
17 18 19 20 21 22 23
	 * The third-party driver registrar.
	 *
	 * @var array
	 */
	public static $registrar = array();

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

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

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

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

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

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

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

			return $resolver();
82 83
		}

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

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

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

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

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

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

	/**
116 117 118 119 120 121 122 123 124 125 126
	 * 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);
	}
Franz Liedke committed
127 128 129 130 131 132 133 134 135 136 137 138 139
	
	/**
	 * 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);
	}
140 141

	/**
142 143 144 145 146 147 148 149
	 * Get the profiling data for all queries.
	 *
	 * @return array
	 */
	public static function profile()
	{
		return Database\Connection::$queries;
	}
150 151 152 153 154 155 156 157 158 159
	
	/**
	 * Get the last query that was executed.
	 *
	 * Returns false if no queries have been executed yet.
	 *
	 * @return string
	 */
	public static function last_query()
	{
Taylor Otwell committed
160
		return end(Database\Connection::$queries);
161
	}
162 163

	/**
164 165 166 167 168 169 170 171
	 * Register a database connector and grammars.
	 *
	 * @param  string   $name
	 * @param  Closure  $connector
	 * @param  Closure  $query
	 * @param  Closure  $schema
	 * @return void
	 */
172
	public static function extend($name, Closure $connector, $query = null, $schema = null)
173 174 175 176 177 178 179
	{
		if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';

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

	/**
Taylor Otwell committed
180
	 * Magic Method for calling methods on the default database connection.
Taylor Otwell committed
181
	 *
182 183 184 185 186 187 188
	 * <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
189
	 */
190
	public static function __callStatic($method, $parameters)
Taylor Otwell committed
191
	{
192
		return call_user_func_array(array(static::connection(), $method), $parameters);
Taylor Otwell committed
193 194
	}

195
}