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
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 7
level: 8
paths:
- src
- tests
61 changes: 19 additions & 42 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Connection implements ConnectionInterface
/**
* Instance of the db-driver defined in the configs.
*/
protected ?DriverInterface $dbDriver = null;
protected DriverInterface $dbDriver;

/**
* The number of transactions currently opened.
Expand Down Expand Up @@ -120,7 +120,7 @@ public function close(): void
$this->logger?->warning('Rolled back open transactions on deletion of current instance of Db!');
}

if ($this->dbDriver !== null && $this->connected) {
if ($this->connected) {
$this->logger?->info('closing database-connection');

$this->dbDriver->dbclose();
Expand Down Expand Up @@ -216,7 +216,7 @@ public function selectRow(
),
);

$row = $this->getPRow($query, array_values($identifiers), 0, $cached, $escapes);
$row = $this->getPRow($query, array_values($identifiers), 0, $cached, $escapes ?? []);
if ($row === []) {
return null;
}
Expand Down Expand Up @@ -317,17 +317,15 @@ public function _pQuery(string $query, array $params = [], array $escapes = []):
// Increasing the counter
$this->number++;

if ($this->dbDriver !== null) {
try {
$output = $this->dbDriver->_pQuery($query, $this->dbsafeParams($params, $escapes));
} catch (QueryException $e) {
$prettifiedQuery = $this->prettifyQuery($e->getQuery(), $e->getParams());
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);
$this->logger?->error($e->getMessage());
$this->logger?->error('Query: ' . $prettifiedQuery);

throw $e;
}
throw $e;
}

if (!$output) {
Expand Down Expand Up @@ -618,10 +616,7 @@ private function getError(string $query, array $params): void
$this->dbconnect();
}

$error = '';
if ($this->dbDriver !== null) {
$error = $this->dbDriver->getError();
}
$error = $this->dbDriver->getError();

// reprocess query
$query = str_ireplace(
Expand Down Expand Up @@ -665,10 +660,6 @@ public function beginTransaction(): void
$this->dbconnect();
}

if ($this->dbDriver === null) {
return;
}

// just start a new transaction, if no other transaction is open.
if ($this->numberOfOpenTransactions === 0) {
$this->dbDriver->beginTransaction();
Expand Down Expand Up @@ -700,10 +691,6 @@ public function commit(): void
$this->dbconnect();
}

if ($this->dbDriver === null) {
return;
}

// check, if the current tx is allowed to be committed.
if ($this->numberOfOpenTransactions === 1) {
$this->numberOfOpenTransactions--;
Expand Down Expand Up @@ -743,10 +730,6 @@ public function rollBack(): void
$this->dbconnect();
}

if ($this->dbDriver === null) {
return;
}

if ($this->numberOfOpenTransactions === 1) {
$this->dbDriver->rollBack();
$this->currentTransactionIsDirty = false;
Expand Down Expand Up @@ -802,15 +785,13 @@ public function getTables(?string $prefix = null): array

$this->tablesCache[$prefix] = [];

if ($this->dbDriver !== null) {
// increase global counter
$this->number++;
$tables = $this->dbDriver->getTables();
// increase global counter
$this->number++;
$tables = $this->dbDriver->getTables();

foreach ($tables as $table) {
if (str_starts_with((string) $table['name'], $prefix)) {
$this->tablesCache[$prefix][] = $table['name'];
}
foreach ($tables as $table) {
if (str_starts_with((string) $table['name'], $prefix)) {
$this->tablesCache[$prefix][] = $table['name'];
}
}

Expand Down Expand Up @@ -845,7 +826,7 @@ public function getColumnsOfTable(string $tableName): array
$columnName = $column->getName();
$return[$columnName] = [
'columnName' => $columnName,
'columnType' => $column->getInternalType(),
'columnType' => $column->getInternalType() ?? DataType::CHAR254,
];
}

Expand Down Expand Up @@ -949,7 +930,7 @@ public function generateTableFromMetadata(Table $table): void
{
$columns = [];
foreach ($table->getColumns() as $colDef) {
$columns[$colDef->getName()] = [$colDef->getInternalType(), $colDef->isNullable()];
$columns[$colDef->getName()] = [$colDef->getInternalType() ?? DataType::CHAR254, $colDef->isNullable()];
}

$primary = [];
Expand Down Expand Up @@ -1195,10 +1176,6 @@ public function getDbInfo(): array
$this->dbconnect();
}

if ($this->dbDriver === null) {
return [];
}

return $this->dbDriver->getDbInfo();
}

Expand Down
3 changes: 3 additions & 0 deletions src/Driver/DriverAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ abstract class DriverAbstract implements DriverInterface

protected ?ConnectionParameters $config = null;

/**
* @phpstan-assert ConnectionParameters $this->config
*/
public function setConfig(ConnectionParameters $params): void
{
$this->config = $params;
Expand Down
39 changes: 36 additions & 3 deletions src/Driver/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use mysqli_sql_exception;
use mysqli_stmt;
use Override;
use RuntimeException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -98,6 +99,8 @@ public function dbclose(): void
return;
}

$this->assertConnected();

$this->linkDB->close();
$this->linkDB = null;
$this->connected = false;
Expand Down Expand Up @@ -237,6 +240,8 @@ public function insertOrUpdate(string $table, array $columns, array $values, arr
#[Override]
public function getError(): string
{
$this->assertConnected();

$error = $this->errorMessage . ' ' . $this->linkDB->error;
$this->errorMessage = '';

Expand Down Expand Up @@ -274,7 +279,7 @@ public function getTableInformation(string $tableName): Table
$table->addColumn(
TableColumn::make($column['Field'])
->setInternalType($this->getCoreTypeForDbType($column))
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column)))
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column) ?? DataType::CHAR254))
->setNullable($column['Null'] === 'YES'),
);
}
Expand Down Expand Up @@ -454,6 +459,8 @@ public function deleteIndex(string $table, string $index): bool
#[Override]
public function beginTransaction(): void
{
$this->assertConnected();

$this->linkDB->begin_transaction();
}

Expand All @@ -472,6 +479,8 @@ public function transactionBegin(): void
#[Override]
public function commit(): void
{
$this->assertConnected();

$this->linkDB->commit();
}

Expand All @@ -487,6 +496,8 @@ public function transactionCommit(): void
#[Override]
public function rollBack(): void
{
$this->assertConnected();

$this->linkDB->rollback();
}

Expand All @@ -505,6 +516,8 @@ public function transactionRollback(): void
#[Override]
public function getDbInfo(): array
{
$this->assertConnected();

return [
'dbbserver' => 'MySQL ' . $this->linkDB->server_info,
'server_version' => $this->linkDB->server_version,
Expand Down Expand Up @@ -542,6 +555,10 @@ public function encloseTableName(string $table): string
#[Override]
public function dbExport(string &$fileName, array $tables): bool
{
if (!$this->config instanceof ConnectionParameters) {
throw new RuntimeException('Connection parameters not set');
}

$dumpBin = new ExecutableFinder()->find($this->dumpBin);
$dumpParams = [
$dumpBin,
Expand Down Expand Up @@ -578,7 +595,11 @@ public function dbExport(string &$fileName, array $tables): bool
public function dbImport(string $fileName): bool
{
if (!in_array(pathinfo($fileName, PATHINFO_EXTENSION), ['sql', 'gz'])) {
throw new \RuntimeException(trim($fileName . ' is not a valid import file'));
throw new RuntimeException(trim($fileName . ' is not a valid import file'));
}

if (!$this->config instanceof ConnectionParameters) {
throw new RuntimeException('Connection parameters not set');
}

$restoreBin = new ExecutableFinder()->find($this->restoreBin);
Expand All @@ -598,7 +619,7 @@ public function dbImport(string $fileName): bool
} elseif (pathinfo($fileName, PATHINFO_EXTENSION) === 'sql') {
$fileCommand = sprintf('cat %s', escapeshellarg($fileName));
} else {
throw new \RuntimeException(trim($fileName . ' is not a valid import file'));
throw new RuntimeException(trim($fileName . ' is not a valid import file'));
}

$process = new Process([
Expand Down Expand Up @@ -636,6 +657,8 @@ private function getPreparedStatement(string $query): false | mysqli_stmt
$this->statementsCache = [];
}

$this->assertConnected();

$statement = $this->linkDB->stmt_init();

try {
Expand Down Expand Up @@ -677,4 +700,14 @@ public function getNthLastElementFromSlug(string $column, int $position): string
{
return "SUBSTRING_INDEX(SUBSTRING_INDEX($column, '/', -$position), '/', 1)";
}

/**
* @phpstan-assert mysqli $this->linkDB
*/
private function assertConnected(): void
{
if ($this->linkDB === null) {
throw new ConnectionException('Database not connected.');
}
}
}
12 changes: 10 additions & 2 deletions src/Driver/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function getTableInformation(string $tableName): Table
$table->addColumn(
TableColumn::make($column['column_name'])
->setInternalType($this->getCoreTypeForDbType($column))
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column)))
->setDatabaseType($this->getDatatype($this->getCoreTypeForDbType($column) ?? DataType::CHAR254))
->setNullable($column['is_nullable'] === 'YES'),
);
}
Expand Down Expand Up @@ -510,6 +510,10 @@ public function dbExport(string &$fileName, array $tables): bool
$tablesString = '-t ' . implode(' -t ', array_map('escapeshellarg', $tables));
}

if (!$this->config instanceof ConnectionParameters) {
throw new RuntimeException('Connection parameters not set');
}

$port = $this->config->getPort();
if (empty($port)) {
$port = 5432;
Expand Down Expand Up @@ -557,6 +561,10 @@ public function dbImport(string $fileName): bool
throw new RuntimeException(trim($fileName . ' is not a valid import file'));
}

if (!$this->config instanceof ConnectionParameters) {
throw new RuntimeException('Connection parameters not set');
}

$restoreBin = new ExecutableFinder()->find($this->restoreBin);
if ($this->handlesDumpCompression() && pathinfo($fileName, PATHINFO_EXTENSION) === 'gz') {
$restoreParams = [
Expand Down Expand Up @@ -618,7 +626,7 @@ protected function processQuery(string $query): string
$i++;

return '$' . $i;
}, $query);
}, $query) ?? '';

return str_replace(' LIKE ', ' ILIKE ', $query);
}
Expand Down
Loading
Loading