Skip to content

Commit

Permalink
Update strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRethore committed Jul 26, 2024
1 parent 4c07a52 commit 16e4708
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ImportConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('sql')->defaultValue('')->end()
->end()
->end()
->scalarNode('update_load_indentifier')->defaultValue('')->end()
->scalarNode('update_copy_indentifier')->defaultValue('')->end()
->variableNode('non_updateable_fields')
->defaultValue([])
->end()
Expand Down
10 changes: 10 additions & 0 deletions ImportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ public function isDistinct(): bool
return $this->config['copy']['strategy_options']['distinct'];
}

public function getLoadIdentifier(): ?string
{
return $this->config['copy']['strategy_options']['update_load_indentifier'] ?: null;
}

public function getCopyIdentifier(): ?string
{
return $this->config['copy']['strategy_options']['update_copy_indentifier'] ?: null;
}

public function getTargetTablename(): string
{
return $this->config['copy']['target'];
Expand Down
73 changes: 73 additions & 0 deletions Strategy/UpdateStrategy.php
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;
}
}

0 comments on commit 16e4708

Please sign in to comment.