Skip to content

Commit

Permalink
PgQuery - fixes?
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Oct 20, 2024
1 parent c816bc8 commit 103887b
Show file tree
Hide file tree
Showing 29 changed files with 233 additions and 241 deletions.
2 changes: 1 addition & 1 deletion phpcs-ignores.neon
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ ignoreErrors:
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
count: 1
path: src/Db/Query.php
path: src/Db/PgQuery.php

-
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
Expand Down
2 changes: 1 addition & 1 deletion src/Db/AsyncHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(Connection $connection)

public function createAndSetAsyncQuery(
ResultBuilder $resultBuilder,
Query $query,
PgQuery $query,
string|NULL $preparedStatementName = NULL,
): AsyncQuery
{
Expand Down
6 changes: 3 additions & 3 deletions src/Db/AsyncPreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function executeArgs(array $params): AsyncQuery

$params = self::prepareParams($params);

$query = new Query($this->query, $params);
$query = new PgQuery($this->query, $params);

$success = @\pg_send_execute($this->connection->getResource(), $statementName, $params); // intentionally @
if ($success === FALSE) {
Expand Down Expand Up @@ -66,7 +66,7 @@ private function prepareStatement(): string
if ($success === FALSE) {
throw Exceptions\QueryException::asyncPreparedStatementQueryFailed(
$statementName,
new Query($this->query, []),
new PgQuery($this->query, []),
$this->connection->getLastError(),
);
}
Expand All @@ -75,7 +75,7 @@ private function prepareStatement(): string
if (($resource === FALSE) || (!$this->asyncHelper::checkAsyncQueryResult($resource))) {
throw Exceptions\QueryException::asyncPreparedStatementQueryFailed(
$statementName,
new Query($this->query, []),
new PgQuery($this->query, []),
($resource !== FALSE) ? (string) \pg_result_error($resource) : $this->connection->getLastError(),
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Db/AsyncQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AsyncQuery

private AsyncHelper $asyncHelper;

private Query $query;
private PgQuery $query;

private string|NULL $preparedStatementName;

Expand All @@ -19,7 +19,7 @@ public function __construct(
Connection $connection,
ResultBuilder $resultBuilder,
AsyncHelper $asyncHelper,
Query $query,
PgQuery $query,
string|NULL $preparedStatementName = NULL,
)
{
Expand All @@ -31,7 +31,7 @@ public function __construct(
}


public function getQuery(): Query
public function getQuery(): PgQuery
{
return $this->query;
}
Expand All @@ -48,8 +48,8 @@ public function getNextResult(): Result
throw Exceptions\ConnectionException::asyncNoQueryIsSent();
} else if (($actualAsyncQuery !== $this) || ($actualAsyncExecuteQuery !== NULL)) {
throw Exceptions\ConnectionException::anotherAsyncQueryIsRunning(
$this->getQuery()->getSql(),
$actualAsyncExecuteQuery ?? $actualAsyncQuery->getQuery()->getSql(),
$this->getQuery()->sql,
$actualAsyncExecuteQuery ?? $actualAsyncQuery->getQuery()->sql,
);
}

Expand Down
36 changes: 18 additions & 18 deletions src/Db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public function setDataTypeCache(DataTypeCache $dataTypeCache): static
* @throws Exceptions\ConnectionException
* @throws Exceptions\QueryException
*/
public function query(string|Query|Sql $sql, mixed ...$params): Result
public function query(string|PgQuery|Sql $sql, mixed ...$params): Result
{
\assert(\array_is_list($params));
return $this->queryArgs($sql, $params);
Expand All @@ -294,17 +294,17 @@ public function query(string|Query|Sql $sql, mixed ...$params): Result
* @throws Exceptions\ConnectionException
* @throws Exceptions\QueryException
*/
public function queryArgs(string|Query|Sql $sql, array $params): Result
public function queryArgs(string|PgQuery|Sql $sql, array $params): Result
{
$query = $this->prepareQuery(Query::from($sql, $params));
$query = $this->prepareQuery(PgQuery::from($sql, $params));

$startTime = $this->events->hasOnQuery() ? \hrtime(TRUE) : NULL;

$queryParams = $query->getParams();
$queryParams = $query->params;
if ($queryParams === []) {
$resource = @\pg_query($this->getConnectedResource(), $query->getSql()); // intentionally @
$resource = @\pg_query($this->getConnectedResource(), $query->sql); // intentionally @
} else {
$resource = @\pg_query_params($this->getConnectedResource(), $query->getSql(), $queryParams); // intentionally @
$resource = @\pg_query_params($this->getConnectedResource(), $query->sql, $queryParams); // intentionally @
}

if ($resource === FALSE) {
Expand All @@ -331,11 +331,11 @@ public function execute(string $sql): static

$resource = @\pg_query($this->getConnectedResource(), $sql); // intentionally @
if ($resource === FALSE) {
throw Exceptions\QueryException::queryFailed(new Query($sql, []), $this->getLastError());
throw Exceptions\QueryException::queryFailed(new PgQuery($sql, []), $this->getLastError());
}

if ($startTime !== NULL) {
$this->events->onQuery(new Query($sql, []), \hrtime(TRUE) - $startTime);
$this->events->onQuery(new PgQuery($sql, []), \hrtime(TRUE) - $startTime);
}

return $this;
Expand All @@ -346,7 +346,7 @@ public function execute(string $sql): static
* @throws Exceptions\ConnectionException
* @throws Exceptions\QueryException
*/
public function asyncQuery(string|Query|Sql $sql, mixed ...$params): AsyncQuery
public function asyncQuery(string|PgQuery|Sql $sql, mixed ...$params): AsyncQuery
{
\assert(\array_is_list($params));
return $this->asyncQueryArgs($sql, $params);
Expand All @@ -358,15 +358,15 @@ public function asyncQuery(string|Query|Sql $sql, mixed ...$params): AsyncQuery
* @throws Exceptions\ConnectionException
* @throws Exceptions\QueryException
*/
public function asyncQueryArgs(string|Query|Sql $sql, array $params): AsyncQuery
public function asyncQueryArgs(string|PgQuery|Sql $sql, array $params): AsyncQuery
{
$query = $this->prepareQuery(Query::from($sql, $params));
$query = $this->prepareQuery(PgQuery::from($sql, $params));

$queryParams = $query->getParams();
$queryParams = $query->params;
if ($queryParams === []) {
$querySuccess = @\pg_send_query($this->getConnectedResource(), $query->getSql()); // intentionally @
$querySuccess = @\pg_send_query($this->getConnectedResource(), $query->sql); // intentionally @
} else {
$querySuccess = @\pg_send_query_params($this->getConnectedResource(), $query->getSql(), $query->getParams()); // intentionally @
$querySuccess = @\pg_send_query_params($this->getConnectedResource(), $query->sql, $query->params); // intentionally @
}

if ($querySuccess === FALSE) {
Expand Down Expand Up @@ -395,7 +395,7 @@ public function asyncExecute(string $sql): static
}

if ($this->events->hasOnQuery()) {
$this->events->onQuery(new Query($sql, []));
$this->events->onQuery(new PgQuery($sql, []));
}

$this->asyncHelper->setAsyncExecuteQuery($sql);
Expand All @@ -418,7 +418,7 @@ public function completeAsyncExecute(): static
while (($resource = \pg_get_result($this->getConnectedResource())) !== FALSE) {
if (!$this->asyncHelper::checkAsyncQueryResult($resource)) {
throw Exceptions\QueryException::asyncQueryFailed(
new Query($asyncExecuteQuery, []),
new PgQuery($asyncExecuteQuery, []),
(string) \pg_result_error($resource),
);
}
Expand Down Expand Up @@ -516,9 +516,9 @@ public function getResource(): PgSql\Connection
/**
* Extend this method to update query before execution.
*
* @return ($query is string ? string : Query)
* @return ($query is string ? string : PgQuery)
*/
protected function prepareQuery(string|Query $query): string|Query
protected function prepareQuery(string|PgQuery $query): string|PgQuery
{
return $query;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Db/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Events
/** @var list<callable(Connection): void> function (Connection $connection) {} */
private array $onClose = [];

/** @var list<callable(Connection, Query, int|float|NULL, string|NULL): void> function (Connection $connection, Query $query, int|float|NULL $timeNs, string|NULL $prepareStatementName) {} */
/** @var list<callable(Connection, PgQuery, int|float|NULL, string|NULL): void> function (Connection $connection, Query $query, int|float|NULL $timeNs, string|NULL $prepareStatementName) {} */
private array $onQuery = [];

/** @var list<callable(Connection, Result): void> function (Connection $connection, Result $result) {} */
Expand Down Expand Up @@ -73,7 +73,7 @@ public function onClose(): void
}


public function onQuery(Query $query, float|NULL $timeNs = NULL, string|NULL $prepareStatementName = NULL): void
public function onQuery(PgQuery $query, float|NULL $timeNs = NULL, string|NULL $prepareStatementName = NULL): void
{
foreach ($this->onQuery as $event) {
$event($this->connection, $query, $timeNs, $prepareStatementName);
Expand Down
22 changes: 11 additions & 11 deletions src/Db/Exceptions/QueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class QueryException extends Exception
public const MISSING_PARAM = 6;
public const EXTRA_PARAM = 7;

private Db\Query|NULL $query;
private Db\PgQuery|NULL $query;


public function __construct(
string $message = '',
int $code = 0,
Db\Query|NULL $query = NULL,
Db\PgQuery|NULL $query = NULL,
\Throwable|NULL $previous = NULL,
)
{
Expand All @@ -29,41 +29,41 @@ public function __construct(
}


public function getQuery(): Db\Query|NULL
public function getQuery(): Db\PgQuery|NULL
{
return $this->query;
}


public static function queryFailed(Db\Query $query, string $error): self
public static function queryFailed(Db\PgQuery $query, string $error): self
{
return new self(\sprintf('Query failed [%s]: \'%s\'.', $error, $query->getSql()), self::QUERY_FAILED, $query);
return new self(\sprintf('Query failed [%s]: \'%s\'.', $error, $query->sql), self::QUERY_FAILED, $query);
}


public static function asyncQueryFailed(Db\Query $query, string $error): self
public static function asyncQueryFailed(Db\PgQuery $query, string $error): self
{
return new self(\sprintf('Async query failed [%s]: \'%s\'.', $error, $query->getSql()), self::ASYNC_QUERY_FAILED, $query);
return new self(\sprintf('Async query failed [%s]: \'%s\'.', $error, $query->sql), self::ASYNC_QUERY_FAILED, $query);
}


public static function preparedStatementQueryFailed(
string $preparedStatementName,
Db\Query $query,
Db\PgQuery $query,
string $error,
): self
{
return new self(\sprintf('Prepared statement failed [%s]: \'%s\', query: \'%s\'.', $error, $preparedStatementName, $query->getSql()), self::PREPARED_STATEMENT_QUERY_FAILED, $query);
return new self(\sprintf('Prepared statement failed [%s]: \'%s\', query: \'%s\'.', $error, $preparedStatementName, $query->sql), self::PREPARED_STATEMENT_QUERY_FAILED, $query);
}


public static function asyncPreparedStatementQueryFailed(
string $preparedStatementName,
Db\Query $query,
Db\PgQuery $query,
string $error,
): self
{
return new self(\sprintf('Async prepared statement failed [%s]: \'%s\', query \'%s\'.', $error, $preparedStatementName, $query->getSql()), self::ASYNC_PREPARED_STATEMENT_QUERY_FAILED, $query);
return new self(\sprintf('Async prepared statement failed [%s]: \'%s\', query \'%s\'.', $error, $preparedStatementName, $query->sql), self::ASYNC_PREPARED_STATEMENT_QUERY_FAILED, $query);
}


Expand Down
4 changes: 2 additions & 2 deletions src/Db/Exceptions/ResultException.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static function fetchMutatorBadReturnType(string $column, mixed $value):
}


public static function noOtherAsyncResult(Db\Query $query): self
public static function noOtherAsyncResult(Db\PgQuery $query): self
{
return new self(\sprintf('No other result for async query \'%s\'.', $query->getSql()), self::NO_OTHER_ASYNC_RESULT);
return new self(\sprintf('No other result for async query \'%s\'.', $query->sql), self::NO_OTHER_ASYNC_RESULT);
}


Expand Down
14 changes: 4 additions & 10 deletions src/Db/Query.php → src/Db/PgQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Forrest79\PhPgSql\Db;

class Query
class PgQuery
{
private string $sql;
public readonly string $sql;

/** @var list<mixed> */
private array $params;
public readonly array $params;


/**
Expand All @@ -20,14 +20,8 @@ public function __construct(string $sql, array $params)
}


public function getSql(): string
{
return $this->sql;
}


/**
* @return list<mixed>
* @param list<mixed> $params
*/
public static function from(string|self|Sql $query, array $params = []): self
{
Expand Down
4 changes: 2 additions & 2 deletions src/Db/PreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function executeArgs(array $params): Result

$params = self::prepareParams($params);

$query = new Query($this->query, $params);
$query = new PgQuery($this->query, $params);

$resource = @\pg_execute($this->connection->getResource(), $statementName, $params); // intentionally @
if ($resource === FALSE) {
Expand All @@ -49,7 +49,7 @@ private function prepareStatement(): string
if ($resource === FALSE) {
throw Exceptions\QueryException::preparedStatementQueryFailed(
$statementName,
new Query($this->query, []),
new PgQuery($this->query, []),
$this->connection->getLastError(),
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Db/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Result implements ColumnValueParser, \Countable, \IteratorAggregate
{
protected PgSql\Result $queryResource;

private Query $query;
private PgQuery $query;

private RowFactory $rowFactory;

Expand Down Expand Up @@ -40,7 +40,7 @@ class Result implements ColumnValueParser, \Countable, \IteratorAggregate
*/
public function __construct(
PgSql\Result $queryResource,
Query $query,
PgQuery $query,
RowFactory $rowFactory,
DataTypeParser $dataTypeParser,
array|NULL $dataTypesCache,
Expand Down Expand Up @@ -380,7 +380,7 @@ public function fetchIterator(): RowIterator
}


public function getQuery(): Query
public function getQuery(): PgQuery
{
return $this->query;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/ResultBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(Connection $connection, Events $events)
}


public function build(PgSql\Result $resource, Query $query): Result
public function build(PgSql\Result $resource, PgQuery $query): Result
{
$result = $this->getResultFactory()->create(
$resource,
Expand Down
2 changes: 1 addition & 1 deletion src/Db/ResultFactories/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Basic implements Db\ResultFactory
*/
public function create(
PgSql\Result $queryResource,
Db\Query $query,
Db\PgQuery $query,
Db\RowFactory $rowFactory,
Db\DataTypeParser $dataTypeParser,
array|NULL $dataTypesCache,
Expand Down
2 changes: 1 addition & 1 deletion src/Db/ResultFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ResultFactory
*/
function create(
PgSql\Result $queryResource,
Query $query,
PgQuery $query,
RowFactory $rowFactory,
DataTypeParser $dataTypeParser,
array|NULL $dataTypesCache,
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Sql/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Expression implements Db\Sql
/**
* @param list<mixed> $params
*/
public function __construct(string $value, array $params)
final public function __construct(string $value, array $params)
{
$this->sql = $value;
$this->params = $params;
Expand Down
Loading

0 comments on commit 103887b

Please sign in to comment.