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
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- phpstan-baseline.neon

parameters:
level: 6
level: 7
paths:
- src
- tests
13 changes: 8 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use InvalidArgumentException;
use Override;
use Psr\Log\LoggerInterface;
use RuntimeException;

/**
* This class handles all traffic from and to the database and takes care of a correct tx-handling
Expand Down Expand Up @@ -155,6 +156,10 @@ public function multiInsert(string $tableName, array $columns, array $valueSets,
$output = true;
$setsPerInsert = (int) floor(970 / count($columns));

if ($setsPerInsert < 1) {
throw new RuntimeException('At least one set per insert operation must be performed.');
}

foreach (array_chunk($valueSets, $setsPerInsert) as $valueSet) {
$output = $output && $this->dbDriver->triggerMultiInsert(
$tableName,
Expand Down Expand Up @@ -1238,13 +1243,11 @@ public function getCacheSize(): int
* An internal wrapper to dbsafeString, used to process a complete array of parameters
* as used by prepared statements.
*
* @template TKey of array-key
*
* @param array<TKey, mixed> $params
* @param array<array-key, mixed> $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.
*
* @return array<TKey, mixed>
* @return list<mixed>
*
* @see Db::dbsafeString($string, $htmlSpecialChars = true)
*/
Expand Down Expand Up @@ -1275,7 +1278,7 @@ private function dbsafeParams(array $params, array | false $escapes = []): array
$replace[$key] = $param;
}

return $replace;
return array_values($replace);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Driver/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function _pQuery(string $query, array $params): bool
throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params);
}

$this->affectedRowsCount = $statement->affected_rows;
$this->affectedRowsCount = (int) $statement->affected_rows;
$statement->free_result();

return $output;
Expand Down Expand Up @@ -193,6 +193,10 @@ public function getPArray(string $query, array $params): Generator

$result = $statement->get_result();

if ($result === false) {
return;
}

while ($row = $result->fetch_assoc()) {
yield $row;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Driver/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function _pQuery(string $query, array $params): bool
throw new QueryException('Could not prepare statement: ' . $this->getError(), $query, $params);
}

$this->assertConnected();

$result = pg_execute($this->linkDB, $name, $params);
if ($result === false) {
throw new QueryException('Could not execute statement: ' . $this->getError(), $query, $params);
Expand All @@ -119,6 +121,8 @@ public function getPArray(string $query, array $params): Generator
throw new QueryException('Could not prepare statement: ' . $this->getError(), $query, $params);
}

$this->assertConnected();

$resultSet = pg_execute($this->linkDB, $name, $params);

if ($resultSet === false) {
Expand Down Expand Up @@ -193,6 +197,8 @@ public function insertOrUpdate(string $table, array $columns, array $values, arr
#[Override]
public function getError(): string
{
$this->assertConnected();

return pg_last_error($this->linkDB);
}

Expand Down Expand Up @@ -486,6 +492,8 @@ public function transactionRollback(): void
#[Override]
public function getDbInfo(): array
{
$this->assertConnected();

return pg_version($this->linkDB);
}

Expand Down Expand Up @@ -626,6 +634,8 @@ private function getPreparedStatementName(string $query): false | string
return $sum;
}

$this->assertConnected();

if (pg_prepare($this->linkDB, $sum, $query)) {
$this->statementsCache[] = $sum;
} else {
Expand Down Expand Up @@ -690,4 +700,14 @@ public function getNthLastElementFromSlug(string $column, int $position): string
{
return "SPLIT_PART(REVERSE(SPLIT_PART(REVERSE($column), '/', $position)), '/', 1)";
}

/**
* @phpstan-assert Connection $this->linkDB
*/
private function assertConnected(): void
{
if (!$this->linkDB instanceof Connection) {
throw new ConnectionException('Database not connected.');
}
}
}
2 changes: 1 addition & 1 deletion src/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DriverFactory
public function factory(string $driver): DriverInterface
{
$class = 'Artemeon\\Database\\Driver\\' . ucfirst($driver) . 'Driver';
if (!class_exists($class)) {
if (!class_exists($class) || !is_a($class, DriverInterface::class, true)) {
throw new DriverNotFoundException('Configured driver ' . $class . ' does not exist');
}

Expand Down
6 changes: 3 additions & 3 deletions src/MockConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
class MockConnection implements ConnectionInterface
{
/**
* @var list<array<array-key, mixed>>
* @var list<array<string, mixed>>
*/
private array $rows = [];

Expand Down Expand Up @@ -76,13 +76,13 @@ public function getPArray(string $query, array $params = [], ?int $start = null,
#[Override]
public function getPRow(string $query, array $params = [], int $number = 0, bool $cache = true, array $escapes = []): array
{
return current($this->rows);
return current($this->rows) ?: [];
}

#[Override]
public function selectRow(string $tableName, array $columns, array $identifiers, bool $cached = true, ?array $escapes = []): ?array
{
return current($this->rows);
return current($this->rows) ?: [];
}

#[Override]
Expand Down
14 changes: 13 additions & 1 deletion src/Schema/TableColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ class TableColumn implements JsonSerializable
private string $databaseType = '';
private bool $nullable = true;

/**
* @param non-empty-string $name
*/
public static function make(string $name): self
{
return new self($name);
}

/**
* @param non-empty-string $name
*/
public function __construct(private string $name)
{
}
Expand All @@ -38,7 +44,7 @@ public function __construct(private string $name)
* @inheritDoc
*
* @return array{
* name: string,
* name: non-empty-string,
* internalType: string,
* databaseType: string,
* nullable: bool,
Expand Down Expand Up @@ -67,11 +73,17 @@ public function setNullable(bool $nullable): self
return $this;
}

/**
* @return non-empty-string
*/
public function getName(): string
{
return $this->name;
}

/**
* @param non-empty-string $name
*/
public function setName(string $name): self
{
$this->name = $name;
Expand Down
4 changes: 4 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ public function testIntComparison(string $id, int $date, int $expected): void
{
// note calculation does not work if we cross a year border.
$objLeftDate = DateTime::createFromFormat('YmdHis', '' . $date);
if ($objLeftDate === false) {
self::fail('Invalid date given.');
}

$objLeftDate->add(new DateInterval('P1M'));
$left = $objLeftDate->format('YmdHis');

Expand Down
Loading