Append query ID and IP address as a /* comment */ to every query, if enabled in debug settings

This commit is contained in:
Kijin Sung 2021-01-13 22:14:15 +09:00
parent fcc3db3a7a
commit 4805a1864b
7 changed files with 47 additions and 1 deletions

View file

@ -42,6 +42,7 @@ class DB
protected $_query_id = '';
protected $_errno = 0;
protected $_errstr = '';
protected $_debug_comment = false;
/**
* Transaction level.
@ -127,6 +128,9 @@ class DB
// Get the DB version.
$this->db_version = $this->_handle->getAttribute(\PDO::ATTR_SERVER_VERSION);
// Cache the debug comment setting.
$this->_debug_comment = config('debug.query_comment') ? true : false;
}
/**
@ -154,6 +158,12 @@ class DB
// Add table prefixes to the query string.
$statement = $this->addPrefixes($statement);
// Add the debug comment.
if ($this->_debug_comment)
{
$statement .= "\n" . sprintf('/* prepare() %s */', \RX_CLIENT_IP);
}
// Create and return a prepared statement.
$this->_last_stmt = $this->_handle->prepare($statement, $driver_options);
return $this->_last_stmt;
@ -185,6 +195,12 @@ class DB
// Add table prefixes to the query string.
$query_string = $this->addPrefixes($query_string);
// Add the debug comment.
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* query() %s */', \RX_CLIENT_IP);
}
// Execute either a prepared statement or a regular query depending on whether there are arguments.
if (count($args))
{
@ -315,6 +331,11 @@ class DB
// Prepare and execute the main query.
try
{
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* %s %s */', $query_id, \RX_CLIENT_IP);
}
$this->_query_id = $query_id;
if (count($query_params))
{
@ -395,6 +416,11 @@ class DB
// Prepare and execute the query.
try
{
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* %s %s */', $query_id, \RX_CLIENT_IP);
}
if (count($query_params))
{
$this->_last_stmt = $this->_handle->prepare($query_string);
@ -457,6 +483,11 @@ class DB
*/
public function _query($query_string)
{
if ($this->_debug_comment)
{
$query_string .= "\n" . sprintf('/* _query() %s */', \RX_CLIENT_IP);
}
$this->_last_stmt = $this->_handle->query($query_string);
return $this->_last_stmt;
}
@ -1084,7 +1115,7 @@ class DB
// Compose the basic structure of the log entry.
$result = array(
'query' => $query,
'query' => preg_replace('!\n/\* .+ \*/$!s', '', $query),
'query_id' => $this->_query_id,
'connection' => $this->_type,
'elapsed_time' => sprintf('%0.5f', $elapsed_time),