Skip to content

Commit

Permalink
Merge pull request #30 from ray-di/type
Browse files Browse the repository at this point in the history
[BC break] Add annotation type to specify return type
  • Loading branch information
koriym committed May 26, 2022
2 parents e79172d + f297305 commit f72320e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 26 deletions.
12 changes: 11 additions & 1 deletion src/Annotation/DbQuery.php
Expand Up @@ -21,9 +21,19 @@ final class DbQuery
/** @var string */
public $entity;

public function __construct(string $id, string $entity = '')
/**
* @Enum({"row", "row_list", "exec"})
* @var 'row'|'row_list'|'exec'
*/
public $type = 'row_list';

/**
* @param 'row'|'row_list'|'exec' $type
*/
public function __construct(string $id, string $entity = '', string $type = 'row_list')
{
$this->id = $id;
$this->entity = $entity;
$this->type = $type;
}
}
16 changes: 7 additions & 9 deletions src/DbQueryInterceptor.php
Expand Up @@ -12,7 +12,6 @@

use function class_exists;
use function method_exists;
use function substr;

class DbQueryInterceptor implements MethodInterceptor
{
Expand Down Expand Up @@ -48,7 +47,7 @@ public function invoke(MethodInvocation $invocation)

$fetchStyle = $this->getFetchMode($dbQury);

return $this->sqlQuery($dbQury->id, $values, $fetchStyle, $dbQury->entity);
return $this->sqlQuery($dbQury, $values, $fetchStyle, $dbQury->entity);
}

/**
Expand All @@ -74,18 +73,17 @@ private function getFetchMode(DbQuery $dbQuery): int
*
* @return array<mixed>|object|null
*/
private function sqlQuery(string $queryId, array $values, int $fetchStyle, $fetchArg)
private function sqlQuery(DbQuery $dbQuery, array $values, int $fetchStyle, $fetchArg)
{
$postFix = substr($queryId, -4);
if ($postFix === 'list') {
return $this->sqlQuery->getRowList($queryId, $values, $fetchStyle, $fetchArg);
if ($dbQuery->type === 'row_list') {
return $this->sqlQuery->getRowList($dbQuery->id, $values, $fetchStyle, $fetchArg);
}

if ($postFix === 'item') {
return $this->sqlQuery->getRow($queryId, $values, $fetchStyle, $fetchArg);
if ($dbQuery->type === 'row') {
return $this->sqlQuery->getRow($dbQuery->id, $values, $fetchStyle, $fetchArg);
}

$this->sqlQuery->exec($queryId, $values);
$this->sqlQuery->exec($dbQuery->id, $values);

return [];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/DbQueryModuleTest.php
Expand Up @@ -52,7 +52,7 @@ protected function setUp(): void
]);
$sqlDir = dirname(__DIR__) . '/tests/sql';
$dbQueryConfig = new DbQueryConfig($sqlDir);
$module = new MediaQueryModule($mediaQueries, [$dbQueryConfig], new AuraSqlModule('sqlite::memory:', '', '', '', [PDO::ATTR_STRINGIFY_FETCHES => true])); // @phpstan-ignore-line
$module = new MediaQueryModule($mediaQueries, [$dbQueryConfig], new AuraSqlModule('sqlite::memory:', '', '', '', [PDO::ATTR_STRINGIFY_FETCHES => true]));
$this->injector = new Injector($module, __DIR__ . '/tmp');
$pdo = $this->injector->getInstance(ExtendedPdoInterface::class);
assert($pdo instanceof ExtendedPdoInterface);
Expand Down
4 changes: 2 additions & 2 deletions tests/Fake/Queries/PromiseItemInterface.php
Expand Up @@ -9,9 +9,9 @@
interface PromiseItemInterface
{
/**
* @DbQuery("promise_item")
* @DbQuery("promise_item", type="row")
* @return array{id: string, title: string, time: string}
*/
#[DbQuery('promise_item')]
#[DbQuery('promise_item', type:'row')]
public function get(string $id): array;
}
2 changes: 1 addition & 1 deletion tests/Fake/Queries/PromiseListInterface.php
Expand Up @@ -9,7 +9,7 @@
interface PromiseListInterface
{
/**
* @DbQuery("promise_list")
* @DbQuery("promise_list", type="row_list")
* @return array{id: string, title: string, time: string}
*/
#[DbQuery('promise_list')]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fake/Queries/TodoConstcuctEntityInterface.php
Expand Up @@ -11,7 +11,7 @@
interface TodoConstcuctEntityInterface
{
/**
* @DbQuery(id="todo_item", entity=TodoConstruct::class)
* @DbQuery(id="todo_item", entity=TodoConstruct::class, type="row")
*/
#[DbQuery('todo_item', entity: TodoConstruct::class)]
public function getItem(string $id): TodoConstruct;
Expand Down
4 changes: 2 additions & 2 deletions tests/Fake/Queries/TodoEntityInterface.php
Expand Up @@ -10,9 +10,9 @@
interface TodoEntityInterface
{
/**
* @DbQuery(id="todo_item", entity=Todo::class)
* @DbQuery(id="todo_item", entity=Todo::class, type="row")
*/
#[DbQuery('todo_item', entity: Todo::class)]
#[DbQuery('todo_item', entity: Todo::class, type:'row')]
public function getItem(string $id): Todo;

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Fake/Queries/TodoItemInterface.php
Expand Up @@ -9,9 +9,9 @@
interface TodoItemInterface
{
/**
* @DbQuery("todo_item")
* @DbQuery("todo_item", type="row")
* @return array{id: string, title: string}
*/
#[DbQuery('todo_item')]
#[DbQuery('todo_item', type:'row')]
public function __invoke(string $id): array;
}
14 changes: 7 additions & 7 deletions vendor-bin/tools/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f72320e

Please sign in to comment.