all database exceptions now include SQL and bindings in message for easier debugging.
This commit is contained in:
parent
dd6402c648
commit
e828b6c0c3
|
@ -208,23 +208,33 @@ protected function execute($sql, $bindings = array())
|
||||||
|
|
||||||
$sql = $this->grammar()->shortcut($sql, $bindings);
|
$sql = $this->grammar()->shortcut($sql, $bindings);
|
||||||
|
|
||||||
|
// 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);
|
$statement = $this->pdo->prepare($sql);
|
||||||
|
|
||||||
// Every query is timed so that we can log the executinon time along
|
$start = microtime(true);
|
||||||
// with the query SQL and array of bindings. This should be make it
|
|
||||||
// convenient for the developer to profile performance.
|
|
||||||
$time = microtime(true);
|
|
||||||
|
|
||||||
$result = $statement->execute($bindings);
|
$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);
|
||||||
|
|
||||||
$time = number_format((microtime(true) - $time) * 1000, 2);
|
throw $exception;
|
||||||
|
}
|
||||||
|
|
||||||
// Once we have execute the query, we log the SQL, bindings, and
|
// Once we have execute the query, we log the SQL, bindings, and
|
||||||
// execution time in a static array that is accessed by all of
|
// execution time in a static array that is accessed by all of
|
||||||
// the connections used by the application.
|
// the connections actively being used by the application.
|
||||||
if (Config::get('database.profile'))
|
if (Config::get('database.profile'))
|
||||||
{
|
{
|
||||||
$this->log($sql, $bindings, $time);
|
$this->log($sql, $bindings, $start);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array($statement, $result);
|
return array($statement, $result);
|
||||||
|
@ -235,11 +245,13 @@ protected function execute($sql, $bindings = array())
|
||||||
*
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @param int $time
|
* @param int $start
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function log($sql, $bindings, $time)
|
protected function log($sql, $bindings, $start)
|
||||||
{
|
{
|
||||||
|
$time = number_format((microtime(true) - $start) * 1000, 2);
|
||||||
|
|
||||||
Event::fire('laravel.query', array($sql, $bindings, $time));
|
Event::fire('laravel.query', array($sql, $bindings, $time));
|
||||||
|
|
||||||
static::$queries[] = compact('sql', 'bindings', 'time');
|
static::$queries[] = compact('sql', 'bindings', 'time');
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php namespace Laravel\Database;
|
||||||
|
|
||||||
|
class Exception extends \Exception {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The inner exception.
|
||||||
|
*
|
||||||
|
* @var Exception
|
||||||
|
*/
|
||||||
|
protected $inner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new database exception instance.
|
||||||
|
*
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $bindings
|
||||||
|
* @param Exception $inner
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($sql, $bindings, \Exception $inner)
|
||||||
|
{
|
||||||
|
$this->inner = $inner;
|
||||||
|
|
||||||
|
$this->setMessage($sql, $bindings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the exception message to include the SQL and bindings.
|
||||||
|
*
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $bindings
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function setMessage($sql, $bindings)
|
||||||
|
{
|
||||||
|
$this->message = $this->inner->getMessage();
|
||||||
|
|
||||||
|
$this->message .= "\n\nSQL: ".$sql."\n\nBindings: ".var_export($bindings, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue