From 640b66a32472e5d67d88290ce1c7383b31707d7a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 5 Oct 2014 20:25:11 +0200 Subject: [PATCH] Added list of SQL statement to debug output --- data/config.ini | 1 + src/Dispatcher.php | 9 ++++++++- src/PDOEx.php | 7 +++++++ tests/DispatcherTest.php | 6 ++++-- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/data/config.ini b/data/config.ini index f3111783..d652ddc2 100644 --- a/data/config.ini +++ b/data/config.ini @@ -76,3 +76,4 @@ postsPerPage = 40 [misc] thumbnailCropStyle = outside customFaviconUrl = /favicon.png +dumpSqlIntoQueries = 0 diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 0073c161..c7b64689 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -4,12 +4,14 @@ namespace Szurubooru; final class Dispatcher { private $router; + private $config; private $databaseConnection; private $authService; private $tokenService; public function __construct( \Szurubooru\Router $router, + \Szurubooru\Config $config, \Szurubooru\DatabaseConnection $databaseConnection, \Szurubooru\Helpers\HttpHelper $httpHelper, \Szurubooru\Services\AuthService $authService, @@ -17,6 +19,7 @@ final class Dispatcher \Szurubooru\ControllerRepository $controllerRepository) { $this->router = $router; + $this->config = $config; $this->databaseConnection = $databaseConnection; $this->httpHelper = $httpHelper; @@ -50,7 +53,11 @@ final class Dispatcher } $end = microtime(true); $json['__time'] = $end - \Szurubooru\Bootstrap::getStartTime(); - $json['__queries'] = $this->databaseConnection->getPDO()->getQueryCount(); + if ($this->config->misc->dumpSqlIntoQueries) + { + $json['__queries'] = $this->databaseConnection->getPDO()->getQueryCount(); + $json['__statements'] = $this->databaseConnection->getPDO()->getStatements(); + } $this->httpHelper->setResponseCode($code); $this->httpHelper->setHeader('Content-Type', 'application/json'); diff --git a/src/PDOEx.php b/src/PDOEx.php index 8146c6e8..3730e161 100644 --- a/src/PDOEx.php +++ b/src/PDOEx.php @@ -4,10 +4,12 @@ namespace Szurubooru; class PDOEx extends \PDO { private $queryCount = 0; + private $statements = []; public function prepare($statement, $driverOptions = []) { ++ $this->queryCount; + $this->statements[] = $statement; return parent::prepare($statement, $driverOptions); } @@ -15,4 +17,9 @@ class PDOEx extends \PDO { return $this->queryCount; } + + public function getStatements() + { + return $this->statements; + } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index ab1a6a4a..29e09315 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -4,6 +4,7 @@ namespace Szurubooru\Tests; final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { private $routerMock; + private $configMock; private $httpHelperMock; private $authServiceMock; private $tokenServiceMock; @@ -13,10 +14,12 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { parent::setUp(); $this->routerMock = $this->mock(\Szurubooru\Router::class); + $this->configMock = $this->mockConfig(); $this->httpHelperMock = $this->mock(\Szurubooru\Helpers\HttpHelper::class); $this->authServiceMock = $this->mock(\Szurubooru\Services\AuthService::class); $this->tokenServiceMock = $this->mock(\Szurubooru\Services\TokenService::class); $this->controllerRepositoryMock = $this->mock(\Szurubooru\ControllerRepository::class); + $this->configMock->set('misc/dumpSqlIntoQueries', 0); } public function testDispatchingArrays() @@ -34,7 +37,6 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $actual = $dispatcher->run('GET', '/'); unset($actual['__time']); - unset($actual['__queries']); $this->assertEquals($expected, $actual); } @@ -51,7 +53,6 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $actual = $dispatcher->run('GET', '/'); unset($actual['__time']); - unset($actual['__queries']); $this->assertEquals($expected, $actual); } @@ -69,6 +70,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { return new \Szurubooru\Dispatcher( $this->routerMock, + $this->configMock, $this->databaseConnection, $this->httpHelperMock, $this->authServiceMock,