From e828b6c0c38ef86543130953048cd77cc8fc8ab8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 16 Feb 2012 22:32:09 -0600 Subject: [PATCH] all database exceptions now include SQL and bindings in message for easier debugging. --- laravel/database/connection.php | 34 ++++++++++++++++++--------- laravel/database/exception.php | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 laravel/database/exception.php diff --git a/laravel/database/connection.php b/laravel/database/connection.php index f39934ab..9e6b5323 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -208,23 +208,33 @@ protected function execute($sql, $bindings = array()) $sql = $this->grammar()->shortcut($sql, $bindings); - $statement = $this->pdo->prepare($sql); + // 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); - // Every query is timed so that we can log the executinon time along - // with the query SQL and array of bindings. This should be make it - // convenient for the developer to profile performance. - $time = microtime(true); + $start = 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 // 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')) { - $this->log($sql, $bindings, $time); + $this->log($sql, $bindings, $start); } return array($statement, $result); @@ -235,11 +245,13 @@ protected function execute($sql, $bindings = array()) * * @param string $sql * @param array $bindings - * @param int $time + * @param int $start * @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)); static::$queries[] = compact('sql', 'bindings', 'time'); diff --git a/laravel/database/exception.php b/laravel/database/exception.php new file mode 100644 index 00000000..a116ad70 --- /dev/null +++ b/laravel/database/exception.php @@ -0,0 +1,41 @@ +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); + } + +} \ No newline at end of file