Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BC break] Add annotation type to specify return type #30

Merged
merged 2 commits into from
May 26, 2022
Merged
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
12 changes: 11 additions & 1 deletion src/Annotation/DbQuery.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.