diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2019-06-26 05:45:22 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2019-06-26 05:45:22 +0300 |
commit | acb892f91c6d0b4b0b7c106269f4b9a2485c318d (patch) | |
tree | cab1658fc858bf657955d494315aa428cfe80015 | |
parent | b107e19aa77e6c6869f7f22d0cd3e3b69712c651 (diff) |
Fix error number is detected wrongly in tracker mode (#14571)
* Fix error number is detected wrongly
Eg when the error is `Error query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry`, it would detect 2300 instead of 1062.
fix DEV-1683
* Update Mysqli.php
same behaviour as for regular DB
* reuse same code
-rw-r--r-- | core/Db/Adapter/Mysqli.php | 16 | ||||
-rw-r--r-- | core/Db/Adapter/Pdo/Mysql.php | 13 | ||||
-rw-r--r-- | core/Tracker/Db/Mysqli.php | 9 | ||||
-rw-r--r-- | core/Tracker/Db/Pdo/Mysql.php | 5 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Db/Adapter/MysqliTest.php | 27 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Db/Adapter/Pdo/MysqlTest.php | 26 |
6 files changed, 82 insertions, 14 deletions
diff --git a/core/Db/Adapter/Mysqli.php b/core/Db/Adapter/Mysqli.php index f8fe9a0d3c..9c4a8abfea 100644 --- a/core/Db/Adapter/Mysqli.php +++ b/core/Db/Adapter/Mysqli.php @@ -185,14 +185,26 @@ class Mysqli extends Zend_Db_Adapter_Mysqli implements AdapterInterface */ public function isErrNo($e, $errno) { - if (is_null($this->_connection)) { + return self::isMysqliErrorNumber($e, $this->_connection, $errno); + } + + /** + * Test error number + * + * @param Exception $e + * @param string $errno + * @return bool + */ + public static function isMysqliErrorNumber($e, $connection, $errno) + { + if (is_null($connection)) { if (preg_match('/(?:\[|\s)([0-9]{4})(?:\]|\s)/', $e->getMessage(), $match)) { return $match[1] == $errno; } return mysqli_connect_errno() == $errno; } - return mysqli_errno($this->_connection) == $errno; + return mysqli_errno($connection) == $errno; } /** diff --git a/core/Db/Adapter/Pdo/Mysql.php b/core/Db/Adapter/Pdo/Mysql.php index b45d718c5a..d4025e2a1d 100644 --- a/core/Db/Adapter/Pdo/Mysql.php +++ b/core/Db/Adapter/Pdo/Mysql.php @@ -209,6 +209,19 @@ class Mysql extends Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface */ public function isErrNo($e, $errno) { + return self::isPdoErrorNumber($e, $errno); + } + + + /** + * Test error number + * + * @param Exception $e + * @param string $errno + * @return bool + */ + public static function isPdoErrorNumber($e, $errno) + { if (preg_match('/(?:\[|\s)([0-9]{4})(?:\]|\s)/', $e->getMessage(), $match)) { return $match[1] == $errno; } diff --git a/core/Tracker/Db/Mysqli.php b/core/Tracker/Db/Mysqli.php index 113d293e20..20df7463ae 100644 --- a/core/Tracker/Db/Mysqli.php +++ b/core/Tracker/Db/Mysqli.php @@ -305,16 +305,9 @@ class Mysqli extends Db } } - /** - * Test error number - * - * @param Exception $e - * @param string $errno - * @return bool - */ public function isErrNo($e, $errno) { - return mysqli_errno($this->connection) == $errno; + return \Piwik\Db\Adapter\Mysqli::isMysqliErrorNumber($e, $this->connection, $errno); } /** diff --git a/core/Tracker/Db/Pdo/Mysql.php b/core/Tracker/Db/Pdo/Mysql.php index 262d703a68..69acedb4d8 100644 --- a/core/Tracker/Db/Pdo/Mysql.php +++ b/core/Tracker/Db/Pdo/Mysql.php @@ -260,10 +260,7 @@ class Mysql extends Db */ public function isErrNo($e, $errno) { - if (preg_match('/([0-9]{4})/', $e->getMessage(), $match)) { - return $match[1] == $errno; - } - return false; + return \Piwik\Db\Adapter\Pdo\Mysql::isPdoErrorNumber($e, $errno); } /** diff --git a/tests/PHPUnit/Integration/Db/Adapter/MysqliTest.php b/tests/PHPUnit/Integration/Db/Adapter/MysqliTest.php new file mode 100644 index 0000000000..0b9b455de4 --- /dev/null +++ b/tests/PHPUnit/Integration/Db/Adapter/MysqliTest.php @@ -0,0 +1,27 @@ +<?php +/** + * Matomo - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Tests\Integration\Db\Adapter; + +use Piwik\Db\Adapter\Mysqli; +use Exception; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; + +class MysqliTest extends IntegrationTestCase +{ + public function test_isMysqliErrorNumber_whenNoConnectionIsSet() + { + $e = new Exception('Error query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry'); + $connection = null; + $this->assertTrue(Mysqli::isMysqliErrorNumber($e, $connection, 1062)); + $this->assertTrue(Mysqli::isMysqliErrorNumber($e, $connection, '1062')); + + $this->assertFalse(Mysqli::isMysqliErrorNumber($e, $connection, '2300')); + $this->assertFalse(Mysqli::isMysqliErrorNumber($e, $connection, '23000')); + } +} diff --git a/tests/PHPUnit/Integration/Db/Adapter/Pdo/MysqlTest.php b/tests/PHPUnit/Integration/Db/Adapter/Pdo/MysqlTest.php new file mode 100644 index 0000000000..75b35d66ef --- /dev/null +++ b/tests/PHPUnit/Integration/Db/Adapter/Pdo/MysqlTest.php @@ -0,0 +1,26 @@ +<?php +/** + * Matomo - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Tests\Integration\Db\Adapter\Pdo; + +use Piwik\Db\Adapter\Pdo\Mysql; +use Exception; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; + +class MysqlTest extends IntegrationTestCase +{ + public function test_isPdoErrorNumber() + { + $e = new Exception('Error query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry'); + $this->assertTrue(Mysql::isPdoErrorNumber($e, 1062)); + $this->assertTrue(Mysql::isPdoErrorNumber($e, '1062')); + + $this->assertFalse(Mysql::isPdoErrorNumber($e, '2300')); + $this->assertFalse(Mysql::isPdoErrorNumber($e, '23000')); + } +} |