mirror of
https://github.com/processwire/processwire.git
synced 2025-08-16 03:34:33 +02:00
Update WireDatabasePDOStatement class so that you can optionally use it even when not in debug mode
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
* #pw-summary All database operations in ProcessWire are performed via this PDO-style database class.
|
* #pw-summary All database operations in ProcessWire are performed via this PDO-style database class.
|
||||||
*
|
*
|
||||||
* @method void unknownColumnError($column) #pw-internal
|
* @method void unknownColumnError($column) #pw-internal
|
||||||
|
* @property bool $debugMode
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class WireDatabasePDO extends Wire implements WireDatabase {
|
class WireDatabasePDO extends Wire implements WireDatabase {
|
||||||
@@ -494,9 +495,12 @@ class WireDatabasePDO extends Wire implements WireDatabase {
|
|||||||
* #pw-group-PDO
|
* #pw-group-PDO
|
||||||
*
|
*
|
||||||
* @param string $statement
|
* @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
|
* @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
|
* @link http://php.net/manual/en/pdo.prepare.php
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -504,6 +508,10 @@ class WireDatabasePDO extends Wire implements WireDatabase {
|
|||||||
if(is_string($driver_options)) {
|
if(is_string($driver_options)) {
|
||||||
$note = $driver_options;
|
$note = $driver_options;
|
||||||
$driver_options = array();
|
$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);
|
$pdoStatement = $this->pdo()->prepare($statement, $driver_options);
|
||||||
if($this->debugMode) {
|
if($this->debugMode) {
|
||||||
@@ -985,7 +993,8 @@ class WireDatabasePDO extends Wire implements WireDatabase {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function __get($key) {
|
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);
|
return parent::__get($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -57,6 +57,14 @@ class WireDatabasePDOStatement extends \PDOStatement {
|
|||||||
*/
|
*/
|
||||||
protected $debugNote = '';
|
protected $debugNote = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Debug mode?
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected $debugMode = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct
|
* Construct
|
||||||
*
|
*
|
||||||
@@ -67,6 +75,7 @@ class WireDatabasePDOStatement extends \PDOStatement {
|
|||||||
*/
|
*/
|
||||||
protected function __construct(WireDatabasePDO $database) {
|
protected function __construct(WireDatabasePDO $database) {
|
||||||
$this->database = $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) {
|
public function bindValue($parameter, $value, $data_type = \PDO::PARAM_STR) {
|
||||||
$result = parent::bindValue($parameter, $value, $data_type);
|
$result = parent::bindValue($parameter, $value, $data_type);
|
||||||
if(strpos($parameter, ':') === 0) {
|
if($this->debugMode && strpos($parameter, ':') === 0) {
|
||||||
$this->setDebugParam($parameter, $value, $data_type);
|
$this->setDebugParam($parameter, $value, $data_type);
|
||||||
} else {
|
} else {
|
||||||
// note we do not handle index/question-mark parameters for debugging
|
// note we do not handle index/question-mark parameters for debugging
|
||||||
@@ -133,6 +142,22 @@ class WireDatabasePDOStatement extends \PDOStatement {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function execute($input_parameters = NULL) {
|
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 executeDebug($input_parameters = NULL) {
|
||||||
|
|
||||||
$timer = Debug::startTimer();
|
$timer = Debug::startTimer();
|
||||||
$exception = null;
|
$exception = null;
|
||||||
|
Reference in New Issue
Block a user