diff --git a/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php b/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php index c4d2d78ebcb5d8e7f214d17865d3647496c94036..fb7d0d2b69df5eb985f0b9b2cc7bed3addf099b7 100644 --- a/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php +++ b/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php @@ -304,13 +304,22 @@ abstract class CodeGenerator implements CodeGeneratorInterface throw CodeGeneratorException::methodToImplement(__METHOD__, get_called_class(), $this->platform); } + public function generateDiffViewName(string $destinationTable): string + { + return $this->tableHelper->generateDiffViewName($destinationTable); + } + /** * @param SourceInterface $source * @param DestinationInterface $destination + * @param string $diffViewName * @param string|null $intermediateTable * @return string */ - public function generateSQLForDiffViewCreation(SourceInterface $source, DestinationInterface $destination, string $intermediateTable = null): string + public function generateSQLForDiffViewCreation(SourceInterface $source, + DestinationInterface $destination, + string $diffViewName, + string $intermediateTable = null): string { $destinationTable = $destination->getTable(); $sourceCodeColumn = $source->getSourceCodeColumn(); @@ -330,6 +339,7 @@ abstract class CodeGenerator implements CodeGeneratorInterface $diffSourceTableName = $intermediateTable ?: $source->getTable(); return $this->tableHelper->generateSQLForDiffViewCreationOrUpdate( + $diffViewName, $diffSourceTableName, $destinationTable, $sourceCodeColumn, diff --git a/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php b/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php index a8472060da91b4ba2d693c7de6654c811949bd2f..8d19804c39df2e0308b9c3a21c857a418019c935 100644 --- a/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php +++ b/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php @@ -117,11 +117,15 @@ interface CodeGeneratorInterface /** * @param SourceInterface $source * @param DestinationInterface $destination - * @param string $intermediateTable + * @param string $diffViewName + * @param string|null $intermediateTable * @return string * @throws \Doctrine\DBAL\Exception */ - public function generateSQLForDiffViewCreation(SourceInterface $source, DestinationInterface $destination, string $intermediateTable): string; + public function generateSQLForDiffViewCreation(SourceInterface $source, + DestinationInterface $destination, + string $diffViewName, + string $intermediateTable = null): string; /** * @param SourceInterface $source diff --git a/src/UnicaenDbImport/CodeGenerator/Helper/TableHelper.php b/src/UnicaenDbImport/CodeGenerator/Helper/TableHelper.php index 0d1257983188ee5a4cfcd1dd4f9593200be3b76b..c17e34da4ed7761f5dd431fa674cd0433536116c 100644 --- a/src/UnicaenDbImport/CodeGenerator/Helper/TableHelper.php +++ b/src/UnicaenDbImport/CodeGenerator/Helper/TableHelper.php @@ -63,6 +63,7 @@ EOT; } /** + * @param string $diffViewName * @param string $diffSourceTableName * @param string $destinationTable * @param string $sourceCodeColumn @@ -70,20 +71,19 @@ EOT; * @param bool $includeIdColumn * @return string */ - public function generateSQLForDiffViewCreationOrUpdate(string $diffSourceTableName, + public function generateSQLForDiffViewCreationOrUpdate(string $diffViewName, + string $diffSourceTableName, string $destinationTable, string $sourceCodeColumn, array $columns, bool $includeIdColumn): string { - $name = $this->generateDiffViewName($destinationTable); - $res = ''; - if ($sql = $this->generateSQLForViewDropBeforeUpdate($name)) { + if ($sql = $this->generateSQLForViewDropBeforeUpdate($diffViewName)) { $res = $sql . ';' . PHP_EOL . PHP_EOL; } - $res .= "CREATE OR REPLACE VIEW $name AS " . PHP_EOL; + $res .= "CREATE OR REPLACE VIEW $diffViewName AS " . PHP_EOL; $res .= $this->indent(4, $this->generateDiffViewBodySelectSQLSnippet( $diffSourceTableName, $destinationTable, @@ -161,7 +161,7 @@ EOT; * @param string $destinationTable * @return string */ - protected function generateDiffViewName(string $destinationTable): string + public function generateDiffViewName(string $destinationTable): string { return 'V_DIFF_' . $destinationTable; } diff --git a/src/UnicaenDbImport/Service/Database/DatabaseService.php b/src/UnicaenDbImport/Service/Database/DatabaseService.php index fa17f32f77c1513e5f1fedf27cce999a78939c5e..f9a998fd05876ed52b1e65e826d4a6971af791f0 100644 --- a/src/UnicaenDbImport/Service/Database/DatabaseService.php +++ b/src/UnicaenDbImport/Service/Database/DatabaseService.php @@ -3,7 +3,7 @@ namespace UnicaenDbImport\Service\Database; use Doctrine\DBAL\Exception; -use UnicaenDbImport\Domain\Destination; +use Throwable; use UnicaenDbImport\Domain\DestinationInterface; use UnicaenDbImport\Domain\Operation; use UnicaenDbImport\Entity\Db\Service\ImportObserv\ImportObservServiceAwareTrait; @@ -337,20 +337,22 @@ class DatabaseService extends AbstractDatabaseService } /** - * @param string|null $intermediateTable * @throws \UnicaenDbImport\Service\Exception\DatabaseServiceException */ - public function recreateDiffView(?string $intermediateTable = null) + public function recreateDiffView(?string $intermediateTable = null): void { + $destinationTable = $this->destination->getTable(); + $diffViewName = $this->codeGenerator->generateDiffViewName($destinationTable); + try { - $sql = $this->codeGenerator->generateSQLForDiffViewCreation($this->source, $this->destination, $intermediateTable); - } catch (Exception $e) { - throw DatabaseServiceException::error("Erreur lors de la génération du SQL de création de la vue différentielle", $e); + $sql = $this->codeGenerator->generateSQLForDiffViewCreation($this->source, $this->destination, $diffViewName, $intermediateTable); + } catch (Throwable $e) { + throw DatabaseServiceException::error("Erreur lors de la génération du SQL de création de la vue différentielle '$diffViewName'", $e); } try { $this->queryExecutor->exec($sql, $this->destination->getConnection()); } catch (Exception $e) { - throw DatabaseServiceException::error("Erreur lors de la création de la vue différentielle", $e); + throw DatabaseServiceException::error("Erreur lors de la création de la vue différentielle '$diffViewName'", $e); } }