From bf0e52a859fa57cf0e7a1c7dd95c6d932cfd0dc3 Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Wed, 3 Dec 2025 11:36:50 +0100 Subject: [PATCH 1/2] #78 feat(*) : improve error logging --- src/Connection.php | 11 ++++++++++- src/Driver/MysqliDriver.php | 20 ++++++++++++++++++-- src/DriverInterface.php | 2 ++ src/Exception/LockException.php | 18 ++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/Exception/LockException.php diff --git a/src/Connection.php b/src/Connection.php index 1b152026..5a53b5a2 100755 --- a/src/Connection.php +++ b/src/Connection.php @@ -304,7 +304,16 @@ public function _pQuery(string $query, array $params = [], array $escapes = []): $this->number++; if ($this->dbDriver !== null) { - $output = $this->dbDriver->_pQuery($query, $this->dbsafeParams($params, $escapes)); + try { + $output = $this->dbDriver->_pQuery($query, $this->dbsafeParams($params, $escapes)); + } catch (QueryException $e) { + $prettifiedQuery = $this->prettifyQuery($e->getQuery(), $e->getParams()); + + $this->logger->error($e->getMessage()); + $this->logger->error('Query: ' . $prettifiedQuery); + + throw $e; + } } if (!$output) { diff --git a/src/Driver/MysqliDriver.php b/src/Driver/MysqliDriver.php index 0c05a892..82a0d297 100755 --- a/src/Driver/MysqliDriver.php +++ b/src/Driver/MysqliDriver.php @@ -15,6 +15,7 @@ use Artemeon\Database\ConnectionParameters; use Artemeon\Database\Exception\ConnectionException; +use Artemeon\Database\Exception\LockException; use Artemeon\Database\Exception\QueryException; use Artemeon\Database\Schema\DataType; use Artemeon\Database\Schema\Table; @@ -146,7 +147,17 @@ public function _pQuery(string $query, array $params): bool } if ($output === false) { - throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params); + // in case there was an error reset the statement cache so that we use a new statement + $name = $this->getPreparedStatementName($query); + if (isset($this->statementsCache[$name])) { + unset($this->statementsCache[$name]); + } + + if ($statement->errno === 1213) { + throw new LockException('Could not execute statement: ' . $this->getError(), $query, $params); + } else { + throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params); + } } $this->affectedRowsCount = $statement->affected_rows; @@ -594,12 +605,17 @@ public function dbImport(string $fileName): bool return true; } + private function getPreparedStatementName(string $query): string + { + return md5($query); + } + /** * Prepares a statement or uses an instance from the cache. */ private function getPreparedStatement(string $query): false | mysqli_stmt { - $name = md5($query); + $name = $this->getPreparedStatementName($query); if (isset($this->statementsCache[$name])) { return $this->statementsCache[$name]; diff --git a/src/DriverInterface.php b/src/DriverInterface.php index f53c5b96..2f373bed 100755 --- a/src/DriverInterface.php +++ b/src/DriverInterface.php @@ -58,6 +58,8 @@ public function insertOrUpdate(string $table, array $columns, array $values, arr /** * Sends a prepared statement to the database. All params must be represented by the "?" char. * The params themselves are stored using the second params using the matching order. + * + * @throws QueryException */ public function _pQuery(string $query, array $params): bool; diff --git a/src/Exception/LockException.php b/src/Exception/LockException.php new file mode 100644 index 00000000..73ad28ee --- /dev/null +++ b/src/Exception/LockException.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Artemeon\Database\Exception; + +class LockException extends QueryException +{ +} From 00424c53a81022e1db0b31ca020fafd57d90f18a Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Wed, 3 Dec 2025 11:39:25 +0100 Subject: [PATCH 2/2] #78 feat(*) : fix style --- src/Driver/MysqliDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Driver/MysqliDriver.php b/src/Driver/MysqliDriver.php index 82a0d297..b8309776 100755 --- a/src/Driver/MysqliDriver.php +++ b/src/Driver/MysqliDriver.php @@ -155,9 +155,9 @@ public function _pQuery(string $query, array $params): bool if ($statement->errno === 1213) { throw new LockException('Could not execute statement: ' . $this->getError(), $query, $params); - } else { - throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params); } + + throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params); } $this->affectedRowsCount = $statement->affected_rows;