diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index bf83d90..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: php - -php: - - 7.0 - -cache: - directories: - - $HOME/.composer/cache - -before_script: - - composer install - -script: - - php ./vendor/bin/phpspec run --format=pretty --stop-on-failure --no-code-generation - - diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a2b851..bacbd59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,13 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 --- -## [1.2.0] - 2019-10-01 +## [1.2.0] - 2020-04-30 ### Added -- SQL Query Monitor show at the Page how many SQL queries have been fired. +- Format SQL query - Measures the SQL execution time -- Testing framework [phpspec](https://www.phpspec.net/en/stable/manual/introduction.html) --- diff --git a/README.md b/README.md index e35db96..65c88de 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ Oxid eShop SQL Logger --------------------- -[![Build Status](https://travis-ci.org/TumTum/oxid-sql-logger.svg?branch=master)](https://travis-ci.org/TumTum/oxid-sql-logger) - Returns all SQL queries into console of a Browser. ## Install @@ -32,28 +30,6 @@ CLI: ![Example CLI](https://raw.githubusercontent.com/d3datadevelopment/oxid-sql-logger/master/img/screenshot-cli.jpg) -## SQL Query Status Monitor - -![Example CLI](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/sql-query-status-monitor.jpg) - -See how many queries and which types of queries have been added to the database. -To determine the amount. - -#### Switch on - -For this purpose, the parameter `$this->blSQLStatusBox = true;` must be stored in the file `config.inc.php`. -So you can turn it on and off temporarily. - -Unique: Insert, at the end, the Smarty tag: `[{tm_sql_status}]` in the `base.tpl` file. - -####### source/Application/views/flow/tpl/layout/base.tpl - -```html - [{tm_sql_status}] - - -``` - ## Credits Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration. diff --git a/composer.json b/composer.json index 4990a51..52d7e90 100644 --- a/composer.json +++ b/composer.json @@ -6,19 +6,11 @@ "monolog/monolog": "^1", "nilportugues/sql-query-formatter": "^1.2.2" }, - "require-dev": { - "phpspec/phpspec": "^4", - "doctrine/dbal": "^2.5", - "bovigo/assert": "^2" - }, "license": "GPL-3.0", "autoload": { "psr-4": { "D3\\OxidSqlLogger\\": "./src" - }, - "files": [ - "./src/functions.php" - ] + } }, "authors": [ { diff --git a/img/sql-query-status-monitor.jpg b/img/sql-query-status-monitor.jpg deleted file mode 100644 index 9a199e6..0000000 Binary files a/img/sql-query-status-monitor.jpg and /dev/null differ diff --git a/phpspec.yml b/phpspec.yml deleted file mode 100644 index 66f8981..0000000 --- a/phpspec.yml +++ /dev/null @@ -1,4 +0,0 @@ -suites: - default: - namespace: "tm\\oxid\\sql\\logger\\" - psr4_prefix: "tm\\oxid\\sql\\logger\\" diff --git a/spec/OxidSQLLoggerSpec.php b/spec/OxidSQLLoggerSpec.php deleted file mode 100644 index 1e8834d..0000000 --- a/spec/OxidSQLLoggerSpec.php +++ /dev/null @@ -1,89 +0,0 @@ -testLogger = new Monolog\Handler\TestHandler(); - Monolog\Registry::addLogger( - new Monolog\Logger('sql', [$this->testLogger]), - 'sql', - true - ); - } - - public function it_is_initializable() - { - $this->shouldHaveType(OxidSQLLogger::class); - } - - /** - * @throws FailureException - */ - public function it_should_log_the_sql_normally() - { - $this->startQuery('SELECT 1', ['param1'], ['master']); - for ($i = 0; $i <= 1000; $i++) @get_declared_classes(); - $this->stopQuery(); - - $this->assertExpectLog(); - $this->assertMatchMessage('/\[\d+ms\] SELECT 1/'); - - $this->assertContext('params', isSameAs(['param1'])); - $this->assertContext('types', isSameAs(['master'])); - $this->assertContext('time', isOfType('float')); - - } - - /** - * @throws FailureException - */ - public function it_should_log_without_time_specification() - { - $this->startQuery('SELECT 1'); - $this->startQuery('SELECT 2'); - - $this->assertExpectLog(); - $this->assertMatchMessage('/\[Statement canceled\] SELECT 1/'); - $this->assertContext('time', isSameAs('Statement canceled')); - - } - - private function assertExpectLog() - { - if (!$this->testLogger->hasRecords(Monolog\Logger::DEBUG)) { - throw new FailureException("No log entry was made"); - } - } - - private function assertMatchMessage($regex) - { - if (!$this->testLogger->hasRecordThatMatches($regex, Monolog\Logger::DEBUG)) { - $message = $this->testLogger->getRecords()[0]['message']; - throw new FailureException("Expect message '{$regex}' got '{$message}'"); - }; - } - - private function assertContext($param, $actual) - { - $record = $this->testLogger->getRecords()[0]; - - \bovigo\assert\assert($record['context'], hasKey($param), "Context '{$param}' not found"); - \bovigo\assert\assert($record['context'][$param], $actual, "Context '{$param} is not same"); - } -} diff --git a/spec/SQLQuerySpec.php b/spec/SQLQuerySpec.php deleted file mode 100644 index 5ef7009..0000000 --- a/spec/SQLQuerySpec.php +++ /dev/null @@ -1,65 +0,0 @@ -shouldHaveType(SQLQuery::class); - } - - public function it_hat_sql_setter_getter() - { - $this->setSQL('SELECT NOW()')->shouldHaveType(SQLQuery::class); - $this->getSQL()->shouldBe('SELECT NOW()'); - } - - public function it_hat_params_setter_getter() - { - $this->setParams(['param1', 'param2'])->shouldHaveType(SQLQuery::class); - $this->getParams()->shouldBe(['param1', 'param2']); - } - - public function it_hat_type_setter_getter() - { - $this->setTypes(['type1'])->shouldHaveType(SQLQuery::class); - $this->getTypes()->shouldBe(['type1']); - } - - public function it_should_indicate_an_aborted_time() - { - $this->setSql('SELECT 1'); - $this->setCanceled()->shouldHaveType(SQLQuery::class); - $this->getElapsedTime()->shouldBe('Statement canceled'); - } - - public function it_should_display_with_a_readable_time() - { - $this->setSql('SELECT 1'); - sleep(1); - $this->getReadableElapsedTime()->shouldMatch('/^1\.\d\d\ds/'); - } - - public function it_should_display_with_a_readable_time_in_ms() - { - $this->setSql('SELECT 1'); - for ($i = 0; $i <= 1000; $i++) @get_declared_classes(); - $this->getReadableElapsedTime()->shouldMatch('/^\d{1,3}ms/'); - } - - public function it_should_display_a_time_in_float() - { - $this->setSql('SELECT 1'); - for ($i = 0; $i <= 1000; $i++) @get_declared_classes(); - $this->getElapsedTime()->shouldBeFloat(); - } -} diff --git a/src/AutoInstallSmaryPlugin.php b/src/AutoInstallSmaryPlugin.php deleted file mode 100644 index 0e02a91..0000000 --- a/src/AutoInstallSmaryPlugin.php +++ /dev/null @@ -1,57 +0,0 @@ - - * Date: 2019-08-20 - * Time: 21:33 - */ - -namespace D3\OxidSqlLogger; - -/** - * Class AutoInstallSmaryPlugin - */ -class AutoInstallSmaryPlugin -{ - public function runInstall() - { - $oxideshop_ce = new \SplFileInfo(__DIR__ . '/../../../oxid-esales/oxideshop-ce/source/Core/Smarty/Plugin'); - $smartyPlugin = new \SplFileInfo(__DIR__ . '/Smarty/function.tm_sql_status.php'); - - if ($oxideshop_ce->isDir()) { - - $target = new \SplFileInfo($oxideshop_ce->getRealPath() . '/' . $smartyPlugin->getBasename()); - - if ($target->isFile() && $this->isSameFile($target, $smartyPlugin)) { - return; - } - - $this->createHardLink($smartyPlugin, $target); - - OxidUtilsView::clearSmarty(); - } - } - - /** - * @param \SplFileInfo $target - * @param \SplFileInfo $ - * @return bool - */ - protected function isSameFile(\SplFileInfo $target, \SplFileInfo $smartyPlugin) - { - return @md5_file($target->getPathname()) == @md5_file($smartyPlugin->getRealPath()); - } - - /** - * @param \SplFileInfo $smarty_func_tm_sql_status - * @param \SplFileInfo $target - */ - protected function createHardLink(\SplFileInfo $smartyPlugin, \SplFileInfo $target) - { - if ($target->isFile()) { - @unlink($target->getPathname()); - } - - link($smartyPlugin->getPathname(), $target->getPathname()); - } -} - diff --git a/src/OxidSQLLogger.php b/src/OxidSQLLogger.php index 6b068e1..b4d7bc1 100644 --- a/src/OxidSQLLogger.php +++ b/src/OxidSQLLogger.php @@ -9,6 +9,7 @@ namespace D3\OxidSqlLogger; use Doctrine\DBAL\Logging\SQLLogger; use Monolog; +use NilPortugues\Sql\QueryFormatter\Formatter; /** * Class OxidSQLLogger @@ -47,14 +48,12 @@ class OxidSQLLogger implements SQLLogger */ public function startQuery($sql, array $params = null, array $types = null) { - $formatter = new Formatter(); - if ($this->SQLQuery) { $this->SQLQuery->setCanceled(); $this->stopQuery(); } - $this->SQLQuery = (new SQLQuery()) ->setSql($formatter->format($sql)) + $this->SQLQuery = (new SQLQuery()) ->setSql($sql) ->setParams($params) ->setTypes($types) ->setLogStartingFile($this->logStartingFile) @@ -69,9 +68,12 @@ class OxidSQLLogger implements SQLLogger public function stopQuery() { if ($this->SQLQuery) { + $formatter = new Formatter(); + Monolog\Registry::sql()->addDebug( '['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ? $this->message : $this->SQLQuery->getSql() ), [ + 'query' => $formatter->format($this->SQLQuery->getSql()), 'params' => $this->SQLQuery->getParams(), 'time' => $this->SQLQuery->getElapsedTime(), 'types' => $this->SQLQuery->getTypes(), diff --git a/src/OxidUtilsView.php b/src/OxidUtilsView.php deleted file mode 100644 index 451e257..0000000 --- a/src/OxidUtilsView.php +++ /dev/null @@ -1,24 +0,0 @@ - - * Date: 2019-08-20 - * Time: 21:33 - */ - -namespace D3\OxidSqlLogger; - -/** - * Class OxidUtilsView - * @package tm\oxid\sql\logger - */ -class OxidUtilsView extends \OxidEsales\Eshop\Core\UtilsView -{ - - /** - * Removes existing Smarty instance - */ - public static function clearSmarty() - { - \OxidEsales\Eshop\Core\UtilsView::$_oSmarty = null; - } -} diff --git a/src/SQLQuery.php b/src/SQLQuery.php index 363dafd..0b61fb1 100644 --- a/src/SQLQuery.php +++ b/src/SQLQuery.php @@ -108,6 +108,14 @@ class SQLQuery return $this; } + /** + * @return string + */ + public function getLogStartingFile() + { + return $this->logStartingFile; + } + /** * @param $file * @return SQLQuery @@ -117,6 +125,14 @@ class SQLQuery $this->logStartingFile = $file; return $this; } + + /** + * @return string + */ + public function getLogStartingLine() + { + return $this->logStartingLine; + } /** * @param $line @@ -127,6 +143,14 @@ class SQLQuery $this->logStartingLine = $line; return $this; } + + /** + * @return string + */ + public function getLogStartingClass() + { + return $this->logStartingClass; + } /** * @param $classname @@ -137,6 +161,14 @@ class SQLQuery $this->logStartingClass = $classname; return $this; } + + /** + * @return string + */ + public function getLogStartingFunction() + { + return $this->logStartingFunction; + } /** * @param $functionname diff --git a/src/Smarty/function.tm_sql_status.php b/src/Smarty/function.tm_sql_status.php deleted file mode 100644 index 433bc98..0000000 --- a/src/Smarty/function.tm_sql_status.php +++ /dev/null @@ -1,55 +0,0 @@ -blSQLStatusBox = true -* ------------------------------------------------------------- -*/ -function smarty_function_tm_sql_status($aParams, &$smarty) -{ - $myConfig = \OxidEsales\Eshop\Core\Registry::getConfig(); - - // muss in config.inc.php gesetzt sein - - $box = $myConfig->getConfigParam('blSQLStatusBox'); - - if ($box == false) { - return ''; - } - - $db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); - $query = "SHOW STATUS WHERE Variable_name IN ( 'Com_select', 'Com_update', 'Com_insert', 'Com_delete' )"; - - $iSelects = $iDeletes = $iInserts = $iUpdates = 0; - $rows = $db->getAll($query); - foreach ($rows as $row) { - switch ($row['Variable_name']) { - case 'Com_select': - $iSelects = (int)$row['Value']; - break; - case 'Com_update': - $iUpdates = (int)$row['Value']; - break; - case 'Com_insert': - $iInserts = (int)$row['Value']; - break; - case 'Com_delete': - $iDeletes = (int)$row['Value']; - break; - default: - break; - } - } - - $iSum = $iSelects + $iDeletes + $iInserts + $iUpdates; - $sTable = ''; - $sTable .= ''; - $sTable .= ""; - $sTable .= '
All: $iSum SELECT: $iSelects UPDATE: $iUpdates INSERT: $iInserts DELETE: $iDeletes
'; - - return $sTable; -} diff --git a/src/functions.php b/src/functions.php index 4a9d318..0571331 100644 --- a/src/functions.php +++ b/src/functions.php @@ -12,5 +12,3 @@ function D3StartSQLLog($message = null) { function D3StopSQLLog() { \D3\OxidSqlLogger\OxidEsalesDatabase::disableLogger(); } - -// (new \D3\OxidSqlLogger\AutoInstallSmaryPlugin())->runInstall();