-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c07a52
commit 16e4708
Showing
3 changed files
with
85 additions
and
0 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
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,73 @@ | ||
<?php | ||
|
||
namespace LePhare\Import\Strategy; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use LePhare\Import\Exception\ImportException; | ||
use LePhare\Import\ImportResource; | ||
|
||
class UpdateStrategy implements StrategyInterface | ||
{ | ||
protected Connection $connection; | ||
|
||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'update'; | ||
} | ||
|
||
public function copy(ImportResource $resource): int | ||
{ | ||
$tablename = $this->connection->quoteIdentifier($resource->getTargetTablename()); | ||
$tempTablename = $this->connection->quoteIdentifier($resource->getTablename()); | ||
|
||
$tempIdentifier = $resource->getLoadIdentifier(); | ||
$destinationIdentifier = $resource->getCopyIdentifier(); | ||
|
||
if (null === $tempIdentifier || null === $destinationIdentifier) { | ||
throw new ImportException(sprintf('Options update_load_indentifier and update_copy_indentifier are mandatory for %s strategy', $this->getName())); | ||
} | ||
|
||
$setters = []; | ||
foreach ($resource->getMapping() as $name => $properties) { | ||
foreach ($properties['property'] as $property) { | ||
$column = $this->connection->quoteIdentifier($property); | ||
$tempColumn = $properties['sql'] ?: $this->connection->quoteIdentifier($name); | ||
|
||
if ($column && $tempColumn) { | ||
$setters[] = " | ||
$column = (SELECT $tempColumn FROM $tempTablename | ||
WHERE $tempTablename.$tempIdentifier = $tablename.$destinationIdentifier)"; | ||
} | ||
} | ||
} | ||
|
||
$setters = implode(',', $setters); | ||
|
||
$whereClause = 'WHERE '.$destinationIdentifier.' IN (SELECT '.$tempIdentifier.' FROM '.$tempTablename.')'; | ||
if ($resource->getCopyCondition()) { | ||
$whereClause .= ' AND '.$resource->getCopyCondition(); | ||
} | ||
|
||
$sql = "UPDATE $tablename | ||
SET $setters | ||
$whereClause"; | ||
|
||
$this->connection->beginTransaction(); | ||
|
||
try { | ||
$stmt = $this->connection->executeQuery($sql); | ||
$lines = $stmt->rowCount(); | ||
$this->connection->commit(); | ||
} catch (\Exception $exception) { | ||
$this->connection->rollback(); | ||
$lines = 0; | ||
} | ||
|
||
return $lines; | ||
} | ||
} |