Skip to content
Snippets Groups Projects
Commit acb8b583 authored by Florian Joriot's avatar Florian Joriot
Browse files

modification synchro avec découpage par valeur en sous ensemble

parent 1f26d911
No related branches found
No related tags found
No related merge requests found
Pipeline #28048 passed
......@@ -8,7 +8,8 @@ use Doctrine\ORM\EntityManager;
use DoctrineModule\Persistence\ProvidesObjectManager;
use RuntimeException;
class SqlHelperService {
class SqlHelperService
{
use ProvidesObjectManager;
public function executeRequeteRef(EntityManager $entityManager, string $sql, array $params): array
......@@ -25,10 +26,13 @@ class SqlHelperService {
echo "033[31mUn problème est survenu lors de l'utilisation de la base de donnée\033[0m";
throw new RuntimeException("Un problème est survenu [DBA_Exception]", 0, $e);
}
return $tmp;
}
public function fetch(EntityManager $entityManager, string $table, array $correspondance, string $type, string $id) : array
public function fetch(EntityManager $entityManager, string $table, array $correspondance, string $type, string $id, string $separatorColumn = null, string $separatorValue = null): array
{
$columns = [];
foreach ($correspondance as $s => $d) {
......@@ -39,34 +43,69 @@ class SqlHelperService {
if ($type === 'destination') $columns[] = 'source_id';
$sql = "select " . implode(" , ", $columns) . " from " . $table;
if($separatorColumn !== null && $separatorValue !== ''){
$sql .= " where " . $separatorColumn . " = '" . $separatorValue . "'";
}
$data = $this->executeRequeteRef($entityManager, $sql, []);
$values = [];
foreach ($data as $item) {
$values[$item[$id]] = $item;
}
return $values;
}
public function fetchValuesSeparator(EntityManager $entityManager, string $table, string $column): array
{
$sql = "select " . $column . " from " . $table . " group by " . $column . ' order by '.$column;
$values = $this->executeRequeteRef($entityManager, $sql, []);
return $values;
}
//todo que ce passe t'il pour les booleans
public function echapValue(?string $value): string
{
if ($value === null or $value === '') return "null";
return "'" . str_replace("'", "''", $value) . "'";
}
public function insert(EntityManager $entityManager, string $table, array $item, array $correspondance, ?string $source = null): void
{
$columns = []; foreach ($correspondance as $d) $columns[] = $d; $columns[] = "created_on";
$columns = [];
foreach ($correspondance as $d) {
$columns[] = $d;
}
$columns[] = "created_on";
if ($source !== null) $columns[] = 'source_id';
$values = []; foreach ($correspondance as $s => $d) $values[] = $this->echapValue($item[$s]); $values[] = "now()";
$values = [];
foreach ($correspondance as $s => $d) {
$values[] = $this->echapValue($item[$s]);
}
$values[] = "now()";
if ($source !== null) $values[] = "'" . $source . "'";
$sql = "insert into " . $table . " (" . implode(',', $columns) . ") values (" . implode(',', $values) . ")";
$this->executeRequeteRef($entityManager, $sql, []);
}
public function update(EntityManager $entityManager, string $table, array $item, array $correspondance, string $id, ?string $source = null): void
{
$values = []; foreach ($correspondance as $s => $d) $values[] = $d."=".$this->echapValue($item[$s]);
$values = [];
foreach ($correspondance as $s => $d) {
$values[] = $d . "=" . $this->echapValue($item[$s]);
}
if ($source !== null) $values[] = "source_id='" . $source . "'";
$values[] = "updated_on=now()";
$sql = "update " . $table . " set " . implode(" , ", $values) . " where id=:id";
......@@ -74,12 +113,15 @@ class SqlHelperService {
}
public function restore(EntityManager $entityManager, string $table, string $id): void
{
$sql = "update " . $table . " set deleted_on=NULL where id=:id";
$this->executeRequeteRef($entityManager, $sql, ["id" => $id]);
}
public function delete(EntityManager $entityManager, string $table, string $id): void
{
$sql = "update " . $table . " set deleted_on=now() where id=:id";
......
......@@ -6,26 +6,44 @@ use DateTime;
use Exception;
use UnicaenSynchro\Service\SqlHelper\SqlHelperServiceAwareTrait;
class SynchronisationService {
class SynchronisationService
{
use SqlHelperServiceAwareTrait;
private array $entityManagers = [];
public function setEntityManagers(array $entityManagers): void
{
$this->entityManagers = $entityManagers;
}
private array $configs = [];
public function setConfigs(array $configs): void
{
$this->configs = $configs;
}
public function getFromConfig(string $name, string $key)
{
if(isset($this->configs[$name][$key])) {
return $this->configs[$name][$key];
}
return null;
}
private function checkDifferences(array $itemSource, array $itemDestination, array $correspondance, ?string $source = null) : bool {
private function checkDifferences(array $itemSource, array $itemDestination, array $correspondance, ?string $source = null): bool
{
foreach ($correspondance as $idSource => $idCorrespondance) {
if ($itemSource[$idSource] != $itemDestination[$idCorrespondance]) {
// var_dump($itemSource[$idSource]);
......@@ -43,9 +61,12 @@ class SynchronisationService {
// die();
if ($itemDestination['source_id'] !== $source) return true;
}
return false;
}
public function synchronise(string $name): string
{
try {
......@@ -63,11 +84,59 @@ class SynchronisationService {
$id_source = $this->getFromConfig($name, 'id');
$source = $this->getFromConfig($name, 'source');
$id_destination = $correspondance[$id_source];
$separatorSource = $this->getFromConfig($name, 'separator');
if ($separatorSource != null && $separatorSource != "") {
$separatorValuesSource = $this->getSqlHelperService()->fetchValuesSeparator($orm_source, $table_source, $separatorSource);
$separatorValuesDestination = $this->getSqlHelperService()->fetchValuesSeparator($orm_destination, $table_destination, $correspondance[$separatorSource]);
$separatorValues = array_unique(array_merge(
array_map(function (array $a) use ($separatorSource) {
return $a[$separatorSource];
}, $separatorValuesSource),
array_map(function ($a) use ($correspondance, $separatorSource) {
return $a[$correspondance[$separatorSource]];
}, $separatorValuesDestination)
));
sort($separatorValues);
foreach ($separatorValues as $separatorValue) {
echo "Traitement " . $separatorValue . "\n";
$data_source = $this->getSqlHelperService()->fetch($orm_source, $table_source, $correspondance, 'source', $id_source, $separatorSource, $separatorValue);
$data_destination = $this->getSqlHelperService()->fetch($orm_destination, $table_destination, $correspondance, 'destination', $id_destination, $separatorSource, $separatorValue);
echo count($data_source) . " entrées dans les données sources.\n";
$this->doSynchronisation($name, $data_source, $data_destination);
}
} else {
$data_source = $this->getSqlHelperService()->fetch($orm_source, $table_source, $correspondance, 'source', $id_source);
$data_destination = $this->getSqlHelperService()->fetch($orm_destination, $table_destination, $correspondance, 'destination', $id_destination);
echo count($data_source) . " entrées dans les données sources.\n";
$this->doSynchronisation($name, $data_source, $data_destination);
}
flush();
$data_destination = $this->getSqlHelperService()->fetch($orm_destination, $table_destination, $correspondance, 'destination', $id_destination);
} catch (Exception $e) {
do {
echo "\033[31m" . $e->getMessage() . "\033[0m\n";
$e = $e->getPrevious();
} while ($e !== null);
die();
}
return "";
}
private function doSynchronisation(string $name, array $data_source, array $data_destination): void
{
$correspondance = $this->getFromConfig($name, 'correspondance');
$orm_destination = $this->entityManagers[$this->getFromConfig($name, 'orm_destination')];
$table_destination = $this->getFromConfig($name, 'table_destination');
$source = $this->getFromConfig($name, 'source');
$debut = new DateTime();
$data_destination_on = [];
$data_destination_off = [];
foreach ($data_destination as $item) {
......@@ -141,14 +210,6 @@ class SynchronisationService {
echo "Fin: " . $fin->format('d/m/y H:i:s:u') . "\n";
echo "Durée de la synchronisation: " . ($fin->diff($debut))->format('%H:%m:%s:%F') . "\n";
return "";
} catch (Exception $e) {
do {
echo "\033[31m".$e->getMessage() . "\033[0m\n";
$e = $e->getPrevious();
} while ($e !== null);
die();
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment