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
4 changes: 2 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ public function getCacheSize(): int
* An internal wrapper to dbsafeString, used to process a complete array of parameters
* as used by prepared statements.
*
* @param array<array-key, BackedEnum|EscapeableParameterInterface|scalar|null> $params
* @param array<array-key, BackedEnum|EscapeableParameterInterface|scalar|Stringable|null> $params
* @param list<bool>|false $escapes An array of boolean for each param, used to block the escaping of html-special chars.
* If not passed, all params will be cleaned.
*
Expand Down Expand Up @@ -1257,7 +1257,7 @@ private function dbsafeParams(array $params, array | false $escapes = []): array
$replace[$key] = $param;
}

return array_values($replace);
return array_values($replace); // @phpstan-ignore return.type (Unable to fix right now)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Artemeon\Database\Schema\DataType;
use Artemeon\Database\Schema\Table;
use Artemeon\Database\Schema\TableIndex;
use BackedEnum;
use Generator;
use Stringable;

Expand Down Expand Up @@ -313,7 +314,7 @@ public function encloseTableName(string $tableName): string;
* Helper to replace all param-placeholder with the matching value, only to be used
* to render a debuggable-statement.
*
* @param list<scalar|Stringable|null> $params
* @param list<BackedEnum|EscapeableParameterInterface|scalar|Stringable|null> $params
*/
public function prettifyQuery(string $query, array $params): string;

Expand Down
3 changes: 2 additions & 1 deletion src/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Artemeon\Database\Schema\DataType;
use Artemeon\Database\Schema\Table;
use Artemeon\Database\Schema\TableIndex;
use BackedEnum;
use Generator;
use Stringable;

Expand Down Expand Up @@ -68,7 +69,7 @@ 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.
*
* @param list<scalar|Stringable|null> $params
* @param list<BackedEnum|EscapeableParameterInterface|scalar|Stringable|null> $params
*
* @throws QueryException
*/
Expand Down
6 changes: 6 additions & 0 deletions src/EscapeableParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

namespace Artemeon\Database;

use Stringable;

interface EscapeableParameterInterface
{
public function isEscape(): bool;

/**
* @return scalar|Stringable|null
*/
public function getValue(): mixed;
}
6 changes: 4 additions & 2 deletions src/Exception/QueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

namespace Artemeon\Database\Exception;

use Artemeon\Database\EscapeableParameterInterface;
use BackedEnum;
use Exception;
use Stringable;
use Throwable;

class QueryException extends Exception
{
/**
* @param list<scalar|Stringable|null> $params
* @param list<BackedEnum|EscapeableParameterInterface|scalar|Stringable|null> $params
*/
public function __construct(string $message, private readonly string $query, private readonly array $params, ?Throwable $previous = null)
{
Expand All @@ -33,7 +35,7 @@ public function getQuery(): string
}

/**
* @return list<scalar|Stringable|null>
* @return list<BackedEnum|EscapeableParameterInterface|scalar|Stringable|null>
*/
public function getParams(): array
{
Expand Down
9 changes: 9 additions & 0 deletions src/MockConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Artemeon\Database\Schema\DataType;
use Artemeon\Database\Schema\Table;
use Artemeon\Database\Schema\TableIndex;
use BackedEnum;
use Generator;
use Override;

Expand Down Expand Up @@ -336,6 +337,14 @@ public function encloseTableName(string $tableName): string
public function prettifyQuery(string $query, array $params): string
{
foreach ($params as $param) {
if ($param instanceof BackedEnum) {
$param = $param->value;
}

if ($param instanceof EscapeableParameterInterface) {
$param = $param->getValue();
}

$query = (string) preg_replace('/\?/', isset($param) ? '"' . $param . '"' : 'NULL', $query, 1);
}

Expand Down
Loading