Skip to content

Commit

Permalink
PgQuery to Query
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed Nov 10, 2024
1 parent aabd986 commit dc1be3b
Show file tree
Hide file tree
Showing 28 changed files with 197 additions and 197 deletions.
8 changes: 4 additions & 4 deletions docs/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ You can get the query, that initiated a result with the `getQuery()` method:
```php
$result = $connection->query('DELETE FROM users WHERE id IN (?)', [1, 2]);
$query = $result->getQuery();
assert($query instanceof Forrest79\PhPgSql\Db\PgQuery);
assert($query instanceof Forrest79\PhPgSql\Db\Query);
```

Or you can get resource, that can be used with native `pg_*` functions with the `getResource()` method:
Expand Down Expand Up @@ -485,7 +485,7 @@ dump($cnt); // (integer) 7
```php
$departmentsQuery = Forrest79\PhPgSql\Db\Sql\Query::createArgs('SELECT id FROM departments WHERE id = ?', [1]);

$query = $departmentsQuery->toPgQuery();
$query = $departmentsQuery->toDbQuery();

dump($query->sql); // (string) 'SELECT id FROM departments WHERE id = $1'
dump($query->params); // (array) [1]
Expand All @@ -506,7 +506,7 @@ class MyOwnResult extends Forrest79\PhPgSql\Db\Result

class MyOwnResultFactory implements Forrest79\PhPgSql\Db\ResultFactory
{
public function create(PgSql\Result $queryResource, Forrest79\PhPgSql\Db\PgQuery $query, Forrest79\PhPgSql\Db\RowFactory $rowFactory, Forrest79\PhPgSql\Db\DataTypeParser $dataTypeParser, array|NULL $dataTypesCache): Forrest79\PhPgSql\Db\Result
public function create(PgSql\Result $queryResource, Forrest79\PhPgSql\Db\Query $query, Forrest79\PhPgSql\Db\RowFactory $rowFactory, Forrest79\PhPgSql\Db\DataTypeParser $dataTypeParser, array|NULL $dataTypesCache): Forrest79\PhPgSql\Db\Result
{
return new MyOwnResult($queryResource, $query, $rowFactory, $dataTypeParser, $dataTypesCache);
}
Expand Down Expand Up @@ -991,7 +991,7 @@ $connection->addOnClose(function (Forrest79\PhPgSql\Db\Connection $connection):
// this is call right before connection is closed...
});

$connection->addOnQuery(function (Forrest79\PhPgSql\Db\Connection $connection, Forrest79\PhPgSql\Db\PgQuery $query, float|NULL $timeNs, string|NULL $prepareStatementName): void {
$connection->addOnQuery(function (Forrest79\PhPgSql\Db\Connection $connection, Forrest79\PhPgSql\Db\Query $query, float|NULL $timeNs, string|NULL $prepareStatementName): void {
// $time === NULL for async queries, $prepareStatementName !== NULL for prepared statements queries
dump($query->sql); // (string) 'SELECT nick FROM users WHERE id = $1'
dump($query->params); // (array) [3]
Expand Down
2 changes: 1 addition & 1 deletion docs/fluent.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $query = $fluent
->select(['*'])
->from('users')
->where('id', 1)
->toPgQuery();
->toDbQuery();

dump($query->sql); // (string) 'SELECT * FROM users WHERE id = $1'
dump($query->params); // (array) [1]
Expand Down
6 changes: 3 additions & 3 deletions phpcs-ignores.neon
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ ignoreErrors:
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
count: 1
path: src/Db/PgQuery.php
path: src/Db/Query.php

-
sniff: SlevomatCodingStandard.Classes.ForbiddenPublicProperty.ForbiddenPublicProperty
message: Do not use public properties. Use method access instead.
count: 2
path: src/Db/PgQuery.php
path: src/Db/Query.php

-
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
Expand Down Expand Up @@ -289,7 +289,7 @@ ignoreErrors:

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "toPgQuery"
message: Visibility must be declared on method "toDbQuery"
count: 1
path: src/Db/Sql/IQuery.php

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,
PgQuery $query,
Query $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 PgQuery($this->query, $params);
$query = new Query($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 PgQuery($this->query, []),
new Query($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 PgQuery($this->query, []),
new Query($this->query, []),
($resource !== FALSE) ? (string) \pg_result_error($resource) : $this->connection->getLastError(),
);
}
Expand Down
6 changes: 3 additions & 3 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 PgQuery $query;
private Query $query;

private string|NULL $preparedStatementName;

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


public function getQuery(): PgQuery
public function getQuery(): Query
{
return $this->query;
}
Expand Down
24 changes: 12 additions & 12 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|PgQuery|Sql $sql, mixed ...$params): Result
public function query(string|Query|Sql $sql, mixed ...$params): Result
{
\assert(\array_is_list($params));
return $this->queryArgs($sql, $params);
Expand All @@ -294,9 +294,9 @@ public function query(string|PgQuery|Sql $sql, mixed ...$params): Result
* @throws Exceptions\ConnectionException
* @throws Exceptions\QueryException
*/
public function queryArgs(string|PgQuery|Sql $sql, array $params): Result
public function queryArgs(string|Query|Sql $sql, array $params): Result
{
$query = $this->prepareQuery(PgQuery::from($sql, $params));
$query = $this->prepareQuery(Query::from($sql, $params));

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

Expand Down Expand Up @@ -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 PgQuery($sql, []), $this->getLastError());
throw Exceptions\QueryException::queryFailed(new Query($sql, []), $this->getLastError());
}

if ($startTime !== NULL) {
$this->events->onQuery(new PgQuery($sql, []), \hrtime(TRUE) - $startTime);
$this->events->onQuery(new Query($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|PgQuery|Sql $sql, mixed ...$params): AsyncQuery
public function asyncQuery(string|Query|Sql $sql, mixed ...$params): AsyncQuery
{
\assert(\array_is_list($params));
return $this->asyncQueryArgs($sql, $params);
Expand All @@ -358,9 +358,9 @@ public function asyncQuery(string|PgQuery|Sql $sql, mixed ...$params): AsyncQuer
* @throws Exceptions\ConnectionException
* @throws Exceptions\QueryException
*/
public function asyncQueryArgs(string|PgQuery|Sql $sql, array $params): AsyncQuery
public function asyncQueryArgs(string|Query|Sql $sql, array $params): AsyncQuery
{
$query = $this->prepareQuery(PgQuery::from($sql, $params));
$query = $this->prepareQuery(Query::from($sql, $params));

$queryParams = $query->params;
if ($queryParams === []) {
Expand Down Expand Up @@ -395,7 +395,7 @@ public function asyncExecute(string $sql): static
}

if ($this->events->hasOnQuery()) {
$this->events->onQuery(new PgQuery($sql, []));
$this->events->onQuery(new Query($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 PgQuery($asyncExecuteQuery, []),
new Query($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 : PgQuery)
* @return ($query is string ? string : Query)
*/
protected function prepareQuery(string|PgQuery $query): string|PgQuery
protected function prepareQuery(string|Query $query): string|Query
{
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, PgQuery, int|float|NULL, string|NULL): void> function (Connection $connection, Query $query, int|float|NULL $timeNs, string|NULL $prepareStatementName) {} */
/** @var list<callable(Connection, Query, 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(PgQuery $query, float|NULL $timeNs = NULL, string|NULL $prepareStatementName = NULL): void
public function onQuery(Query $query, float|NULL $timeNs = NULL, string|NULL $prepareStatementName = NULL): void
{
foreach ($this->onQuery as $event) {
$event($this->connection, $query, $timeNs, $prepareStatementName);
Expand Down
14 changes: 7 additions & 7 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\PgQuery|NULL $query;
private Db\Query|NULL $query;


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


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


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


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


public static function preparedStatementQueryFailed(
string $preparedStatementName,
Db\PgQuery $query,
Db\Query $query,
string $error,
): self
{
Expand All @@ -59,7 +59,7 @@ public static function preparedStatementQueryFailed(

public static function asyncPreparedStatementQueryFailed(
string $preparedStatementName,
Db\PgQuery $query,
Db\Query $query,
string $error,
): self
{
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Exceptions/ResultException.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function fetchMutatorBadReturnType(string $column, mixed $value):
}


public static function noOtherAsyncResult(Db\PgQuery $query): self
public static function noOtherAsyncResult(Db\Query $query): self
{
return new self(\sprintf('No other result for async query \'%s\'.', $query->sql), self::NO_OTHER_ASYNC_RESULT);
}
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 PgQuery($this->query, $params);
$query = new Query($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 PgQuery($this->query, []),
new Query($this->query, []),
$this->connection->getLastError(),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Db/PgQuery.php → src/Db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Forrest79\PhPgSql\Db;

class PgQuery
class Query
{
public readonly string $sql;

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 PgQuery $query;
private Query $query;

private RowFactory $rowFactory;

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


public function getQuery(): PgQuery
public function getQuery(): Query
{
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, PgQuery $query): Result
public function build(PgSql\Result $resource, Query $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\PgQuery $query,
Db\Query $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,
PgQuery $query,
Query $query,
RowFactory $rowFactory,
DataTypeParser $dataTypeParser,
array|NULL $dataTypesCache,
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Sql/IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ interface IQuery extends Db\Sql
/**
* Create SQL query for pg_query_params function.
*/
function toPgQuery(): Db\PgQuery;
function toDbQuery(): Db\Query;

}
Loading

0 comments on commit dc1be3b

Please sign in to comment.