connection.php 8.09 KB
Newer Older
1 2 3
<?php namespace Laravel\Database;

use PDO, PDOStatement, Laravel\Config, Laravel\Event;
4

Taylor Otwell committed
5 6 7
class Connection {

	/**
Taylor Otwell committed
8
	 * The raw PDO connection instance.
9
	 *
Taylor Otwell committed
10
	 * @var PDO
11
	 */
Taylor Otwell committed
12
	public $pdo;
13 14

	/**
Taylor Otwell committed
15
	 * The connection configuration array.
Taylor Otwell committed
16
	 *
Taylor Otwell committed
17
	 * @var array
Taylor Otwell committed
18
	 */
19
	public $config;
Taylor Otwell committed
20 21

	/**
Taylor Otwell committed
22
	 * The query grammar instance for the connection.
23
	 *
24
	 * @var Query\Grammars\Grammar
25
	 */
Taylor Otwell committed
26
	protected $grammar;
27 28

	/**
29 30 31 32 33 34 35
	 * All of the queries that have been executed on all connections.
	 *
	 * @var array
	 */
	public static $queries = array();

	/**
36
	 * Create a new database connection instance.
Taylor Otwell committed
37
	 *
38 39
	 * @param  PDO    $pdo
	 * @param  array  $config
Taylor Otwell committed
40 41
	 * @return void
	 */
42
	public function __construct(PDO $pdo, $config)
Taylor Otwell committed
43 44
	{
		$this->pdo = $pdo;
45
		$this->config = $config;
Taylor Otwell committed
46
	}
Taylor Otwell committed
47 48

	/**
49 50
	 * Begin a fluent query against a table.
	 *
51 52 53 54 55 56 57 58
	 * <code>
	 *		// Start a fluent query against the "users" table
	 *		$query = DB::connection()->table('users');
	 *
	 *		// Start a fluent query against the "users" table and get all the users
	 *		$users = DB::connection()->table('users')->get();
	 * </code>
	 *
59 60 61 62 63 64 65 66 67 68 69
	 * @param  string  $table
	 * @return Query
	 */
	public function table($table)
	{
		return new Query($this, $this->grammar(), $table);
	}

	/**
	 * Create a new query grammar for the connection.
	 *
70
	 * @return Query\Grammars\Grammar
71 72 73 74 75
	 */
	protected function grammar()
	{
		if (isset($this->grammar)) return $this->grammar;

76 77
		if (isset(\Laravel\Database::$registrar[$this->driver()]))
		{
78 79
			$resolver = \Laravel\Database::$registrar[$this->driver()]['query'];
			return $this->grammar = $resolver($this);
80 81 82
		}

		switch ($this->driver())
83 84
		{
			case 'mysql':
85
				return $this->grammar = new Query\Grammars\MySQL($this);
86

Taylor Otwell committed
87 88 89
			case 'sqlite':
				return $this->grammar = new Query\Grammars\SQLite($this);

90
			case 'sqlsrv':
91
				return $this->grammar = new Query\Grammars\SQLServer($this);
92

93 94 95
			case 'pgsql':
				return $this->grammar = new Query\Grammars\Postgres($this);

96
			default:
97
				return $this->grammar = new Query\Grammars\Grammar($this);
98 99 100 101
		}
	}

	/**
102 103
	 * Execute a callback wrapped in a database transaction.
	 *
104
	 * @param  callback  $callback
105
	 * @return bool
106 107 108 109 110
	 */
	public function transaction($callback)
	{
		$this->pdo->beginTransaction();

111
		// After beginning the database transaction, we will call the callback
112 113 114 115 116 117 118 119 120 121 122 123 124
		// so that it can do its database work. If an exception occurs we'll
		// rollback the transaction and re-throw back to the developer.
		try
		{
			call_user_func($callback);
		}
		catch (\Exception $e)
		{
			$this->pdo->rollBack();

			throw $e;
		}

125
		return $this->pdo->commit();
126 127 128
	}

	/**
129
	 * Execute a SQL query against the connection and return a single column result.
Taylor Otwell committed
130
	 *
Taylor Otwell committed
131 132
	 * <code>
	 *		// Get the total number of rows on a table
133
	 *		$count = DB::connection()->only('select count(*) from users');
Taylor Otwell committed
134 135
	 *
	 *		// Get the sum of payment amounts from a table
136
	 *		$sum = DB::connection()->only('select sum(amount) from payments')
Taylor Otwell committed
137 138 139 140
	 * </code>
	 *
	 * @param  string  $sql
	 * @param  array   $bindings
141
	 * @return mixed
Taylor Otwell committed
142
	 */
143
	public function only($sql, $bindings = array())
Taylor Otwell committed
144
	{
145
		$results = (array) $this->first($sql, $bindings);
Taylor Otwell committed
146

147
		return reset($results);
Taylor Otwell committed
148 149 150 151 152
	}

	/**
	 * Execute a SQL query against the connection and return the first result.
	 *
Taylor Otwell committed
153 154 155 156 157 158 159 160
	 * <code>
	 *		// Execute a query against the database connection
	 *		$user = DB::connection()->first('select * from users');
	 *
	 *		// Execute a query with bound parameters
	 *		$user = DB::connection()->first('select * from users where id = ?', array($id));
	 * </code>
	 *
Taylor Otwell committed
161 162 163 164 165 166
	 * @param  string  $sql
	 * @param  array   $bindings
	 * @return object
	 */
	public function first($sql, $bindings = array())
	{
Taylor Otwell committed
167 168 169 170
		if (count($results = $this->query($sql, $bindings)) > 0)
		{
			return $results[0];
		}
Taylor Otwell committed
171 172 173
	}

	/**
174
	 * Execute a SQL query and return an array of StdClass objects.
Taylor Otwell committed
175
	 *
176 177 178 179 180 181
	 * @param  string  $sql
	 * @param  array   $bindings
	 * @return array
	 */
	public function query($sql, $bindings = array())
	{
182 183
		$sql = trim($sql);

184 185
		list($statement, $result) = $this->execute($sql, $bindings);

186 187
		// The result we return depends on the type of query executed against the
		// database. On SELECT clauses, we will return the result set, for update
Taylor Otwell committed
188
		// and deletes we will return the affected row count.
189
		if (stripos($sql, 'select') === 0 || stripos($sql, 'show') === 0)
190
		{
191
			return $this->fetch($statement, Config::get('database.fetch'));
192 193 194 195 196
		}
		elseif (stripos($sql, 'update') === 0 or stripos($sql, 'delete') === 0)
		{
			return $statement->rowCount();
		}
197
		// For insert statements that use the "returning" clause, which is allowed
SonicHedgehog committed
198
		// by database systems such as Postgres, we need to actually return the
199 200 201 202 203
		// real query result so the consumer can get the ID.
		elseif (stripos($sql, 'insert') === 0 and stripos($sql, 'returning') !== false)
		{
			return $this->fetch($statement, Config::get('database.fetch'));
		}
204 205 206 207
		else
		{
			return $result;
		}
208 209 210 211 212
	}

	/**
	 * Execute a SQL query against the connection.
	 *
Chris Berthe committed
213
	 * The PDO statement and boolean result will be returned in an array.
Taylor Otwell committed
214
	 *
Taylor Otwell committed
215 216
	 * @param  string  $sql
	 * @param  array   $bindings
217
	 * @return array
Taylor Otwell committed
218
	 */
219
	protected function execute($sql, $bindings = array())
Taylor Otwell committed
220
	{
Taylor Otwell committed
221 222
		$bindings = (array) $bindings;

223 224
		// Since expressions are injected into the query as strings, we need to
		// remove them from the array of bindings. After we have removed them,
Taylor Otwell committed
225 226
		// we'll reset the array so there are not gaps within the keys.
		$bindings = array_filter($bindings, function($binding)
227
		{
228
			return ! $binding instanceof Expression;
Taylor Otwell committed
229 230 231
		});

		$bindings = array_values($bindings);
232

233
		$sql = $this->grammar()->shortcut($sql, $bindings);
234

235 236 237 238 239 240 241 242 243 244 245 246 247
		// Next we need to translate all DateTime bindings to their date-time
		// strings that are compatible with the database. Each grammar may
		// define it's own date-time format according to its needs.
		$datetime = $this->grammar()->datetime;

		for ($i = 0; $i < count($bindings); $i++)
		{
			if ($bindings[$i] instanceof \DateTime)
			{
				$bindings[$i] = $bindings[$i]->format($datetime);
			}
		}

248 249 250 251 252 253
		// Each database operation is wrapped in a try / catch so we can wrap
		// any database exceptions in our custom exception class, which will
		// set the message to include the SQL and query bindings.
		try
		{
			$statement = $this->pdo->prepare($sql);
254

255
			$start = microtime(true);
256

257 258 259 260 261 262 263 264
			$result = $statement->execute($bindings);
		}
		// If an exception occurs, we'll pass it into our custom exception
		// and set the message to include the SQL and query bindings so
		// debugging is much easier on the developer.
		catch (\Exception $exception)
		{
			$exception = new Exception($sql, $bindings, $exception);
265

266 267
			throw $exception;
		}
268

Chris Berthe committed
269
		// Once we have executed the query, we log the SQL, bindings, and
270
		// execution time in a static array that is accessed by all of
271
		// the connections actively being used by the application.
272 273
		if (Config::get('database.profile'))
		{
274
			$this->log($sql, $bindings, $start);
275
		}
276 277

		return array($statement, $result);
Taylor Otwell committed
278
	}
Taylor Otwell committed
279

Taylor Otwell committed
280
	/**
281 282 283 284 285 286 287 288 289 290
	 * Fetch all of the rows for a given statement.
	 *
	 * @param  PDOStatement  $statement
	 * @param  int           $style
	 * @return array
	 */
	protected function fetch($statement, $style)
	{
		// If the fetch style is "class", we'll hydrate an array of PHP
		// stdClass objects as generic containers for the query rows,
Chris Berthe committed
291
		// otherwise we'll just use the fetch style value.
292 293 294 295 296 297 298 299 300 301 302
		if ($style === PDO::FETCH_CLASS)
		{
			return $statement->fetchAll(PDO::FETCH_CLASS, 'stdClass');
		}
		else
		{
			return $statement->fetchAll($style);
		}
	}

	/**
303 304 305 306
	 * Log the query and fire the core query event.
	 *
	 * @param  string  $sql
	 * @param  array   $bindings
307
	 * @param  int     $start
308 309
	 * @return void
	 */
310
	protected function log($sql, $bindings, $start)
311
	{
312 313
		$time = number_format((microtime(true) - $start) * 1000, 2);

314
		Event::fire('laravel.query', array($sql, $bindings, $time));
315

Taylor Otwell committed
316
		static::$queries[] = compact('sql', 'bindings', 'time');
317 318 319
	}

	/**
Taylor Otwell committed
320 321 322 323 324 325
	 * Get the driver name for the database connection.
	 *
	 * @return string
	 */
	public function driver()
	{
326
		return $this->config['driver'];
Taylor Otwell committed
327 328
	}

329 330 331 332 333 334 335 336
	/**
	 * Magic Method for dynamically beginning queries on database tables.
	 */
	public function __call($method, $parameters)
	{
		return $this->table($method);
	}

337
}