Loading src/UnicaenDbImport/CodeGenerator/Common/Helper/AbstractHelper.php +6 −0 Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/AbstractHelper.php: 6 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -16,6 +16,12 @@ abstract class AbstractHelper */ abstract protected function getPlatform(); /** * @param string $argument * @return string */ abstract protected function getQuoteLiteralFunctionCallSQLSnippet($argument); /** * @param int $length * @param string $string Loading src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php +209 −121 Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php: 209 added lines, 121 removed lines. Original line number Diff line number Diff line Loading @@ -2,8 +2,6 @@ namespace UnicaenDbImport\CodeGenerator\Common\Helper; use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; /** * Génération de code SQL de création de la fonction "create_import_metarequest_for_{table}". * Loading @@ -13,8 +11,6 @@ use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; */ abstract class FunctionCreationHelper extends AbstractHelper { use PostgreSQLCommonsTrait; /** * @param string $destinationTable * @param string $sourceCodeColumn Loading Loading @@ -113,130 +109,52 @@ abstract class FunctionCreationHelper extends AbstractHelper return $res; } /** * @return string */ abstract protected function generateFunctionBodyEndingSQLSnippet(); /** * @param $destinationTable * @param $sourceCodeColumn * @param $columns * @return string */ abstract protected function generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns); /** * @param $destinationTable * @param $sourceCodeColumn * @param $field * @return string */ abstract protected function generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $field); /** * @param $destinationTable * @param $sourceCodeColumn * @param $columns * @return string */ abstract protected function generateMetaRequestAffectationForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns); /** * @param $destinationTable * @param $sourceCodeColumn * @return string */ abstract protected function generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn); /** * @param string $destinationTable * @param string $sourceCodeColumn * @param array $columns * @return string */ protected function generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn) protected function generateInsertOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $now = $this->getPlatform()->getNowExpression(); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('insert', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, import_hash);"; return $res; } $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); %s END IF; EOT; $plsql = ''; /** * @param string $destinationTable * @param string $sourceCodeColumn * @param $field * @return string */ protected function generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field) { $quotedSrcSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('update', '$destinationTable', src_$sourceCodeColumn, '$field', src_$field, dest_$field, sql, $now, import_hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string * PL/SQL construisant l'instruction SQL qui sera inscrite dans le registre d'import. */ protected function generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('undelete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, import_hash);"; return $res; $cols = array_merge([$sourceCodeColumn], $columns); $sourceCols = array_map(function ($col) { return 'src_' . $col; }, $columns); // instruction de mise à jour de la table destination $plsql .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ", inserted_on) VALUES (''', $quotedSrcSourceCodeColumn, '''"; foreach ($sourceCols as $col) { $quotedCol = $this->getQuoteLiteralFunctionCallSQLSnippet($col); $plsql .= ", ''', " . $quotedCol . ", '''"; } $plsql .= ", $now) ;');" . PHP_EOL; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string * PL/SQL inscrivant l'opération dans le registre d'import. */ protected function generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $plsql .= $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('delete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, import_hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @param array $columns * @return string */ protected function generateInsertOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $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'; %s %s END IF; EOT; $markup = sprintf($insertOperationTemplate, $this->indent(4, $this->generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn)) $this->indent(4, $plsql) ); return $markup; Loading @@ -256,13 +174,33 @@ EOT; -- '$col' doit être mis à jour IF (src_$col <> dest_$col) THEN operation = 'update'; %s hash = concat(concat(concat(concat(dest_$sourceCodeColumn, '-'), dest_$col), '-'), import_hash); %s END IF; EOT; $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col"); $quotedDestSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("dest_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $plsql = ''; /** * 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; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; /** * PL/SQL inscrivant l'opération dans le registre d'import. */ $plsql .= $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $col); $updateOperationMarkup .= sprintf($template, $this->indent(4, $this->generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $col)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $col)) $this->indent(4, $plsql) ); $updateOperationMarkup .= PHP_EOL; } Loading @@ -288,17 +226,39 @@ EOT; */ protected function generateUndeleteOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $undeleteOperationTemplate = <<<EOT $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'; %s hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); %s END IF; EOT; $markup = sprintf($undeleteOperationTemplate, $this->indent(4, $this->generateMetaRequestAffectationForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn)) $quotedDestSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("dest_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $plsql = ''; /** * 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 "; foreach ($columns as $col) { $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col"); $plsql .= "$col = ''', $quotedSrcCol, ''', "; } $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; /** * PL/SQL inscrivant l'opération dans le registre d'import. */ $plsql .= $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn); $markup = sprintf($template, $this->indent(4, $plsql) ); return $markup; Loading @@ -315,15 +275,143 @@ 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'; %s hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); %s END IF; EOT; $quotedDestSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("dest_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $plsql = ''; /** * 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; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; /** * PL/SQL inscrivant l'opération dans le registre d'import. */ $plsql .= $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn); $markup = sprintf($deleteOperationTemplate, $this->indent(4, $this->generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn)) $this->indent(4, $plsql) ); return $markup; } /** * @return string */ protected function generateRegisterExecutionDateUpdateConcat() { $now = $this->getPlatform()->getNowExpression(); 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 * @return string */ protected function generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('insert', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @param $field * @return string */ protected function generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('update', '$destinationTable', src_$sourceCodeColumn, '$field', src_$field, dest_$field, sql, $now, hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string */ protected function generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('undelete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string */ protected function generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('delete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, hash);"; return $res; } } No newline at end of file src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php +3 −3 Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php: 3 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -33,9 +33,9 @@ class FunctionCallingHelper extends \UnicaenDbImport\CodeGenerator\Common\Helper $res = ''; $res .= $functionName . '(' . PHP_EOL; $res .= ' ' . implode(', ', $sourceCols) . ',' . PHP_EOL; $res .= ' ' . implode(', ', $destinCols) . ',' . PHP_EOL; $res .= ' ' . '$$' . $importHash . '$$' . 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; Loading src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCreationHelper.php +14 −5 Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCreationHelper.php: 14 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ class FunctionCreationHelper extends \UnicaenDbImport\CodeGenerator\Common\Helpe $Q$ DECLARE operation VARCHAR(64); hash VARCHAR(255); sql TEXT; BEGIN EOT; Loading @@ -32,7 +33,7 @@ LANGUAGE plpgsql; EOT; } protected function generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns) protected function generateSQLForInsertOperation($destinationTable, $sourceCodeColumn, $columns) { $cols = array_merge([$sourceCodeColumn], $columns); $sourceCols = array_map(function ($col) { Loading @@ -50,10 +51,12 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn); return $res; } protected function generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $field) protected function generateSQLForUpdateOperation($destinationTable, $sourceCodeColumn, $field) { $res = ''; $res .= "sql = concat('UPDATE $destinationTable SET $field = $$', src_$field, '$$, updated_on = LOCALTIMESTAMP(0) " . Loading @@ -63,10 +66,12 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field); return $res; } protected function generateMetaRequestAffectationForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns) protected function generateSQLForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns) { $res = ''; $res .= "sql = concat('UPDATE $destinationTable SET "; Loading @@ -79,10 +84,12 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn); return $res; } protected function generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn) protected function generateSQLForDeleteOperation($destinationTable, $sourceCodeColumn) { $res = ''; $res .= "sql = concat('UPDATE $destinationTable SET deleted_on = LOCALTIMESTAMP(0) WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');"; Loading @@ -91,6 +98,8 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn); return $res; } Loading @@ -98,6 +107,6 @@ EOT; { $now = $this->getPlatform()->getNowExpression(); return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', import_hash, ''' ;');"; return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', hash, ''' ;');"; } } No newline at end of file src/UnicaenDbImport/CodeGenerator/PostgreSQL/PostgreSQLCommonsTrait.php +9 −0 Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/PostgreSQLCommonsTrait.php: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -19,4 +19,13 @@ trait PostgreSQLCommonsTrait return $this->platform; } /** * @param string $argument * @return string */ protected function getQuoteLiteralFunctionCallSQLSnippet($argument) { return 'quote_literal(' . $argument . ')'; } } No newline at end of file Loading
src/UnicaenDbImport/CodeGenerator/Common/Helper/AbstractHelper.php +6 −0 Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/AbstractHelper.php: 6 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -16,6 +16,12 @@ abstract class AbstractHelper */ abstract protected function getPlatform(); /** * @param string $argument * @return string */ abstract protected function getQuoteLiteralFunctionCallSQLSnippet($argument); /** * @param int $length * @param string $string Loading
src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php +209 −121 Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php: 209 added lines, 121 removed lines. Original line number Diff line number Diff line Loading @@ -2,8 +2,6 @@ namespace UnicaenDbImport\CodeGenerator\Common\Helper; use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; /** * Génération de code SQL de création de la fonction "create_import_metarequest_for_{table}". * Loading @@ -13,8 +11,6 @@ use UnicaenDbImport\CodeGenerator\PostgreSQL\PostgreSQLCommonsTrait; */ abstract class FunctionCreationHelper extends AbstractHelper { use PostgreSQLCommonsTrait; /** * @param string $destinationTable * @param string $sourceCodeColumn Loading Loading @@ -113,130 +109,52 @@ abstract class FunctionCreationHelper extends AbstractHelper return $res; } /** * @return string */ abstract protected function generateFunctionBodyEndingSQLSnippet(); /** * @param $destinationTable * @param $sourceCodeColumn * @param $columns * @return string */ abstract protected function generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns); /** * @param $destinationTable * @param $sourceCodeColumn * @param $field * @return string */ abstract protected function generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $field); /** * @param $destinationTable * @param $sourceCodeColumn * @param $columns * @return string */ abstract protected function generateMetaRequestAffectationForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns); /** * @param $destinationTable * @param $sourceCodeColumn * @return string */ abstract protected function generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn); /** * @param string $destinationTable * @param string $sourceCodeColumn * @param array $columns * @return string */ protected function generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn) protected function generateInsertOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $now = $this->getPlatform()->getNowExpression(); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('insert', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, import_hash);"; return $res; } $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); %s END IF; EOT; $plsql = ''; /** * @param string $destinationTable * @param string $sourceCodeColumn * @param $field * @return string */ protected function generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field) { $quotedSrcSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('update', '$destinationTable', src_$sourceCodeColumn, '$field', src_$field, dest_$field, sql, $now, import_hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string * PL/SQL construisant l'instruction SQL qui sera inscrite dans le registre d'import. */ protected function generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('undelete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, import_hash);"; return $res; $cols = array_merge([$sourceCodeColumn], $columns); $sourceCols = array_map(function ($col) { return 'src_' . $col; }, $columns); // instruction de mise à jour de la table destination $plsql .= "sql = concat('INSERT INTO $destinationTable(" . implode(', ', $cols) . ", inserted_on) VALUES (''', $quotedSrcSourceCodeColumn, '''"; foreach ($sourceCols as $col) { $quotedCol = $this->getQuoteLiteralFunctionCallSQLSnippet($col); $plsql .= ", ''', " . $quotedCol . ", '''"; } $plsql .= ", $now) ;');" . PHP_EOL; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string * PL/SQL inscrivant l'opération dans le registre d'import. */ protected function generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $plsql .= $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn); $res = ''; $res .= "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('delete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, import_hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @param array $columns * @return string */ protected function generateInsertOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $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'; %s %s END IF; EOT; $markup = sprintf($insertOperationTemplate, $this->indent(4, $this->generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn)) $this->indent(4, $plsql) ); return $markup; Loading @@ -256,13 +174,33 @@ EOT; -- '$col' doit être mis à jour IF (src_$col <> dest_$col) THEN operation = 'update'; %s hash = concat(concat(concat(concat(dest_$sourceCodeColumn, '-'), dest_$col), '-'), import_hash); %s END IF; EOT; $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col"); $quotedDestSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("dest_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $plsql = ''; /** * 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; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; /** * PL/SQL inscrivant l'opération dans le registre d'import. */ $plsql .= $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $col); $updateOperationMarkup .= sprintf($template, $this->indent(4, $this->generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $col)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $col)) $this->indent(4, $plsql) ); $updateOperationMarkup .= PHP_EOL; } Loading @@ -288,17 +226,39 @@ EOT; */ protected function generateUndeleteOperationMarkup($destinationTable, $sourceCodeColumn, array $columns) { $undeleteOperationTemplate = <<<EOT $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'; %s hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); %s END IF; EOT; $markup = sprintf($undeleteOperationTemplate, $this->indent(4, $this->generateMetaRequestAffectationForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn)) $quotedDestSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("dest_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $plsql = ''; /** * 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 "; foreach ($columns as $col) { $quotedSrcCol = $this->getQuoteLiteralFunctionCallSQLSnippet("src_$col"); $plsql .= "$col = ''', $quotedSrcCol, ''', "; } $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; /** * PL/SQL inscrivant l'opération dans le registre d'import. */ $plsql .= $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn); $markup = sprintf($template, $this->indent(4, $plsql) ); return $markup; Loading @@ -315,15 +275,143 @@ 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'; %s hash = concat(concat(dest_$sourceCodeColumn, '-'), import_hash); %s END IF; EOT; $quotedDestSourceCodeColumn = $this->getQuoteLiteralFunctionCallSQLSnippet("dest_$sourceCodeColumn"); $now = $this->getPlatform()->getNowExpression(); $plsql = ''; /** * 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; // suivie de l'instruction permettant de marquer comme exécutée la ligne du registre d'import $plsql .= $this->generateRegisterExecutionDateUpdateConcat() . PHP_EOL; /** * PL/SQL inscrivant l'opération dans le registre d'import. */ $plsql .= $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn); $markup = sprintf($deleteOperationTemplate, $this->indent(4, $this->generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn)), $this->indent(4, $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn)) $this->indent(4, $plsql) ); return $markup; } /** * @return string */ protected function generateRegisterExecutionDateUpdateConcat() { $now = $this->getPlatform()->getNowExpression(); 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 * @return string */ protected function generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('insert', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @param $field * @return string */ protected function generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('update', '$destinationTable', src_$sourceCodeColumn, '$field', src_$field, dest_$field, sql, $now, hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string */ protected function generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('undelete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, hash);"; return $res; } /** * @param string $destinationTable * @param string $sourceCodeColumn * @return string */ protected function generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn) { $now = $this->getPlatform()->getNowExpression(); $res = "INSERT INTO import_reg(operation, table_name, source_code, field_name, to_value, from_value, sql, created_on, import_hash) " . "VALUES ('delete', '$destinationTable', src_$sourceCodeColumn, null, null, null, sql, $now, hash);"; return $res; } } No newline at end of file
src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php +3 −3 Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php: 3 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -33,9 +33,9 @@ class FunctionCallingHelper extends \UnicaenDbImport\CodeGenerator\Common\Helper $res = ''; $res .= $functionName . '(' . PHP_EOL; $res .= ' ' . implode(', ', $sourceCols) . ',' . PHP_EOL; $res .= ' ' . implode(', ', $destinCols) . ',' . PHP_EOL; $res .= ' ' . '$$' . $importHash . '$$' . 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; Loading
src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCreationHelper.php +14 −5 Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCreationHelper.php: 14 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -19,6 +19,7 @@ class FunctionCreationHelper extends \UnicaenDbImport\CodeGenerator\Common\Helpe $Q$ DECLARE operation VARCHAR(64); hash VARCHAR(255); sql TEXT; BEGIN EOT; Loading @@ -32,7 +33,7 @@ LANGUAGE plpgsql; EOT; } protected function generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns) protected function generateSQLForInsertOperation($destinationTable, $sourceCodeColumn, $columns) { $cols = array_merge([$sourceCodeColumn], $columns); $sourceCols = array_map(function ($col) { Loading @@ -50,10 +51,12 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn); return $res; } protected function generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $field) protected function generateSQLForUpdateOperation($destinationTable, $sourceCodeColumn, $field) { $res = ''; $res .= "sql = concat('UPDATE $destinationTable SET $field = $$', src_$field, '$$, updated_on = LOCALTIMESTAMP(0) " . Loading @@ -63,10 +66,12 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field); return $res; } protected function generateMetaRequestAffectationForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns) protected function generateSQLForUndeleteOperation($destinationTable, $sourceCodeColumn, $columns) { $res = ''; $res .= "sql = concat('UPDATE $destinationTable SET "; Loading @@ -79,10 +84,12 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn); return $res; } protected function generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn) protected function generateSQLForDeleteOperation($destinationTable, $sourceCodeColumn) { $res = ''; $res .= "sql = concat('UPDATE $destinationTable SET deleted_on = LOCALTIMESTAMP(0) WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');"; Loading @@ -91,6 +98,8 @@ EOT; $res .= PHP_EOL; $res .= $this->generateRegisterExecutionDateUpdateConcat(); $res .= $this->generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn); return $res; } Loading @@ -98,6 +107,6 @@ EOT; { $now = $this->getPlatform()->getNowExpression(); return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', import_hash, ''' ;');"; return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', hash, ''' ;');"; } } No newline at end of file
src/UnicaenDbImport/CodeGenerator/PostgreSQL/PostgreSQLCommonsTrait.php +9 −0 Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/PostgreSQLCommonsTrait.php: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -19,4 +19,13 @@ trait PostgreSQLCommonsTrait return $this->platform; } /** * @param string $argument * @return string */ protected function getQuoteLiteralFunctionCallSQLSnippet($argument) { return 'quote_literal(' . $argument . ')'; } } No newline at end of file