Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
e828b6c0
Commit
e828b6c0
authored
Feb 17, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
all database exceptions now include SQL and bindings in message for easier debugging.
parent
dd6402c6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
11 deletions
+64
-11
connection.php
laravel/database/connection.php
+23
-11
exception.php
laravel/database/exception.php
+41
-0
No files found.
laravel/database/connection.php
View file @
e828b6c0
...
...
@@ -208,23 +208,33 @@ class Connection {
$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 @@ class Connection {
*
* @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'
);
...
...
laravel/database/exception.php
0 → 100644
View file @
e828b6c0
<?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\n
SQL: "
.
$sql
.
"
\n\n
Bindings: "
.
var_export
(
$bindings
,
true
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment