1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +02:00

Update WireDatabasePDOStatement class so that you can optionally use it even when not in debug mode

This commit is contained in:
Ryan Cramer
2020-07-10 12:45:54 -04:00
parent 8e0f2ed721
commit 6f4c21d5b9
2 changed files with 39 additions and 5 deletions

View File

@@ -17,6 +17,7 @@
* #pw-summary All database operations in ProcessWire are performed via this PDO-style database class.
*
* @method void unknownColumnError($column) #pw-internal
* @property bool $debugMode
*
*/
class WireDatabasePDO extends Wire implements WireDatabase {
@@ -494,9 +495,12 @@ class WireDatabasePDO extends Wire implements WireDatabase {
* #pw-group-PDO
*
* @param string $statement
* @param array|string $driver_options Driver options array or you may specify $note here
* @param array|string|bool $driver_options Optionally specify one of the following:
* - Boolean true for WireDatabasePDOStatement rather than PDOStatement (also assumed when debug mode is on) 3.0.162+
* - Driver options array
* - or you may specify the $note argument here
* @param string $note Debug notes to save with query in debug mode
* @return \PDOStatement
* @return \PDOStatement|WireDatabasePDOStatement
* @link http://php.net/manual/en/pdo.prepare.php
*
*/
@@ -504,6 +508,10 @@ class WireDatabasePDO extends Wire implements WireDatabase {
if(is_string($driver_options)) {
$note = $driver_options;
$driver_options = array();
} else if($driver_options === true) {
$driver_options = array(
\PDO::ATTR_STATEMENT_CLASS => array(__NAMESPACE__ . "\\WireDatabasePDOStatement", array($this))
);
}
$pdoStatement = $this->pdo()->prepare($statement, $driver_options);
if($this->debugMode) {
@@ -985,7 +993,8 @@ class WireDatabasePDO extends Wire implements WireDatabase {
*
*/
public function __get($key) {
if($key == 'pdo') return $this->pdo();
if($key === 'pdo') return $this->pdo();
if($key === 'debugMode') return $this->debugMode;
return parent::__get($key);
}

View File

@@ -57,6 +57,14 @@ class WireDatabasePDOStatement extends \PDOStatement {
*/
protected $debugNote = '';
/**
* Debug mode?
*
* @var bool
*
*/
protected $debugMode = false;
/**
* Construct
*
@@ -67,6 +75,7 @@ class WireDatabasePDOStatement extends \PDOStatement {
*/
protected function __construct(WireDatabasePDO $database) {
$this->database = $database;
$this->debugMode = $database->debugMode;
}
/**
@@ -116,7 +125,7 @@ class WireDatabasePDOStatement extends \PDOStatement {
*/
public function bindValue($parameter, $value, $data_type = \PDO::PARAM_STR) {
$result = parent::bindValue($parameter, $value, $data_type);
if(strpos($parameter, ':') === 0) {
if($this->debugMode && strpos($parameter, ':') === 0) {
$this->setDebugParam($parameter, $value, $data_type);
} else {
// note we do not handle index/question-mark parameters for debugging
@@ -126,13 +135,29 @@ class WireDatabasePDOStatement extends \PDOStatement {
/**
* Execute prepared statement
*
* @param array|null $input_parameters
* @return bool
* @throws \PDOException
*
*/
public function execute($input_parameters = NULL) {
if($this->debugMode) {
return $this->executeDebug($input_parameters);
} else {
return parent::execute($input_parameters);
}
}
/**
* Execute prepared statement when in debug mode only
*
* @param array|null $input_parameters
* @return bool
* @throws \PDOException
*
*/
public function execute($input_parameters = NULL) {
public function executeDebug($input_parameters = NULL) {
$timer = Debug::startTimer();
$exception = null;