Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed May 6, 2024
1 parent a3a1322 commit 8a3b941
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/Db/Schema/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ';
Expand Down
23 changes: 17 additions & 6 deletions tests/PHPUnit/Unit/DbHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Piwik\Tests\Unit;

use Piwik\Config;
use Piwik\Db\Schema;
use Piwik\DbHelper;

/**
Expand Down Expand Up @@ -62,20 +64,29 @@ 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);
}

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'],
];
}
}

0 comments on commit 8a3b941

Please sign in to comment.