Skip to content

Commit

Permalink
version 1.0.1: BulkUpdate hack for booleans on postgres platform
Browse files Browse the repository at this point in the history
  • Loading branch information
peefodee committed Jul 19, 2019
1 parent 2348acc commit 98e94af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "6dreams/doctrine-bulk",
"description": "Simple classes that allows bulk operations on doctrine entities.",
"version": "1.0",
"version": "1.0.1",
"type": "library",
"license": "MIT",
"authors": [
Expand Down
21 changes: 15 additions & 6 deletions src/SixDreams/Bulk/BulkUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace SixDreams\Bulk;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManagerInterface;
use SixDreams\DTO\ColumnMetadata;
use SixDreams\Exceptions\CannotChangeWhereException;
use SixDreams\Exceptions\FieldNotFoundException;
use SixDreams\Exceptions\NullValueException;
Expand Down Expand Up @@ -172,6 +175,7 @@ public function execute(): int
public function getSQL(): array
{
$values = $this->values;
$platform = $this->manager->getConnection()->getDatabasePlatform();
if (!\count($values)) {
return [null, []];
}
Expand All @@ -182,7 +186,7 @@ public function getSQL(): array
$cases = $bindings = [];
$thenId = 0;
foreach ($values as $when => $entity) {
$whenEsc = $this->simpleValue($when);
$whenEsc = $this->simpleValue($when, $platform);
foreach ($fields as $field) {
if (null === $whenEsc) {
$bindings[':W' . $thenId] = [$when, $idMeta];
Expand All @@ -192,9 +196,9 @@ public function getSQL(): array
'WHEN %s = %s THEN %s',
$this->escape($idMeta->getName()),
$whenEsc ?? ':W' . $thenId,
$this->simpleValue($entity[$field][0]) ?? ':T' . $thenId
$this->simpleValue($entity[$field][0], $platform, $entity[$field][1]) ?? ':T' . $thenId
);
if ($this->simpleValue($entity[$field][0]) === null) {
if ($this->simpleValue($entity[$field][0], $platform, $entity[$field][1]) === null) {
$bindings[':T' . $thenId] = $entity[$field];
}
} else {
Expand All @@ -220,7 +224,7 @@ public function getSQL(): array
if ('' !== $criterias) {
$criterias .= ', ';
}
$critEsc = $this->simpleValue($criteria);
$critEsc = $this->simpleValue($criteria, $platform);
$criterias .= $critEsc ?? ':C' . $critId;
if (null === $critEsc) {
$bindings[':C' . $critId] = [$criteria, $idMeta];
Expand All @@ -243,12 +247,17 @@ public function getSQL(): array
* Check is value is simple (float, int, null) and return it's representation in SQL, otherwise return null (marker
* that value require binding).
*
* @param mixed $value
* @param mixed $value
* @param AbstractPlatform $platform
* @param ColumnMetadata $metadata
*
* @return float|int|string|null
*/
protected function simpleValue($value)
protected function simpleValue($value, AbstractPlatform $platform, ?ColumnMetadata $metadata = null)
{
if ($metadata && $platform->getName() === 'postgresql' && $metadata->getType() === Type::BOOLEAN) {
return $value ? 'true' : 'false';
}
if (null === $value) {
return 'NULL';
}
Expand Down

0 comments on commit 98e94af

Please sign in to comment.