-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from compositephp/dev
0.3.3 setup
- Loading branch information
Showing
18 changed files
with
224 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/.github export-ignore | ||
/doc export-ignore | ||
/phpunit.xml export-ignore | ||
/tests export-ignore |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composite\DB\Exceptions; | ||
|
||
class LockException extends DbException | ||
{ | ||
} |
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,64 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composite\DB\MultiQuery; | ||
|
||
use Composite\DB\Exceptions\DbException; | ||
use Composite\DB\TableConfig; | ||
use Composite\Entity\AbstractEntity; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
|
||
class MultiSelect | ||
{ | ||
private readonly QueryBuilder $queryBuilder; | ||
|
||
public function __construct( | ||
Connection $connection, | ||
TableConfig $tableConfig, | ||
array $condition, | ||
) { | ||
$query = $connection->createQueryBuilder()->select('*')->from($tableConfig->tableName); | ||
/** @var class-string<AbstractEntity> $class */ | ||
$class = $tableConfig->entityClass; | ||
|
||
$pkColumns = []; | ||
foreach ($tableConfig->primaryKeys as $primaryKeyName) { | ||
$pkColumns[$primaryKeyName] = $class::schema()->getColumn($primaryKeyName); | ||
} | ||
|
||
if (count($pkColumns) === 1) { | ||
if (!array_is_list($condition)) { | ||
throw new DbException('Input argument $pkList must be list'); | ||
} | ||
/** @var \Composite\Entity\Columns\AbstractColumn $pkColumn */ | ||
$pkColumn = reset($pkColumns); | ||
$preparedPkValues = array_map(fn ($pk) => $pkColumn->uncast($pk), $condition); | ||
$query->andWhere($query->expr()->in($pkColumn->name, $preparedPkValues)); | ||
} else { | ||
$expressions = []; | ||
foreach ($condition as $i => $pkArray) { | ||
if (!is_array($pkArray)) { | ||
throw new DbException('For tables with composite keys, input array must consist associative arrays'); | ||
} | ||
$pkOrExpr = []; | ||
foreach ($pkArray as $pkName => $pkValue) { | ||
if (is_string($pkName) && isset($pkColumns[$pkName])) { | ||
$preparedPkValue = $pkColumns[$pkName]->cast($pkValue); | ||
$pkOrExpr[] = $query->expr()->eq($pkName, ':' . $pkName . $i); | ||
$query->setParameter($pkName . $i, $preparedPkValue); | ||
} | ||
} | ||
if ($pkOrExpr) { | ||
$expressions[] = $query->expr()->and(...$pkOrExpr); | ||
} | ||
} | ||
$query->where($query->expr()->or(...$expressions)); | ||
} | ||
$this->queryBuilder = $query; | ||
} | ||
|
||
public function getQueryBuilder(): QueryBuilder | ||
{ | ||
return $this->queryBuilder; | ||
} | ||
} |
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
Oops, something went wrong.