Commit 009688d5 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Légère refacto pour pouvoir afficher dans le message d'erreur le nom de la vue...

Légère refacto pour pouvoir afficher dans le message d'erreur le nom de la vue dont la création échoue.
parent d43034c5
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -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,
+6 −2
Original line number Diff line number Diff line
@@ -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
+6 −6
Original line number Diff line number Diff line
@@ -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;
    }
+9 −7
Original line number Diff line number Diff line
@@ -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);
        }
    }