Commit fef816e0 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Déplacements dans Common.

parent de2c00aa
Loading
Loading
Loading
Loading
+12 −43
Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php: 12 added lines, 43 removed lines.
Original line number Diff line number Diff line
@@ -91,6 +91,11 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    abstract protected function generateFunctionBodyBeginningSQLSnippet();

    /**
     * @return string
     */
    abstract protected function generateFunctionBodyEndingSQLSnippet();

    /**
     * @param string $destinationTable
     * @param string $sourceCodeColumn
@@ -138,10 +143,10 @@ EOT;
            return 'src_' . $col;
        }, $columns);
        // instruction de mise à jour de la table destination
        $plsql .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ", inserted_on) VALUES (''', $quotedSrcSourceCodeColumn, '''";
        $plsql .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ", created_on) VALUES (', $quotedSrcSourceCodeColumn, '";
        foreach ($sourceCols as $col) {
            $quotedCol = $this->getQuoteLiteralFunctionCallSQLSnippet($col);
            $plsql .= ", ''', " . $quotedCol . ", '''";
            $plsql .= ", ', " . $quotedCol . ", '";
        }
        $plsql .= ", $now) ;');" . PHP_EOL;
        // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import
@@ -188,8 +193,8 @@ EOT;
             * PL/SQL construisant l'instruction SQL qui sera inscrite dans le registre d'import.
             */
            // instruction de mise à jour de la table destination
            $plsql .= "sql = concat('UPDATE $destinationTable SET $col = ''', $quotedSrcCol, ''', updated_on = $now " .
                "WHERE $sourceCodeColumn = ''', $quotedDestSourceCodeColumn, ''' ;');" . PHP_EOL;
            $plsql .= "sql = concat('UPDATE $destinationTable SET $col = ', $quotedSrcCol, ', updated_on = $now " .
                "WHERE $sourceCodeColumn = ', $quotedDestSourceCodeColumn, ' ;');" . PHP_EOL;
            // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import
            $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL;

@@ -246,9 +251,9 @@ EOT;
        $plsql .= "sql = concat('UPDATE $destinationTable SET ";
        foreach ($columns as $col) {
            $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col");
            $plsql .= "$col = ''', $quotedSrcCol, ''', ";
            $plsql .= "$col = ', $quotedSrcCol, ', ";
        }
        $plsql .= "updated_on = $now, deleted_on = null WHERE $sourceCodeColumn = ''', $quotedDestSourceCodeColumn, ''' ;');" . PHP_EOL;
        $plsql .= "updated_on = $now, deleted_on = null WHERE $sourceCodeColumn = ', $quotedDestSourceCodeColumn, ' ;');" . PHP_EOL;
        // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import
        $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL;

@@ -288,7 +293,7 @@ EOT;
         * PL/SQL construisant l'instruction SQL qui sera inscrite dans le registre d'import.
         */
        // instruction de mise à jour de la table destination
        $plsql .= "sql = concat('UPDATE $destinationTable SET deleted_on = $now WHERE $sourceCodeColumn = ''', $quotedDestSourceCodeColumn, ''' ;');" . PHP_EOL;
        $plsql .= "sql = concat('UPDATE $destinationTable SET deleted_on = $now WHERE $sourceCodeColumn = ', $quotedDestSourceCodeColumn, ' ;');" . PHP_EOL;
        // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import
        $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL;

@@ -314,42 +319,6 @@ EOT;
        return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', hash, ''' ;');";
    }

    /**
     * @return string
     */
    abstract protected function generateFunctionBodyEndingSQLSnippet();

    /**
     * @param $destinationTable
     * @param $sourceCodeColumn
     * @param $columns
     * @return string
     */
    abstract protected function generateSQLForInsertOperation($destinationTable, $sourceCodeColumn, $columns);

    /**
     * @param $destinationTable
     * @param $sourceCodeColumn
     * @param $field
     * @return string
     */
    abstract protected function generateSQLForUpdateOperation($destinationTable, $sourceCodeColumn, $field);

    /**
     * @param $destinationTable
     * @param $sourceCodeColumn
     * @param $columns
     * @return string
     */
    abstract protected function generateSQLForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns);

    /**
     * @param $destinationTable
     * @param $sourceCodeColumn
     * @return string
     */
    abstract protected function generateSQLForDeleteOperation($destinationTable, $sourceCodeColumn);

    /**
     * @param string $destinationTable
     * @param string $sourceCodeColumn
+0 −77
Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCreationHelper.php: 0 added lines, 77 removed lines.
Original line number Diff line number Diff line
@@ -32,81 +32,4 @@ END; $Q$
LANGUAGE plpgsql;
EOT;
    }

    protected function generateSQLForInsertOperation($destinationTable, $sourceCodeColumn, $columns)
    {
        $cols = array_merge([$sourceCodeColumn], $columns);
        $sourceCols = array_map(function ($col) {
            return 'src_' . $col;
        }, $columns);

        $res = '';
        $res .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ") VALUES ($$', src_$sourceCodeColumn, '$$";
        foreach ($sourceCols as $col) {
            $res .= ", $$', " . $col . ", '$$";
        }
        $res .= ") ;');";

        // ajout systématique: instruction SQL permettant de marquer comme exécutée la ligne du registre d'import
        $res .= PHP_EOL;
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        $res .= $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn);

        return $res;
    }

    protected function generateSQLForUpdateOperation($destinationTable, $sourceCodeColumn, $field)
    {
        $res = '';
        $res .= "sql = concat('UPDATE $destinationTable SET $field = $$', src_$field, '$$, updated_on = LOCALTIMESTAMP(0) " .
            "WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');";

        // ajout systématique: instruction SQL permettant de marquer comme exécutée la ligne du registre d'import
        $res .= PHP_EOL;
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        $res .= $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field);

        return $res;
    }

    protected function generateSQLForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns)
    {
        $res = '';
        $res .= "sql = concat('UPDATE $destinationTable SET ";
        foreach ($columns as $col) {
            $res .= "$col = $$', src_$col, '$$, ";
        }
        $res .= "updated_on = LOCALTIMESTAMP(0), deleted_on = null WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');";

        // ajout systématique: instruction SQL permettant de marquer comme exécutée la ligne du registre d'import
        $res .= PHP_EOL;
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        $res .= $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn);

        return $res;
    }

    protected function generateSQLForDeleteOperation($destinationTable, $sourceCodeColumn)
    {
        $res = '';
        $res .= "sql = concat('UPDATE $destinationTable SET deleted_on = LOCALTIMESTAMP(0) WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');";

        // ajout systématique: instruction SQL permettant de marquer comme exécutée la ligne du registre d'import
        $res .= PHP_EOL;
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        $res .= $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn);

        return $res;
    }

    protected function generateRegisterExecutionDateUpdateConcat()
    {
        $now = $this->getPlatform()->getNowExpression();

        return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', hash, ''' ;');";
    }
}
 No newline at end of file
+6 −6
Changes for test/UnicaenDbImportUnitTest/CodeGenerator/PostgreSQL/Helper/expected_function_creation_sql.sql: 6 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ BEGIN
    IF (src_code IS NOT NULL AND dest_code IS NULL) THEN
        operation = 'insert';
        hash = concat(concat(src_code, '-'), import_hash);
        sql = concat('INSERT INTO ztemptable(code, libelle, debut_validite, fin_validite, inserted_on) VALUES (''', quote_literal(src_code), ''', ''', quote_literal(src_libelle), ''', ''', quote_literal(src_debut_validite), ''', ''', quote_literal(src_fin_validite), ''', LOCALTIMESTAMP(0)) ;');
        sql = concat('INSERT INTO ztemptable(code, libelle, debut_validite, fin_validite, created_on) VALUES (', quote_literal(src_code), ', ', quote_literal(src_libelle), ', ', quote_literal(src_debut_validite), ', ', quote_literal(src_fin_validite), ', LOCALTIMESTAMP(0)) ;');
        sql = concat(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;');
        INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) VALUES ('insert', 'ztemptable', src_code, null, null, null, sql, LOCALTIMESTAMP(0), hash);
    END IF;
@@ -24,7 +24,7 @@ BEGIN
        IF (src_libelle <> dest_libelle) THEN
            operation = 'update';
            hash = concat(concat(concat(concat(dest_code, '-'), dest_libelle), '-'), import_hash);
            sql = concat('UPDATE ztemptable SET libelle = ''', quote_literal(src_libelle), ''', updated_on = LOCALTIMESTAMP(0) WHERE code = ''', quote_literal(dest_code), ''' ;');
            sql = concat('UPDATE ztemptable SET libelle = ', quote_literal(src_libelle), ', updated_on = LOCALTIMESTAMP(0) WHERE code = ', quote_literal(dest_code), ' ;');
            sql = concat(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;');
            INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) VALUES ('update', 'ztemptable', src_code, 'libelle', src_libelle, dest_libelle, sql, LOCALTIMESTAMP(0), hash);
        END IF;
@@ -32,7 +32,7 @@ BEGIN
        IF (src_debut_validite <> dest_debut_validite) THEN
            operation = 'update';
            hash = concat(concat(concat(concat(dest_code, '-'), dest_debut_validite), '-'), import_hash);
            sql = concat('UPDATE ztemptable SET debut_validite = ''', quote_literal(src_debut_validite), ''', updated_on = LOCALTIMESTAMP(0) WHERE code = ''', quote_literal(dest_code), ''' ;');
            sql = concat('UPDATE ztemptable SET debut_validite = ', quote_literal(src_debut_validite), ', updated_on = LOCALTIMESTAMP(0) WHERE code = ', quote_literal(dest_code), ' ;');
            sql = concat(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;');
            INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) VALUES ('update', 'ztemptable', src_code, 'debut_validite', src_debut_validite, dest_debut_validite, sql, LOCALTIMESTAMP(0), hash);
        END IF;
@@ -40,7 +40,7 @@ BEGIN
        IF (src_fin_validite <> dest_fin_validite) THEN
            operation = 'update';
            hash = concat(concat(concat(concat(dest_code, '-'), dest_fin_validite), '-'), import_hash);
            sql = concat('UPDATE ztemptable SET fin_validite = ''', quote_literal(src_fin_validite), ''', updated_on = LOCALTIMESTAMP(0) WHERE code = ''', quote_literal(dest_code), ''' ;');
            sql = concat('UPDATE ztemptable SET fin_validite = ', quote_literal(src_fin_validite), ', updated_on = LOCALTIMESTAMP(0) WHERE code = ', quote_literal(dest_code), ' ;');
            sql = concat(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;');
            INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) VALUES ('update', 'ztemptable', src_code, 'fin_validite', src_fin_validite, dest_fin_validite, sql, LOCALTIMESTAMP(0), hash);
        END IF;
@@ -51,7 +51,7 @@ BEGIN
    IF (src_code IS NOT NULL AND dest_code IS NOT NULL and dest_deleted_on IS NOT NULL) THEN
        operation = 'undelete';
        hash = concat(concat(dest_code, '-'), import_hash);
        sql = concat('UPDATE ztemptable SET libelle = ''', quote_literal(src_libelle), ''', debut_validite = ''', quote_literal(src_debut_validite), ''', fin_validite = ''', quote_literal(src_fin_validite), ''', updated_on = LOCALTIMESTAMP(0), deleted_on = null WHERE code = ''', quote_literal(dest_code), ''' ;');
        sql = concat('UPDATE ztemptable SET libelle = ', quote_literal(src_libelle), ', debut_validite = ', quote_literal(src_debut_validite), ', fin_validite = ', quote_literal(src_fin_validite), ', updated_on = LOCALTIMESTAMP(0), deleted_on = null WHERE code = ', quote_literal(dest_code), ' ;');
        sql = concat(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;');
        INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) VALUES ('undelete', 'ztemptable', src_code, null, null, null, sql, LOCALTIMESTAMP(0), hash);
    END IF;
@@ -60,7 +60,7 @@ BEGIN
    IF (src_code IS NULL AND dest_code IS NOT NULL and dest_deleted_on IS NULL) THEN
        operation = 'delete';
        hash = concat(concat(dest_code, '-'), import_hash);
        sql = concat('UPDATE ztemptable SET deleted_on = LOCALTIMESTAMP(0) WHERE code = ''', quote_literal(dest_code), ''' ;');
        sql = concat('UPDATE ztemptable SET deleted_on = LOCALTIMESTAMP(0) WHERE code = ', quote_literal(dest_code), ' ;');
        sql = concat(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;');
        INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) VALUES ('delete', 'ztemptable', src_code, null, null, null, sql, LOCALTIMESTAMP(0), hash);
    END IF;