Skip to content
Open
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
72 changes: 9 additions & 63 deletions analyzer-baseline.toml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lint-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ count = 1
file = "src/Metadata/Source.php"
code = "no-else-clause"
message = "Avoid `else` clauses."
count = 4
count = 3

[[issues]]
file = "src/Metadata/Source.php"
Expand Down
3 changes: 3 additions & 0 deletions mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ php-version = "8.3.0"
paths = ["src", "test"]
includes = ["vendor"]

[formatter]
parentheses-around-new-in-member-access = true

[linter]
baseline = "lint-baseline.toml"

Expand Down
37 changes: 19 additions & 18 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ class Connection extends AbstractConnection implements DriverAwareInterface
{
protected DriverInterface|Driver $driver;

/** @var ?PgSqlConnection */
protected $resource;

/** PostgreSQL connection type */
protected ?int $type = null;

Expand All @@ -64,7 +61,7 @@ public function beginTransaction(): static
$this->connect();
}

pg_query($this->resource, 'BEGIN');
pg_query($this->getResource(), 'BEGIN');
$this->inTransaction = true;

return $this;
Expand All @@ -84,7 +81,7 @@ public function commit(): static
return $this; // We ignore attempts to commit non-existing transaction
}

pg_query($this->resource, 'COMMIT');
pg_query($this->getResource(), 'COMMIT');
$this->inTransaction = false;

return $this;
Expand All @@ -103,13 +100,16 @@ public function connect(): static
}

$connection = $this->getConnectionString();
set_error_handler(static function ($number, $string) {
throw new Exception\RuntimeException(
self::class . '::connect: Unable to connect to database',
$number,
new Exception\ErrorException($string, $number),
);
});
set_error_handler(
/** @throws Exception\RuntimeException */
static function ($number, $string) {
throw new Exception\RuntimeException(
self::class . '::connect: Unable to connect to database',
$number,
new Exception\ErrorException($string, $number),
);
},
);
try {
$this->resource = pg_connect($connection);
} finally {
Expand Down Expand Up @@ -142,7 +142,7 @@ public function connect(): static
#[Override]
public function disconnect(): static
{
pg_close($this->resource);
pg_close($this->getResource());
$this->resource = null;
return $this;
}
Expand All @@ -161,13 +161,13 @@ public function execute($sql): ResultInterface

$this->profiler?->profilerStart($sql);

$resultResource = pg_query($this->resource, $sql);
$resultResource = pg_query($this->getResource(), $sql);

$this->profiler?->profilerFinish();

// if the returnValue is something other than a pg result resource, bypass wrapping it
if (false === $resultResource) {
throw new Exception\InvalidQueryException(pg_last_error($this->resource));
throw new Exception\InvalidQueryException(pg_last_error($this->getResource()));
}

/** @phpstan-ignore argument.type */
Expand All @@ -184,7 +184,7 @@ public function getCurrentSchema(): string|false
$this->connect();
}

$result = pg_query($this->resource, 'SELECT CURRENT_SCHEMA AS "currentschema"');
$result = pg_query($this->getResource(), 'SELECT CURRENT_SCHEMA AS "currentschema"');
if (false === $result) {
return false;
}
Expand All @@ -202,7 +202,7 @@ public function getLastGeneratedValue(?string $name = null): string|int|false|nu
return null;
}
$result = pg_query(
$this->resource,
$this->getResource(),
'SELECT CURRVAL(\'' . str_replace('\'', '\\\'', $name) . '\') as "currval"',
);

Expand All @@ -213,6 +213,7 @@ public function getLastGeneratedValue(?string $name = null): string|int|false|nu
#[Override]
public function getResource(): ?PgSqlConnection
{
/** @var PgSqlConnection|null */
return $this->resource;
}

Expand All @@ -239,7 +240,7 @@ public function rollback(): static
throw new Exception\RuntimeException('Must call beginTransaction() before you can rollback');
}

pg_query($this->resource, 'ROLLBACK');
pg_query($this->getResource(), 'ROLLBACK');
$this->inTransaction = false;

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Container/PdoConnectionInterfaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function __invoke(
$connectionInfo = $options['connection']
?? $config[$requestedName]['connection']
?? $config[AdapterInterface::class]['adapters'][$requestedName]['connection']
?? null;
?? null;
if (null === $connectionInfo || ! is_array($connectionInfo) || [] === $connectionInfo) {
throw new InvalidConnectionParametersException(
'Connection configuration must be an array of parameters',
$connectionInfo,
is_array($connectionInfo) ? $connectionInfo : [],
);
}
return new Pdo\Connection($connectionInfo);
Expand Down
2 changes: 1 addition & 1 deletion src/Container/PdoDriverInterfaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __invoke(
$connectionParameters = $options['connection']
?? $config[$requestedName]['connection']
?? $config[AdapterInterface::class]['adapters'][$requestedName]['connection']
?? null;
?? null;
$connection = $container->build(Pdo\Connection::class, ['connection' => $connectionParameters]);
return new Pdo\Driver(
connection: $connection,
Expand Down
10 changes: 9 additions & 1 deletion src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpDb\Pgsql;

use Override;
use PgSql\Connection as PgSqlConnection;
use PgSql\Result as PgSqlResult;
use PhpDb\Adapter\Driver\ConnectionInterface;
use PhpDb\Adapter\Driver\DriverAwareInterface;
Expand Down Expand Up @@ -91,7 +92,14 @@ public function createStatement($sqlOrResource = null): StatementInterface&State
$this->connection->connect();
}

$statement->initialize($this->connection->getResource());
$resource = $this->connection->getResource();
if (! $resource instanceof PgSqlConnection) {
throw new Exception\RuntimeException(
'Unable to create a statement without a PostgreSQL connection resource',
);
}

$statement->initialize($resource);

return $statement;
}
Expand Down
28 changes: 11 additions & 17 deletions src/Metadata/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function array_change_key_case;
use function array_walk;
use function implode;
use function is_array;
use function is_string;
use function key;

Expand Down Expand Up @@ -71,6 +70,7 @@ protected function loadColumnData(string $table, string $schema): void
/** @var ResultSet\ResultSetInterface&ResultSet\ResultSet $results */
$results = $this->adapter->query($sql, AdapterInterface::QUERY_MODE_EXECUTE);
$columns = [];
/** @var array{column_name: string, ordinal_position: int|string, column_default: ?string, is_nullable: string, data_type: string, character_maximum_length: int|string|null, character_octet_length: int|string|null, numeric_precision: int|string|null, numeric_scale: int|string|null} $row */
foreach ($results->toArray() as $row) {
$columns[$row['column_name']] = [
'ordinal_position' => $row['ordinal_position'],
Expand Down Expand Up @@ -225,7 +225,7 @@ protected function loadConstraintData(string $table, string $schema): void
$isFK = false;
foreach ($results->toArray() as $row) {
if ($row['constraint_name'] !== $name) {
$name = $row['constraint_name'];
$name = (string) $row['constraint_name'];
$constraints[$name] = [
'constraint_name' => $name,
'constraint_type' => $row['constraint_type'],
Expand All @@ -246,9 +246,9 @@ protected function loadConstraintData(string $table, string $schema): void
$constraints[$name]['delete_rule'] = $row['delete_rule'];
}
}
$constraints[$name]['columns'][] = $row['column_name'];
$constraints[(string) $name]['columns'][] = $row['column_name'];
if ($isFK) {
$constraints[$name]['referenced_columns'][] = $row['referenced_column_name'];
$constraints[(string) $name]['referenced_columns'][] = $row['referenced_column_name'];
}
}

Expand Down Expand Up @@ -283,6 +283,7 @@ protected function loadSchemaData(): void
$results = $this->adapter->query($sql, AdapterInterface::QUERY_MODE_EXECUTE);

$schemas = [];
/** @var array{schema_name: string} $row */
foreach ($results->toArray() as $row) {
$schemas[] = $row['schema_name'];
}
Expand Down Expand Up @@ -346,6 +347,7 @@ protected function loadTableNameData(string $schema): void
$results = $this->adapter->query($sql, AdapterInterface::QUERY_MODE_EXECUTE);

$tables = [];
/** @var array{table_name: string, table_type: string, view_definition: ?string, check_option: ?string, is_updatable: ?string} $row */
foreach ($results->toArray() as $row) {
$tables[$row['table_name']] = [
'table_type' => $row['table_type'],
Expand Down Expand Up @@ -379,22 +381,14 @@ protected function loadTriggerData(string $schema): void
'action_condition',
'action_statement',
'action_orientation',
['action_timing' => 'condition_timing'],
['action_reference_old_table' => 'condition_reference_old_table'],
['action_reference_new_table' => 'condition_reference_new_table'],
'action_timing',
'action_reference_old_table',
'action_reference_new_table',
'created',
];

array_walk($isColumns, static function (&$c) use ($p) {
if (is_array($c)) {
$alias = key($c);
$c = $p->quoteIdentifierChain($c);
if (is_string($alias)) {
$c .= " {$p->quoteIdentifier($alias)}";
}
} else {
$c = $p->quoteIdentifier($c);
}
$c = $p->quoteIdentifier($c);
});

$sql =
Expand Down Expand Up @@ -423,7 +417,7 @@ protected function loadTriggerData(string $schema): void
if (null !== $row['created']) {
$row['created'] = new DateTime($row['created']);
}
$data[$row['trigger_name']] = $row;
$data[(string) $row['trigger_name']] = $row;
}

$this->data['triggers'][$schema] = $data;
Expand Down
2 changes: 1 addition & 1 deletion src/Pdo/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function createResult($resource): ResultInterface
{
/** @var Result $result */
$result = clone $this->resultPrototype;
$result->initialize($resource, $this->connection->getLastGeneratedValue());
$result->initialize($resource, $this->getLastGeneratedValue());

return $result;
}
Expand Down
8 changes: 4 additions & 4 deletions test/integration/AdapterPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PhpDbIntegrationTest\Pgsql;

use PhpDb\Adapter\Exception\VunerablePlatformQuoteException;
use PhpDb\Adapter\Exception\RuntimeException;
use PhpDb\Pgsql\AdapterPlatform;
use PhpDbTestAsset\Pgsql\SetupTrait;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -23,8 +23,7 @@ public function quoteValueWithPdoPgsql(): void
{
$adapter = $this->getAdapter(self::PDO_ADAPTER);
$pgsql = $adapter->getPlatform();
//$this->expectException(VunerablePlatformQuoteException::class);
$value = $pgsql->quoteValue('value');
$value = $pgsql->quoteValue('value');
static::assertSame('\'value\'', $value);
}

Expand All @@ -33,7 +32,8 @@ public function quoteValueWithPgsql(): void
{
$adapter = $this->getAdapter(self::NATIVE_ADAPTER);
$pgsql = $adapter->getPlatform();
$this->expectException(VunerablePlatformQuoteException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('without extension/driver support can introduce security vulnerabilities');
$value = $pgsql->quoteValue('value');
static::assertSame('\'value\'', $value);

Expand Down
Loading
Loading