Loading src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCallingHelper.php +21 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,8 @@ namespace UnicaenDbImport\CodeGenerator\Common\Helper; use Ramsey\Uuid\Uuid; /** * Génération de code SQL pour l'appel de la fonction "create_import_metarequest_for_{table}". * Loading Loading @@ -31,7 +33,25 @@ abstract class FunctionCallingHelper extends AbstractHelper * @param array $columnsToChar * @return string */ abstract public function generateSQL($functionName, $sourceCodeColumn, array $columns, array $columnsToChar); public function generateSQL($functionName, $sourceCodeColumn, array $columns, array $columnsToChar) { $sourceCols = array_merge([$sourceCodeColumn], $columns); $destinCols = array_merge([$sourceCodeColumn], $columns, ['deleted_on']); $sourceCols = $this->generateColumnNamesWithPrefix($sourceCols, 'src.', $columnsToChar); $destinCols = $this->generateColumnNamesWithPrefix($destinCols, 'dest.', $columnsToChar); $importHash = $this->importHash ?: Uuid::uuid1()->toString(); $res = ''; $res .= $functionName . '(' . PHP_EOL; $res .= $this->indent(4, implode(', ', $sourceCols) . ',') . PHP_EOL; $res .= $this->indent(4, implode(', ', $destinCols) . ',') . PHP_EOL; $res .= $this->indent(4, "'" . $importHash . "'") . PHP_EOL; $res .= ")"; return $res; } /** * @param array $columns Loading src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php +53 −14 Original line number Diff line number Diff line Loading @@ -122,11 +122,13 @@ abstract class FunctionCreationHelper extends AbstractHelper */ protected function generateInsertOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("src_$sourceCodeColumn", "'-'", 'import_hash'); $insertOperationTemplate = <<<EOT -- l'enregistrement existe dans la source mais pas dans la destination : il devra être ajouté IF (src_$sourceCodeColumn IS NOT NULL AND dest_$sourceCodeColumn IS NULL) THEN operation = 'insert'; hash = concat(concat(src_$sourceCodeColumn, '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -143,12 +145,18 @@ EOT; return 'src_' . $col; }, $columns); // instruction de mise à jour de la table destination $plsql .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ", created_on) VALUES (', $quotedSrcSourceCodeColumn, '"; $concatArgs = [ "'INSERT INTO $destinationTable(" . implode(', ', $cols) . ", created_on) VALUES ('", $quotedSrcSourceCodeColumn, "', '" ]; foreach ($sourceCols as $col) { $quotedCol = $this->getQuoteLiteralFunctionCallSQLSnippet($col); $plsql .= ", ', " . $quotedCol . ", '"; $concatArgs[] = $quotedCol; $concatArgs[] = "', '"; } $plsql .= ", $now) ;');" . PHP_EOL; $concatArgs[] = "'$now) ;'"; $plsql .= "sql = " . $this->getPlatform()->getConcatExpression(...$concatArgs) . " ;" . PHP_EOL; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; Loading @@ -175,11 +183,13 @@ EOT; { $updateOperationMarkup = ''; foreach ($columns as $col) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("dest_$sourceCodeColumn", "'-'", "dest_$col", "'-'", 'import_hash'); $template = <<<EOT -- '$col' doit être mis à jour IF (src_$col <> dest_$col) THEN operation = 'update'; hash = concat(concat(concat(concat(dest_$sourceCodeColumn, '-'), dest_$col), '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -193,8 +203,14 @@ 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 = " . $this->getPlatform()->getConcatExpression( "'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; Loading Loading @@ -231,11 +247,13 @@ EOT; */ protected function generateUndeleteOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("dest_$sourceCodeColumn", "'-'", 'import_hash'); $template = <<<EOT -- l'enregistrement existe dans la destination mais historisé : il sera dé-historisé IF (src_$sourceCodeColumn IS NOT NULL AND dest_$sourceCodeColumn IS NOT NULL and dest_deleted_on IS NOT NULL) THEN operation = 'undelete'; hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -248,12 +266,19 @@ 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 "; $concatArgs = [ "'UPDATE $destinationTable SET '", ]; foreach ($columns as $col) { $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col"); $plsql .= "$col = ', $quotedSrcCol, ', "; $concatArgs[] = "'$col = '"; $concatArgs[] = $quotedSrcCol; $concatArgs[] = "', '"; } $plsql .= "updated_on = $now, deleted_on = null WHERE $sourceCodeColumn = ', $quotedDestSourceCodeColumn, ' ;');" . PHP_EOL; $concatArgs[] = "'updated_on = $now, deleted_on = null WHERE $sourceCodeColumn = '"; $concatArgs[] = $quotedDestSourceCodeColumn; $concatArgs[] = "' ;'"; $plsql .= "sql = " . $this->getPlatform()->getConcatExpression(...$concatArgs) . " ;" . PHP_EOL; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; Loading @@ -276,11 +301,13 @@ EOT; */ protected function generateDeleteOperationMarkup($destinationTable, $sourceCodeColumn) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("dest_$sourceCodeColumn", "'-'", 'import_hash'); $deleteOperationTemplate = <<<EOT -- l'enregistrement existe dans la destination mais plus dans la source : il sera historisé IF (src_$sourceCodeColumn IS NULL AND dest_$sourceCodeColumn IS NOT NULL and dest_deleted_on IS NULL) THEN operation = 'delete'; hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -293,7 +320,11 @@ 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 = " . $this->getPlatform()->getConcatExpression( "'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; Loading @@ -314,9 +345,17 @@ EOT; */ protected function generateRegisterExecutionDateUpdateConcat() { $quotedHashExpr = $this->getQuoteLiteralFunctionCallSQLSnippet("hash"); $now = $this->getPlatform()->getNowExpression(); return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', hash, ''' ;');"; $concatExpr = $this->getPlatform()->getConcatExpression( "sql", "' UPDATE import_reg SET executed_on = $now WHERE import_hash = '", $quotedHashExpr, "' ;'" ); return "sql = " . $concatExpr . " ;"; } /** Loading src/UnicaenDbImport/CodeGenerator/PostgreSQL/CodeGenerator.php +1 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,7 @@ use UnicaenDbImport\Domain\SourceInterface; * Version PostgreSQL. * * @author Unicaen * @todo N'utiliser que des helpers de génération de code car quel est la différence entre CodeGenerator et un helper ? */ class CodeGenerator extends AbstractCodeGenerator { Loading src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php +0 −28 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ namespace UnicaenDbImport\CodeGenerator\PostgreSQL\Helper; use Ramsey\Uuid\Uuid; use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; /** Loading @@ -13,31 +12,4 @@ use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; class FunctionCallingHelper extends \UnicaenDbImport\CodeGenerator\Common\Helper\FunctionCallingHelper { use PostgreSQLCommonsTrait; /** * @param string $functionName * @param string $sourceCodeColumn * @param array $columns * @param array $columnsToChar * @return string */ public function generateSQL($functionName, $sourceCodeColumn, array $columns, array $columnsToChar) { $sourceCols = array_merge([$sourceCodeColumn], $columns); $destinCols = array_merge([$sourceCodeColumn], $columns, ['deleted_on']); $sourceCols = $this->generateColumnNamesWithPrefix($sourceCols, 'src.', $columnsToChar); $destinCols = $this->generateColumnNamesWithPrefix($destinCols, 'dest.', $columnsToChar); $importHash = $this->importHash ?: Uuid::uuid1()->toString(); $res = ''; $res .= $functionName . '(' . PHP_EOL; $res .= $this->indent(4, implode(', ', $sourceCols) . ',') . PHP_EOL; $res .= $this->indent(4, implode(', ', $destinCols) . ',') . PHP_EOL; $res .= $this->indent(4, "'" . $importHash . "'") . PHP_EOL; $res .= ")"; return $res; } } No newline at end of file test/UnicaenDbImportUnitTest/CodeGenerator/PostgreSQL/Helper/expected_function_creation_sql.sql +18 −18 Original line number Diff line number Diff line Loading @@ -12,9 +12,9 @@ BEGIN -- 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'; hash = concat(concat(src_code, '-'), import_hash); 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, ''' ;'); hash = src_code || '-' || import_hash; sql = '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 = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; Loading @@ -23,25 +23,25 @@ BEGIN -- 'libelle' doit être mis à jour 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || dest_libelle || '-' || import_hash; sql = 'UPDATE ztemptable SET libelle = ' || quote_literal(src_libelle) || ', updated_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; -- 'debut_validite' doit être mis à jour 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || dest_debut_validite || '-' || import_hash; sql = 'UPDATE ztemptable SET debut_validite = ' || quote_literal(src_debut_validite) || ', updated_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; -- 'fin_validite' doit être mis à jour 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || dest_fin_validite || '-' || import_hash; sql = 'UPDATE ztemptable SET fin_validite = ' || quote_literal(src_fin_validite) || ', updated_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; Loading @@ -50,18 +50,18 @@ BEGIN -- l'enregistrement existe dans la destination mais historisé : il sera dé-historisé 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || import_hash; sql = '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 = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; -- l'enregistrement existe dans la destination mais plus dans la source : il sera historisé 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || import_hash; sql = 'UPDATE ztemptable SET deleted_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; Loading Loading
src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCallingHelper.php +21 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,8 @@ namespace UnicaenDbImport\CodeGenerator\Common\Helper; use Ramsey\Uuid\Uuid; /** * Génération de code SQL pour l'appel de la fonction "create_import_metarequest_for_{table}". * Loading Loading @@ -31,7 +33,25 @@ abstract class FunctionCallingHelper extends AbstractHelper * @param array $columnsToChar * @return string */ abstract public function generateSQL($functionName, $sourceCodeColumn, array $columns, array $columnsToChar); public function generateSQL($functionName, $sourceCodeColumn, array $columns, array $columnsToChar) { $sourceCols = array_merge([$sourceCodeColumn], $columns); $destinCols = array_merge([$sourceCodeColumn], $columns, ['deleted_on']); $sourceCols = $this->generateColumnNamesWithPrefix($sourceCols, 'src.', $columnsToChar); $destinCols = $this->generateColumnNamesWithPrefix($destinCols, 'dest.', $columnsToChar); $importHash = $this->importHash ?: Uuid::uuid1()->toString(); $res = ''; $res .= $functionName . '(' . PHP_EOL; $res .= $this->indent(4, implode(', ', $sourceCols) . ',') . PHP_EOL; $res .= $this->indent(4, implode(', ', $destinCols) . ',') . PHP_EOL; $res .= $this->indent(4, "'" . $importHash . "'") . PHP_EOL; $res .= ")"; return $res; } /** * @param array $columns Loading
src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php +53 −14 Original line number Diff line number Diff line Loading @@ -122,11 +122,13 @@ abstract class FunctionCreationHelper extends AbstractHelper */ protected function generateInsertOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("src_$sourceCodeColumn", "'-'", 'import_hash'); $insertOperationTemplate = <<<EOT -- l'enregistrement existe dans la source mais pas dans la destination : il devra être ajouté IF (src_$sourceCodeColumn IS NOT NULL AND dest_$sourceCodeColumn IS NULL) THEN operation = 'insert'; hash = concat(concat(src_$sourceCodeColumn, '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -143,12 +145,18 @@ EOT; return 'src_' . $col; }, $columns); // instruction de mise à jour de la table destination $plsql .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ", created_on) VALUES (', $quotedSrcSourceCodeColumn, '"; $concatArgs = [ "'INSERT INTO $destinationTable(" . implode(', ', $cols) . ", created_on) VALUES ('", $quotedSrcSourceCodeColumn, "', '" ]; foreach ($sourceCols as $col) { $quotedCol = $this->getQuoteLiteralFunctionCallSQLSnippet($col); $plsql .= ", ', " . $quotedCol . ", '"; $concatArgs[] = $quotedCol; $concatArgs[] = "', '"; } $plsql .= ", $now) ;');" . PHP_EOL; $concatArgs[] = "'$now) ;'"; $plsql .= "sql = " . $this->getPlatform()->getConcatExpression(...$concatArgs) . " ;" . PHP_EOL; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; Loading @@ -175,11 +183,13 @@ EOT; { $updateOperationMarkup = ''; foreach ($columns as $col) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("dest_$sourceCodeColumn", "'-'", "dest_$col", "'-'", 'import_hash'); $template = <<<EOT -- '$col' doit être mis à jour IF (src_$col <> dest_$col) THEN operation = 'update'; hash = concat(concat(concat(concat(dest_$sourceCodeColumn, '-'), dest_$col), '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -193,8 +203,14 @@ 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 = " . $this->getPlatform()->getConcatExpression( "'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; Loading Loading @@ -231,11 +247,13 @@ EOT; */ protected function generateUndeleteOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("dest_$sourceCodeColumn", "'-'", 'import_hash'); $template = <<<EOT -- l'enregistrement existe dans la destination mais historisé : il sera dé-historisé IF (src_$sourceCodeColumn IS NOT NULL AND dest_$sourceCodeColumn IS NOT NULL and dest_deleted_on IS NOT NULL) THEN operation = 'undelete'; hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -248,12 +266,19 @@ 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 "; $concatArgs = [ "'UPDATE $destinationTable SET '", ]; foreach ($columns as $col) { $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col"); $plsql .= "$col = ', $quotedSrcCol, ', "; $concatArgs[] = "'$col = '"; $concatArgs[] = $quotedSrcCol; $concatArgs[] = "', '"; } $plsql .= "updated_on = $now, deleted_on = null WHERE $sourceCodeColumn = ', $quotedDestSourceCodeColumn, ' ;');" . PHP_EOL; $concatArgs[] = "'updated_on = $now, deleted_on = null WHERE $sourceCodeColumn = '"; $concatArgs[] = $quotedDestSourceCodeColumn; $concatArgs[] = "' ;'"; $plsql .= "sql = " . $this->getPlatform()->getConcatExpression(...$concatArgs) . " ;" . PHP_EOL; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; Loading @@ -276,11 +301,13 @@ EOT; */ protected function generateDeleteOperationMarkup($destinationTable, $sourceCodeColumn) { $hashConcatExpr = $this->getPlatform()->getConcatExpression("dest_$sourceCodeColumn", "'-'", 'import_hash'); $deleteOperationTemplate = <<<EOT -- l'enregistrement existe dans la destination mais plus dans la source : il sera historisé IF (src_$sourceCodeColumn IS NULL AND dest_$sourceCodeColumn IS NOT NULL and dest_deleted_on IS NULL) THEN operation = 'delete'; hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); hash = $hashConcatExpr; %s END IF; EOT; Loading @@ -293,7 +320,11 @@ 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 = " . $this->getPlatform()->getConcatExpression( "'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; Loading @@ -314,9 +345,17 @@ EOT; */ protected function generateRegisterExecutionDateUpdateConcat() { $quotedHashExpr = $this->getQuoteLiteralFunctionCallSQLSnippet("hash"); $now = $this->getPlatform()->getNowExpression(); return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', hash, ''' ;');"; $concatExpr = $this->getPlatform()->getConcatExpression( "sql", "' UPDATE import_reg SET executed_on = $now WHERE import_hash = '", $quotedHashExpr, "' ;'" ); return "sql = " . $concatExpr . " ;"; } /** Loading
src/UnicaenDbImport/CodeGenerator/PostgreSQL/CodeGenerator.php +1 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,7 @@ use UnicaenDbImport\Domain\SourceInterface; * Version PostgreSQL. * * @author Unicaen * @todo N'utiliser que des helpers de génération de code car quel est la différence entre CodeGenerator et un helper ? */ class CodeGenerator extends AbstractCodeGenerator { Loading
src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php +0 −28 Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ namespace UnicaenDbImport\CodeGenerator\PostgreSQL\Helper; use Ramsey\Uuid\Uuid; use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; /** Loading @@ -13,31 +12,4 @@ use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; class FunctionCallingHelper extends \UnicaenDbImport\CodeGenerator\Common\Helper\FunctionCallingHelper { use PostgreSQLCommonsTrait; /** * @param string $functionName * @param string $sourceCodeColumn * @param array $columns * @param array $columnsToChar * @return string */ public function generateSQL($functionName, $sourceCodeColumn, array $columns, array $columnsToChar) { $sourceCols = array_merge([$sourceCodeColumn], $columns); $destinCols = array_merge([$sourceCodeColumn], $columns, ['deleted_on']); $sourceCols = $this->generateColumnNamesWithPrefix($sourceCols, 'src.', $columnsToChar); $destinCols = $this->generateColumnNamesWithPrefix($destinCols, 'dest.', $columnsToChar); $importHash = $this->importHash ?: Uuid::uuid1()->toString(); $res = ''; $res .= $functionName . '(' . PHP_EOL; $res .= $this->indent(4, implode(', ', $sourceCols) . ',') . PHP_EOL; $res .= $this->indent(4, implode(', ', $destinCols) . ',') . PHP_EOL; $res .= $this->indent(4, "'" . $importHash . "'") . PHP_EOL; $res .= ")"; return $res; } } No newline at end of file
test/UnicaenDbImportUnitTest/CodeGenerator/PostgreSQL/Helper/expected_function_creation_sql.sql +18 −18 Original line number Diff line number Diff line Loading @@ -12,9 +12,9 @@ BEGIN -- 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'; hash = concat(concat(src_code, '-'), import_hash); 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, ''' ;'); hash = src_code || '-' || import_hash; sql = '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 = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; Loading @@ -23,25 +23,25 @@ BEGIN -- 'libelle' doit être mis à jour 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || dest_libelle || '-' || import_hash; sql = 'UPDATE ztemptable SET libelle = ' || quote_literal(src_libelle) || ', updated_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; -- 'debut_validite' doit être mis à jour 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || dest_debut_validite || '-' || import_hash; sql = 'UPDATE ztemptable SET debut_validite = ' || quote_literal(src_debut_validite) || ', updated_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; -- 'fin_validite' doit être mis à jour 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || dest_fin_validite || '-' || import_hash; sql = 'UPDATE ztemptable SET fin_validite = ' || quote_literal(src_fin_validite) || ', updated_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; Loading @@ -50,18 +50,18 @@ BEGIN -- l'enregistrement existe dans la destination mais historisé : il sera dé-historisé 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || import_hash; sql = '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 = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; -- l'enregistrement existe dans la destination mais plus dans la source : il sera historisé 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(sql, ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ''', hash, ''' ;'); hash = dest_code || '-' || import_hash; sql = 'UPDATE ztemptable SET deleted_on = LOCALTIMESTAMP(0) WHERE code = ' || quote_literal(dest_code) || ' ;' ; sql = sql || ' UPDATE import_reg SET executed_on = LOCALTIMESTAMP(0) WHERE import_hash = ' || quote_literal(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; Loading