Commit 87c7ffd2 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Correction d'un bug lorsqu'un champ à synchroniser est null : on ajoute un coalesce.

parent 70effaba
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ abstract class FunctionCreationHelper extends AbstractHelper
        $res = '';
        $res .= $this->generateFunctionCreationSQLSnippet($destinationTable, $sourceCodeColumn, $columns) . PHP_EOL;
        $res .= $this->generateFunctionBodyBeginningSQLSnippet() . PHP_EOL;
        $res .= $this->indent(4, $this->generateFunctionArgsNormalizationSQLSnippet($columns)) . PHP_EOL;
        $res .= $this->indent(4, $this->generateFunctionBodyContentSQLSnippet($destinationTable, $sourceCodeColumn, $columns)) . PHP_EOL;
        $res .= $this->generateFunctionBodyEndingSQLSnippet();

@@ -86,6 +87,32 @@ abstract class FunctionCreationHelper extends AbstractHelper
        return $res;
    }

    /**
     * @param array $columns
     * @return string
     */
    protected function generateFunctionArgsNormalizationSQLSnippet(array $columns)
    {
        $sourceCols = array_map(function ($col) {
            return 'src_' . $col;
        }, $columns);
        $destinCols = array_map(function ($col) {
            return 'dest_' . $col;
        }, $columns);

        $coalescer = function ($col) {
            return sprintf("%s = coalesce(%s, 'null');", $col, $col);
        };

        $coalesceSourceColsSQLSnippet = array_map($coalescer, $sourceCols);
        $coalesceDestinColsSQLSnippet = array_map($coalescer, $destinCols);

        return
            "-- normalisation des valeurs d'entrée" . PHP_EOL .
            implode(PHP_EOL, $coalesceSourceColsSQLSnippet) . PHP_EOL .
            implode(PHP_EOL, $coalesceDestinColsSQLSnippet) . PHP_EOL;
    }

    /**
     * @return string
     */
+8 −0
Original line number Diff line number Diff line
@@ -9,6 +9,14 @@ DECLARE
    hash VARCHAR(255);
    sql TEXT;
BEGIN
    -- normalisation des valeurs d'entrée
    src_libelle = coalesce(src_libelle, 'null');
    src_debut_validite = coalesce(src_debut_validite, 'null');
    src_fin_validite = coalesce(src_fin_validite, 'null');
    dest_libelle = coalesce(dest_libelle, 'null');
    dest_debut_validite = coalesce(dest_debut_validite, 'null');
    dest_fin_validite = coalesce(dest_fin_validite, 'null');

    -- l'enregistrement existe dans la source mais pas dans la destination : il devra être ajouté
    IF (src_code IS NOT NULL AND dest_code IS NULL) THEN
        operation = 'insert';