From 8a3b941b88339d7415036169e2142ad913988701 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Mon, 6 May 2024 15:41:36 +0200 Subject: [PATCH] add tests --- core/Db/Schema/Mariadb.php | 1 + tests/PHPUnit/Unit/DbHelperTest.php | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/core/Db/Schema/Mariadb.php b/core/Db/Schema/Mariadb.php index 8b5b8b87f6fc..0c93c6678437 100644 --- a/core/Db/Schema/Mariadb.php +++ b/core/Db/Schema/Mariadb.php @@ -28,6 +28,7 @@ public function addMaxExecutionTimeHintToQuery(string $sql, float $limit): strin } $sql = trim($sql); + $pos = stripos($sql, 'SELECT'); $isMaxExecutionTimeoutAlreadyPresent = (stripos($sql, 'max_statement_time=') !== false); if ($pos !== false && !$isMaxExecutionTimeoutAlreadyPresent) { $maxExecutionTimeHint = 'SET STATEMENT max_statement_time=' . ceil($limit) . ' FOR '; diff --git a/tests/PHPUnit/Unit/DbHelperTest.php b/tests/PHPUnit/Unit/DbHelperTest.php index b4bfd44c631d..7013bf650276 100644 --- a/tests/PHPUnit/Unit/DbHelperTest.php +++ b/tests/PHPUnit/Unit/DbHelperTest.php @@ -9,6 +9,8 @@ namespace Piwik\Tests\Unit; +use Piwik\Config; +use Piwik\Db\Schema; use Piwik\DbHelper; /** @@ -62,8 +64,10 @@ public function getVariousDbNames() /** * @dataProvider getTestQueries */ - public function testAddMaxExecutionTimeHintToQuery($expected, $query, $timeLimit) + public function testAddMaxExecutionTimeHintToQuery($expected, $query, $timeLimit, $schema) { + Schema::unsetInstance(); + Config::getInstance()->database['schema'] = $schema; $result = DbHelper::addMaxExecutionTimeHintToQuery($query, $timeLimit); $this->assertEquals($expected, $result); } @@ -71,11 +75,18 @@ public function testAddMaxExecutionTimeHintToQuery($expected, $query, $timeLimit public function getTestQueries() { return [ - ['SELECT /*+ MAX_EXECUTION_TIME(1500) */ * FROM table', 'SELECT * FROM table', 1.5], - ['SELECT /*+ MAX_EXECUTION_TIME(20000) */ column FROM (SELECT * FROM table)', 'SELECT column FROM (SELECT * FROM table)', 20], - ['SELECT * FROM table', 'SELECT * FROM table', 0], - ['SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM table', 'SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM table', 3.5], // should not append/change MAX_EXECUTION_TIME hint if already present - ['UPDATE table SET column = value', 'UPDATE table SET column = value', 150], + // MySql Schema + ['SELECT /*+ MAX_EXECUTION_TIME(1500) */ * FROM table', 'SELECT * FROM table', 1.5, 'Mysql'], + ['SELECT /*+ MAX_EXECUTION_TIME(20000) */ column FROM (SELECT * FROM table)', 'SELECT column FROM (SELECT * FROM table)', 20, 'Mysql'], + ['SELECT * FROM table', 'SELECT * FROM table', 0, 'Mysql'], + ['SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM table', 'SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM table', 3.5, 'Mysql'], // should not append/change MAX_EXECUTION_TIME hint if already present + ['UPDATE table SET column = value', 'UPDATE table SET column = value', 150, 'Mysql'], + // MariaDB Schema + ['SET STATEMENT max_statement_time=2 FOR SELECT * FROM table', 'SELECT * FROM table', 1.5, 'Mariadb'], + ['SET STATEMENT max_statement_time=20 FOR SELECT column FROM (SELECT * FROM table)', 'SELECT column FROM (SELECT * FROM table)', 20, 'Mariadb'], + ['SELECT * FROM table', 'SELECT * FROM table', 0, 'Mariadb'], + ['SET STATEMENT max_statement_time=2 FOR SELECT * FROM table', 'SET STATEMENT max_statement_time=2 FOR SELECT * FROM table', 3.5, 'Mariadb'], // should not append/change MAX_EXECUTION_TIME hint if already present + ['UPDATE table SET column = value', 'UPDATE table SET column = value', 150, 'Mariadb'], ]; } }