Skip to content

Commit

Permalink
Option to allow devs to include the values used in the SQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
b-hayes committed May 12, 2023
1 parent b7ca8ba commit 12f625c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/PicoDb/StatementHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class StatementHandler
*/
protected $logQueries = false;

/**
* Flag to combine values in the logged SQL queries.
*
* @access protected
* @var boolean
*/
protected $logQueryValues = false;

/**
* Run explain command on each query
*
Expand Down Expand Up @@ -125,10 +133,12 @@ public function __construct(Database $db)
* Enable query logging
*
* @access public
* @param bool $includeValues
* @return $this
*/
public function withLogging()
public function withLogging(bool $includeValues = false)
{
$this->logQueryValues = $includeValues;
$this->logQueries = true;
return $this;
}
Expand Down Expand Up @@ -229,6 +239,7 @@ public function getNbQueries()
*
* @access public
* @return PDOStatement|false
* @throws SQLException
*/
public function execute()
{
Expand Down Expand Up @@ -285,7 +296,13 @@ protected function bindParams(PDOStatement $pdoStatement)
protected function beforeExecute()
{
if ($this->logQueries) {
$this->db->setLogMessage($this->sql);
$sql = $this->sql;
if ($this->logQueryValues) {
foreach ($this->lobParams as $value) {
$sql = substr_replace($sql, "'$value'", strpos($sql, '?'), 1);
}
}
$this->db->setLogMessage($sql);
}

if ($this->stopwatch) {
Expand Down Expand Up @@ -335,7 +352,6 @@ protected function cleanup()
*
* @access public
* @param PDOException $e
* @return bool
* @throws SQLException
*/
public function handleSqlError(PDOException $e)
Expand Down

0 comments on commit 12f625c

Please sign in to comment.