Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update strategy #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
SimonRethore marked this conversation as resolved.
Show resolved Hide resolved
->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;
SimonRethore marked this conversation as resolved.
Show resolved Hide resolved
}

public function getCopyIdentifier(): ?string
{
return $this->config['copy']['strategy_options']['update_copy_indentifier'] ?: null;
SimonRethore marked this conversation as resolved.
Show resolved Hide resolved
}

public function getTargetTablename(): string
{
return $this->config['copy']['target'];
Expand Down
155 changes: 155 additions & 0 deletions Strategy/UpdateStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace LePhare\Import\Strategy;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
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
{
$platform = $this->connection->getDatabasePlatform();

if ($platform instanceof MySQLPlatform) {
$rowCount = $this->mysqlCopy($resource);
} elseif ($platform instanceof PostgreSQLPlatform) {
$rowCount = $this->postgresqlCopy($resource);
} else {
throw new ImportException('update strategy is not implemented for '.get_class($platform));
}

return $rowCount;
}

public function mysqlCopy(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()));
SimonRethore marked this conversation as resolved.
Show resolved Hide resolved
}

$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) {
if ('NULL' !== strtoupper($tempColumn)) {
$setters[] = "
temp.$column = (SELECT $tempColumn FROM $tempTablename
WHERE $tempTablename.$tempIdentifier = temp.$destinationIdentifier)";
} else {
$setters[] = "
temp.$column = NULL";
}
}
}
}

$setters = implode(',', $setters);

$joins = $resource->getJoins();
$whereClause = 'WHERE temp.'.$destinationIdentifier.' IN (SELECT '.$tempIdentifier.' FROM '.$tempTablename.')';
if ($resource->getCopyCondition()) {
$whereClause .= ' AND '.$resource->getCopyCondition();
}

$sql = "UPDATE $tablename temp
$joins
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;
}

public function postgresqlCopy(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()));
SimonRethore marked this conversation as resolved.
Show resolved Hide resolved
}

$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) {
if ('NULL' !== strtoupper($tempColumn)) {
$setters[] = "
$column = (SELECT $tempColumn FROM $tempTablename
WHERE $tempTablename.$tempIdentifier = $tablename.$destinationIdentifier)";
} else {
$setters[] = "
$column = NULL";
}
}
}
}

$setters = implode(',', $setters);

$joins = $resource->getJoins();
$whereClause = 'WHERE '.$tablename.'.'.$destinationIdentifier.' IN (SELECT '.$tempIdentifier.' FROM '.$tempTablename.')';
if ($resource->getCopyCondition()) {
$whereClause .= ' AND '.$resource->getCopyCondition();
}

$sql = "UPDATE $tablename
SET $setters
FROM $tempTablename temp $joins
$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;
}
}