Commit 2afc8fed authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Optimisation : pas besoin d'interroger la vue diff pour générer le SQL ;...

Optimisation : pas besoin d'interroger la vue diff pour générer le SQL ; AMélioration : une erreur lors des inserts d'une synchro par exemple ne stoppe plus les synchros.
parent fdaffa54
Loading
Loading
Loading
Loading
+33 −4
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use UnicaenDbImport\CodeGenerator\Helper\TableHelper;
use UnicaenDbImport\CodeGenerator\Helper\TableValidationHelper;
use UnicaenDbImport\Config\ConfigAwareTrait;
use UnicaenDbImport\Domain\DestinationInterface;
use UnicaenDbImport\Domain\Operation;
use UnicaenDbImport\Domain\ResultInterface;
use UnicaenDbImport\Domain\SourceInterface;

@@ -254,8 +255,8 @@ abstract class CodeGenerator implements CodeGeneratorInterface
    /**
     * @inheritDoc
     */
    public function generateSQLForDestinationUpdateFromDiffViewRequestResult(
        array $rows,
    public function generateSQLForDestinationOperation(
        $operation,
        SourceInterface $source,
        DestinationInterface $destination)
    {
@@ -264,12 +265,40 @@ abstract class CodeGenerator implements CodeGeneratorInterface
        $sourceCodeColumn = $source->getSourceCodeColumn();
        $columns = $source->getColumns();

        return $this->tableHelper->generateSQLForOperationInDestinationTableFromDiffViewRequestResult(
            $rows,
        // colonne ID écartée
        $columns = array_diff($columns, ['ID', 'id']);

        switch ($operation) {
            case Operation::OPERATION_INSERT;
                $sql = $this->tableHelper->generateSQLForInsertOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns,
                        $idColumnSequence);
                break;
            case Operation::OPERATION_UPDATE;
                $sql = $this->tableHelper->generateSQLForUpdateOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns);
                break;
            case Operation::OPERATION_DELETE;
                $sql = $this->tableHelper->generateSQLForDeleteOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns);
                break;
            case Operation::OPERATION_UNDELETE;
                $sql = $this->tableHelper->generateSQLForUndeleteOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns);
                break;
            default:
                throw new RuntimeException("Opération inattendue");
        }

        return $sql . PHP_EOL;
    }

    /**
+4 −4
Original line number Diff line number Diff line
@@ -124,13 +124,13 @@ interface CodeGeneratorInterface
    public function generateSQLForDiffViewSelect(SourceInterface $source, DestinationInterface $destination);

    /**
     * @param array                $rows
     * @param string $operation
     * @param SourceInterface $source
     * @param DestinationInterface $destination
     * @return string
     * @return string[]
     */
    public function generateSQLForDestinationUpdateFromDiffViewRequestResult(
        array $rows,
    public function generateSQLForDestinationOperation(
        $operation,
        SourceInterface $source,
        DestinationInterface $destination);

+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ EOT;
                throw new RuntimeException("Type de résultat inattendu");
                break;
        }
        $success = $result->getException() ? 0 : 1;
        $success = $result->getFailure() ? 0 : 1;
        $startDate = $result->getStartDate()->format("Y-m-d H:i:s");
        $endDate = $result->getEndDate()->format("Y-m-d H:i:s");

+24 −10
Original line number Diff line number Diff line
@@ -243,7 +243,7 @@ EOT;
     * @param string $sourceCodeColumn
     * @param string[] $columns
     * @param string|null|false $idColumnSequence
     * @return string
     * @return string[]
     */
    public function generateSQLForOperationInDestinationTableFromDiffViewRequestResult(
        array $rows,
@@ -272,16 +272,29 @@ EOT;
        foreach ($operations as $operation) {
            switch ($operation) {
                case Operation::OPERATION_INSERT:
                    $sqls[] = $this->generateSQLForInsertOperationInDestinationTable($destinationTable, $sourceCodeColumn, $columns, $idColumnSequence) . ';' . PHP_EOL;
                    $sqls[] = $this->generateSQLForInsertOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns,
                        $idColumnSequence) . PHP_EOL;
                    break;
                case Operation::OPERATION_UPDATE:
                    $sqls[] = $this->generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, $columns) . ';' . PHP_EOL;
                    $sqls[] = $this->generateSQLForUpdateOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns) . PHP_EOL;
                    break;
                case Operation::OPERATION_UNDELETE:
                    $sqls[] = $this->generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, $columns) . ';' . PHP_EOL;
                    $sqls[] = $this->generateSQLForUndeleteOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns) . PHP_EOL;
                    break;
                case Operation::OPERATION_DELETE:
                    $sqls[] = $this->generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, $columns) . ';' . PHP_EOL;
                    $sqls[] = $this->generateSQLForDeleteOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns) . PHP_EOL;
                    break;
                default:
                    throw new RuntimeException("Opération inconnue rencontrée : " . $operation);
@@ -289,7 +302,8 @@ EOT;
            }
        }

        return empty($sqls) ? '' : implode(PHP_EOL, $sqls);
//        return empty($sqls) ? '' : implode(PHP_EOL, $sqls);
        return $sqls;
    }

    /**
@@ -363,7 +377,7 @@ EOT;
     * @param string|null|false $idColumnSequence
     * @return string
     */
    protected function generateSQLForInsertOperationInDestinationTable(
    public function generateSQLForInsertOperationInDestinationTable(
        $destinationTable,
        $sourceCodeColumn,
        array $columns,
@@ -502,7 +516,7 @@ EOT;
     * @param array $columns
     * @return string
     */
    abstract protected function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
    abstract public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);

    /**
     * @param string $destinationTable
@@ -510,7 +524,7 @@ EOT;
     * @param array $columns
     * @return string
     */
    abstract protected function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
    abstract public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);

    /**
     * @param string $destinationTable
@@ -518,5 +532,5 @@ EOT;
     * @param array $columns
     * @return string
     */
    abstract protected function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
    abstract public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
}
 No newline at end of file
+0 −23
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@
namespace UnicaenDbImport\CodeGenerator\Oracle;

use UnicaenDbImport\CodeGenerator\Oracle;
use UnicaenDbImport\Domain\DestinationInterface;
use UnicaenDbImport\Domain\SourceInterface;
use UnicaenDbImport\Platforms\OraclePlatform;

/**
@@ -42,27 +40,6 @@ class CodeGenerator extends \UnicaenDbImport\CodeGenerator\CodeGenerator
            'END;';
    }

    /**
     * @inheritDoc
     */
    public function generateSQLForDestinationUpdateFromDiffViewRequestResult(
        array $rows,
        SourceInterface $source,
        DestinationInterface $destination)
    {
        $sql = parent::generateSQLForDestinationUpdateFromDiffViewRequestResult($rows, $source, $destination);

        if ($sql === '') {
            return '';
        }

        // les ; séparant les INSERT/MERGE perturbent le driver Oracle donc on met ça dans un bloc BEGIN END et miracle...
        return
            'BEGIN ' . PHP_EOL .
            $sql . PHP_EOL .
            'END;';
    }

    /**
     * @inheritDoc
     */
Loading