-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CountMethodCompiler generates SQL and passes it to SQLMethodCompiler
- Loading branch information
1 parent
16a7baf
commit 47e7481
Showing
8 changed files
with
107 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Method; | ||
|
||
use DataAccessKit\Attribute\Table; | ||
use DataAccessKit\Repository\Attribute\Count; | ||
use DataAccessKit\Repository\Attribute\Find; | ||
use DataAccessKit\Repository\Result; | ||
use DataAccessKit\Repository\ResultMethod; | ||
use LogicException; | ||
use function implode; | ||
use function sprintf; | ||
|
||
trait BuildWhereTrait | ||
{ | ||
private function buildWhere(ResultMethod $method, Table $table, Result $result, Find|Count $attribute): string | ||
{ | ||
$conditions = []; | ||
foreach ($method->reflection->getParameters() as $parameter) { | ||
$column = null; | ||
foreach ($table->columns as $candidate) { | ||
if ($candidate->reflection->getName() === $parameter->getName()) { | ||
$column = $candidate; | ||
break; | ||
} | ||
} | ||
if ($column === null) { | ||
throw new LogicException(sprintf( | ||
"Method [%s::%s] parameter [%s] does not match any property of [%s], and therefore cannot be used as a query condition.", | ||
$result->reflection->getName(), | ||
$method->reflection->getName(), | ||
$parameter->getName(), | ||
$result->repository->class, | ||
)); | ||
} | ||
|
||
$conditions[] = "{$attribute->alias}.{$column->name} = @{$parameter->getName()}"; | ||
} | ||
|
||
return implode(" AND ", $conditions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
data-access-kit/test/Repository/Fixture/CountRepositoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Fixture; | ||
|
||
use DataAccessKit\Repository\Attribute\Repository; | ||
|
||
#[Repository(Foo::class)] | ||
interface CountRepositoryInterface | ||
{ | ||
public function countByTitle(string $title): int; | ||
} |
28 changes: 28 additions & 0 deletions
28
data-access-kit/test/Repository/Snapshot/CompilerTest.testCompile.count.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DataAccessKit\Repository\Fixture; | ||
|
||
use DataAccessKit\PersistenceInterface; | ||
|
||
final class CountRepository implements CountRepositoryInterface | ||
{ | ||
public function __construct( | ||
private readonly PersistenceInterface $persistence, | ||
) | ||
{ | ||
} | ||
|
||
public function countByTitle( | ||
string $title, | ||
): int | ||
{ | ||
$result = $this->persistence->query(int::class, 'SELECT COUNT(*) FROM foos t WHERE t.title = ?', [$title]); | ||
$objects = iterator_to_array($result); | ||
if (count($objects) === 0) { | ||
throw new LogicException("Not found"); | ||
} else if (count($objects) > 1) { | ||
throw new LogicException("Multiple objects found"); | ||
} | ||
return $objects[0]; | ||
} | ||
} |