1: <?php namespace Laravel;
2:
3: use Closure;
4: use Laravel\Database\Expression;
5: use Laravel\Database\Connection;
6:
7: class Database {
8:
9: /**
10: * The established database connections.
11: *
12: * @var array
13: */
14: public static $connections = array();
15:
16: /**
17: * The third-party driver registrar.
18: *
19: * @var array
20: */
21: public static $registrar = array();
22:
23: /**
24: * Get a database connection.
25: *
26: * If no database name is specified, the default connection will be returned.
27: *
28: * <code>
29: * // Get the default database connection for the application
30: * $connection = DB::connection();
31: *
32: * // Get a specific connection by passing the connection name
33: * $connection = DB::connection('mysql');
34: * </code>
35: *
36: * @param string $connection
37: * @return Database\Connection
38: */
39: public static function connection($connection = null)
40: {
41: if (is_null($connection)) $connection = Config::get('database.default');
42:
43: if ( ! isset(static::$connections[$connection]))
44: {
45: $config = Config::get("database.connections.{$connection}");
46:
47: if (is_null($config))
48: {
49: throw new \Exception("Database connection is not defined for [$connection].");
50: }
51:
52: static::$connections[$connection] = new Connection(static::connect($config), $config);
53: }
54:
55: return static::$connections[$connection];
56: }
57:
58: /**
59: * Get a PDO database connection for a given database configuration.
60: *
61: * @param array $config
62: * @return PDO
63: */
64: protected static function connect($config)
65: {
66: return static::connector($config['driver'])->connect($config);
67: }
68:
69: /**
70: * Create a new database connector instance.
71: *
72: * @param string $driver
73: * @return Database\Connectors\Connector
74: */
75: protected static function connector($driver)
76: {
77: if (isset(static::$registrar[$driver]))
78: {
79: $resolver = static::$registrar[$driver]['connector'];
80:
81: return $resolver();
82: }
83:
84: switch ($driver)
85: {
86: case 'sqlite':
87: return new Database\Connectors\SQLite;
88:
89: case 'mysql':
90: return new Database\Connectors\MySQL;
91:
92: case 'pgsql':
93: return new Database\Connectors\Postgres;
94:
95: case 'sqlsrv':
96: return new Database\Connectors\SQLServer;
97:
98: default:
99: throw new \Exception("Database driver [$driver] is not supported.");
100: }
101: }
102:
103: /**
104: * Begin a fluent query against a table.
105: *
106: * @param string $table
107: * @param string $connection
108: * @return Database\Query
109: */
110: public static function table($table, $connection = null)
111: {
112: return static::connection($connection)->table($table);
113: }
114:
115: /**
116: * Create a new database expression instance.
117: *
118: * Database expressions are used to inject raw SQL into a fluent query.
119: *
120: * @param string $value
121: * @return Expression
122: */
123: public static function raw($value)
124: {
125: return new Expression($value);
126: }
127:
128: /**
129: * Escape a string for usage in a query.
130: *
131: * This uses the correct quoting mechanism for the default database connection.
132: *
133: * @param string $value
134: * @return string
135: */
136: public static function escape($value)
137: {
138: return static::connection()->pdo->quote($value);
139: }
140:
141: /**
142: * Get the profiling data for all queries.
143: *
144: * @return array
145: */
146: public static function profile()
147: {
148: return Database\Connection::$queries;
149: }
150:
151: /**
152: * Get the last query that was executed.
153: *
154: * Returns false if no queries have been executed yet.
155: *
156: * @return string
157: */
158: public static function last_query()
159: {
160: return end(Database\Connection::$queries);
161: }
162:
163: /**
164: * Register a database connector and grammars.
165: *
166: * @param string $name
167: * @param Closure $connector
168: * @param Closure $query
169: * @param Closure $schema
170: * @return void
171: */
172: public static function extend($name, Closure $connector, $query = null, $schema = null)
173: {
174: if (is_null($query)) $query = '\Laravel\Database\Query\Grammars\Grammar';
175:
176: static::$registrar[$name] = compact('connector', 'query', 'schema');
177: }
178:
179: /**
180: * Magic Method for calling methods on the default database connection.
181: *
182: * <code>
183: * // Get the driver name for the default database connection
184: * $driver = DB::driver();
185: *
186: * // Execute a fluent query on the default database connection
187: * $users = DB::table('users')->get();
188: * </code>
189: */
190: public static function __callStatic($method, $parameters)
191: {
192: return call_user_func_array(array(static::connection(), $method), $parameters);
193: }
194:
195: }