Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 17 additions & 1 deletion src/Driver/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,6 +147,16 @@ public function _pQuery(string $query, array $params): bool
}

if ($output === false) {
// 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);
}

throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params);
}

Expand Down Expand Up @@ -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];
Expand Down
2 changes: 2 additions & 0 deletions src/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 18 additions & 0 deletions src/Exception/LockException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* 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
{
}
Loading