Skip to content

Commit

Permalink
WIP [tests + doc]
Browse files Browse the repository at this point in the history
  • Loading branch information
forrest79 committed May 14, 2024
1 parent bdbe7b7 commit 64187a5
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 54 deletions.
52 changes: 32 additions & 20 deletions phpcs-ignores.neon
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ ignoreErrors:
count: 3
path: src/Db/Result.php

-
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
count: 1
path: src/Db/ResultFactories/Basic.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "createResult"
count: 1
path: src/Db/ResultFactory.php

-
sniff: SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
message: All classes should be declared using either the "abstract" or "final" keyword.
Expand Down Expand Up @@ -359,6 +371,18 @@ ignoreErrors:
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "doNothing"
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "doUpdate"
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "except"
Expand Down Expand Up @@ -479,18 +503,6 @@ ignoreErrors:
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "doUpdate"
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "doNothing"
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "orderBy"
Expand Down Expand Up @@ -613,13 +625,13 @@ ignoreErrors:

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "whereIf"
message: Visibility must be declared on method "whereAnd"
count: 1
path: src/Fluent/Sql.php

-
sniff: Squiz.Scope.MethodScope.Missing
message: Visibility must be declared on method "whereAnd"
message: Visibility must be declared on method "whereIf"
count: 1
path: src/Fluent/Sql.php

Expand Down Expand Up @@ -672,22 +684,22 @@ ignoreErrors:
path: tests/Integration/CollectingResultsTest.php

-
sniff: PSR1.Files.SideEffects.FoundWithSymbols
message: 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 15 and the first side effect is on line 9.'
sniff: SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
message: Unused parameter $connection.
count: 1
path: tests/Integration/CustomPrepareQueryTest.php
path: tests/Integration/CollectingResultsTest.php

-
sniff: SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
message: Unused parameter $connection.
sniff: PSR1.Files.SideEffects.FoundWithSymbols
message: 'A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 15 and the first side effect is on line 9.'
count: 1
path: tests/Integration/CustomPrepareQueryTest.php

-
sniff: SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
message: Unused parameter $connection.
count: 1
path: tests/Integration/CollectingResultsTest.php
path: tests/Integration/CustomPrepareQueryTest.php

-
sniff: PSR1.Files.SideEffects.FoundWithSymbols
Expand Down
8 changes: 6 additions & 2 deletions src/Db/AsyncHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ public function __construct(Connection $connection)
}


public function createAndSetAsyncQuery(Query $query, string|NULL $preparedStatementName = NULL): AsyncQuery
public function createAndSetAsyncQuery(
ResultFactory $resultFactory,
Query $query,
string|NULL $preparedStatementName = NULL,
): AsyncQuery
{
$this->asyncQuery = new AsyncQuery($this->connection, $this, $query, $preparedStatementName);
$this->asyncQuery = new AsyncQuery($this->connection, $resultFactory, $this, $query, $preparedStatementName);
$this->asyncExecuteQuery = NULL;

return $this->asyncQuery;
Expand Down
12 changes: 9 additions & 3 deletions src/Db/AsyncPreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ class AsyncPreparedStatement extends PreparedStatementHelper
private AsyncHelper $asyncHelper;


public function __construct(Connection $connection, AsyncHelper $asyncHelper, Events $events, string $query)
public function __construct(
AsyncHelper $asyncHelper,
Connection $connection,
ResultFactory $resultFactory,
Events $events,
string $query,
)
{
parent::__construct($connection, $events, $query);
parent::__construct($connection, $resultFactory, $events, $query);
$this->asyncHelper = $asyncHelper;
}

Expand Down Expand Up @@ -45,7 +51,7 @@ public function executeArgs(array $params): AsyncQuery
$this->events->onQuery($query, NULL, $statementName);
}

return $this->asyncHelper->createAndSetAsyncQuery($query, $statementName);
return $this->asyncHelper->createAndSetAsyncQuery($this->resultFactory, $query, $statementName);
}


Expand Down
6 changes: 5 additions & 1 deletion src/Db/AsyncQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class AsyncQuery
{
private Connection $connection;

private ResultFactory $resultFactory;

private AsyncHelper $asyncHelper;

private Query $query;
Expand All @@ -15,12 +17,14 @@ class AsyncQuery

public function __construct(
Connection $connection,
ResultFactory $resultFactory,
AsyncHelper $asyncHelper,
Query $query,
string|NULL $preparedStatementName = NULL,
)
{
$this->connection = $connection;
$this->resultFactory = $resultFactory;
$this->asyncHelper = $asyncHelper;
$this->query = $query;
$this->preparedStatementName = $preparedStatementName;
Expand Down Expand Up @@ -67,7 +71,7 @@ public function getNextResult(): Result
}
}

return $this->connection->createResult($result, $this->getQuery());
return $this->resultFactory->createResult($result, $this->getQuery());
}

}
53 changes: 27 additions & 26 deletions src/Db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Connection

private Events $events;

private ResultFactory|NULL $resultFactory = NULL;

private RowFactory|NULL $defaultRowFactory = NULL;

private DataTypeParser|NULL $dataTypeParser = NULL;
Expand Down Expand Up @@ -239,6 +241,24 @@ public function close(): self
}


public function setResultFactory(ResultFactory $resultFactory): self
{
$this->resultFactory = $resultFactory;

return $this;
}


private function getResultFactory(): ResultFactory
{
if ($this->resultFactory === NULL) {
$this->resultFactory = new ResultFactories\Basic($this, $this->events);
}

return $this->resultFactory;
}


public function setDefaultRowFactory(RowFactory $rowFactory): self
{
$this->defaultRowFactory = $rowFactory;
Expand All @@ -247,7 +267,7 @@ public function setDefaultRowFactory(RowFactory $rowFactory): self
}


private function getDefaultRowFactory(): RowFactory
public function getDefaultRowFactory(): RowFactory
{
if ($this->defaultRowFactory === NULL) {
$this->defaultRowFactory = new RowFactories\Basic();
Expand All @@ -265,7 +285,7 @@ public function setDataTypeParser(DataTypeParser $dataTypeParser): self
}


private function getDataTypeParser(): DataTypeParser
public function getDataTypeParser(): DataTypeParser
{
if ($this->dataTypeParser === NULL) {
$this->dataTypeParser = new DataTypeParsers\Basic();
Expand All @@ -286,7 +306,7 @@ public function setDataTypeCache(DataTypeCache $dataTypeCache): self
/**
* @return array<int, string>|NULL
*/
private function getDataTypesCache(): array|NULL
public function getDataTypesCache(): array|NULL
{
if ($this->dataTypeCache !== NULL) {
return $this->dataTypeCache->load($this);
Expand Down Expand Up @@ -333,26 +353,7 @@ public function queryArgs(string|Query|Sql\Query $sql, array $params): Result
$this->events->onQuery($query, \hrtime(TRUE) - $startTime);
}

return $this->createResult($resource, $query);
}


/**
* @internal
*/
public function createResult(PgSql\Result $resource, Query $query): Result
{
$result = new Result(
$resource,
$query,
$this->getDefaultRowFactory(),
$this->getDataTypeParser(),
$this->getDataTypesCache(),
);

$this->events->onResult($result);

return $result;
return $this->getResultFactory()->createResult($resource, $query);
}


Expand Down Expand Up @@ -414,7 +415,7 @@ public function asyncQueryArgs(string|Query|Sql\Query $sql, array $params): Asyn
$this->events->onQuery($query);
}

return $this->asyncHelper->createAndSetAsyncQuery($query);
return $this->asyncHelper->createAndSetAsyncQuery($this->getResultFactory(), $query);
}


Expand Down Expand Up @@ -484,13 +485,13 @@ public function cancelAsyncQuery(): self

public function prepareStatement(string $sql): PreparedStatement
{
return new PreparedStatement($this, $this->events, $this->prepareQuery($sql));
return new PreparedStatement($this, $this->getResultFactory(), $this->events, $this->prepareQuery($sql));
}


public function asyncPrepareStatement(string $sql): AsyncPreparedStatement
{
return new AsyncPreparedStatement($this, $this->asyncHelper, $this->events, $this->prepareQuery($sql));
return new AsyncPreparedStatement($this->asyncHelper, $this, $this->getResultFactory(), $this->events, $this->prepareQuery($sql));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Db/PreparedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function executeArgs(array $params): Result
$this->events->onQuery($query, \hrtime(TRUE) - $startTime, $statementName);
}

return $this->connection->createResult($resource, $query);
return $this->resultFactory->createResult($resource, $query);
}


Expand Down
5 changes: 4 additions & 1 deletion src/Db/PreparedStatementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ abstract class PreparedStatementHelper

protected Connection $connection;

protected ResultFactory $resultFactory;

protected Events $events;

protected string $query;

protected string|NULL $statementName = NULL;


public function __construct(Connection $connection, Events $events, string $query)
public function __construct(Connection $connection, ResultFactory $resultFactory, Events $events, string $query)
{
$this->connection = $connection;
$this->resultFactory = $resultFactory;
$this->events = $events;
$this->query = $query;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Db/ResultFactories/Basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace Forrest79\PhPgSql\Db\ResultFactories;

use Forrest79\PhPgSql\Db;
use PgSql;

class Basic implements Db\ResultFactory
{
private Db\Connection $connection;

private Db\Events $events;


public function __construct(Db\Connection $connection, Db\Events $events)
{
$this->connection = $connection;
$this->events = $events;
}


public function createResult(PgSql\Result $resource, Db\Query $query): Db\Result
{
$result = new Db\Result(
$resource,
$query,
$this->connection->getDefaultRowFactory(),
$this->connection->getDataTypeParser(),
$this->connection->getDataTypesCache(),
);

$this->events->onResult($result);

return $result;
}

}
12 changes: 12 additions & 0 deletions src/Db/ResultFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Forrest79\PhPgSql\Db;

use PgSql;

interface ResultFactory
{

function createResult(PgSql\Result $resource, Query $query): Result;

}

0 comments on commit 64187a5

Please sign in to comment.