Loading src/UnicaenDbImport/CodeGenerator/Oracle/Helper/DiffViewHelper.php 0 → 100644 +88 −0 Original line number Diff line number Diff line <?php namespace UnicaenDbImport\CodeGenerator\Oracle\Helper; use Doctrine\DBAL\Platforms\OraclePlatform; use UnicaenApp\Exception\LogicException; use UnicaenDbImport\Domain\Operation; class DiffViewHelper extends \UnicaenDbImport\CodeGenerator\Helper\DiffViewHelper { /** * @var OraclePlatform */ protected $platform; /** * {@inheritDoc} */ protected function generateViewDeletionSQLSnippet($destinationTable) { $name = $this->generateViewName($destinationTable); return "DROP VIEW $name"; } /** * {@inheritDoc} */ protected function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns) { return $this->generateSQLUpdate(Operation::OPERATION_UPDATE, $destinationTable, $sourceCodeColumn, $columns); } /** * {@inheritDoc} */ protected function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns) { return $this->generateSQLUpdate(Operation::OPERATION_UNDELETE, $destinationTable, $sourceCodeColumn, $columns); } /** * {@inheritDoc} */ protected function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns) { return $this->generateSQLUpdate(Operation::OPERATION_DELETE, $destinationTable, $sourceCodeColumn, $columns); } /** * @param string $operation * @param string $destinationTable * @param string $sourceCodeColumn * @param array $columns * @return string */ protected function generateSQLUpdate($operation, $destinationTable, $sourceCodeColumn, array $columns) { $diffViewName = $this->generateViewName($destinationTable); $now = $this->platform->getNowExpression(); // instruction de mise à jour de la table destination $markup = ''; $markup .= "MERGE INTO $destinationTable d USING (SELECT * FROM $diffViewName) AS diff " . PHP_EOL; $markup .= "ON diff.operation = '$operation' AND d.$sourceCodeColumn = diff.$sourceCodeColumn " . PHP_EOL; $markup .= "WHEN MATCHED THEN UPDATE SET " . PHP_EOL; switch ($operation) { case Operation::OPERATION_UPDATE: case Operation::OPERATION_UNDELETE: $colUpdates = array_map(function ($col) { return "$col = diff.S_$col"; }, $columns); $markup .= implode(', ', $colUpdates) . ", updated_on = $now, deleted_on = null"; break; case Operation::OPERATION_DELETE: $markup .= "deleted_on = $now"; break; default: throw new LogicException("Opération spécifiées inattendue: " . $operation); } return $markup; } } No newline at end of file Loading
src/UnicaenDbImport/CodeGenerator/Oracle/Helper/DiffViewHelper.php 0 → 100644 +88 −0 Original line number Diff line number Diff line <?php namespace UnicaenDbImport\CodeGenerator\Oracle\Helper; use Doctrine\DBAL\Platforms\OraclePlatform; use UnicaenApp\Exception\LogicException; use UnicaenDbImport\Domain\Operation; class DiffViewHelper extends \UnicaenDbImport\CodeGenerator\Helper\DiffViewHelper { /** * @var OraclePlatform */ protected $platform; /** * {@inheritDoc} */ protected function generateViewDeletionSQLSnippet($destinationTable) { $name = $this->generateViewName($destinationTable); return "DROP VIEW $name"; } /** * {@inheritDoc} */ protected function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns) { return $this->generateSQLUpdate(Operation::OPERATION_UPDATE, $destinationTable, $sourceCodeColumn, $columns); } /** * {@inheritDoc} */ protected function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns) { return $this->generateSQLUpdate(Operation::OPERATION_UNDELETE, $destinationTable, $sourceCodeColumn, $columns); } /** * {@inheritDoc} */ protected function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns) { return $this->generateSQLUpdate(Operation::OPERATION_DELETE, $destinationTable, $sourceCodeColumn, $columns); } /** * @param string $operation * @param string $destinationTable * @param string $sourceCodeColumn * @param array $columns * @return string */ protected function generateSQLUpdate($operation, $destinationTable, $sourceCodeColumn, array $columns) { $diffViewName = $this->generateViewName($destinationTable); $now = $this->platform->getNowExpression(); // instruction de mise à jour de la table destination $markup = ''; $markup .= "MERGE INTO $destinationTable d USING (SELECT * FROM $diffViewName) AS diff " . PHP_EOL; $markup .= "ON diff.operation = '$operation' AND d.$sourceCodeColumn = diff.$sourceCodeColumn " . PHP_EOL; $markup .= "WHEN MATCHED THEN UPDATE SET " . PHP_EOL; switch ($operation) { case Operation::OPERATION_UPDATE: case Operation::OPERATION_UNDELETE: $colUpdates = array_map(function ($col) { return "$col = diff.S_$col"; }, $columns); $markup .= implode(', ', $colUpdates) . ", updated_on = $now, deleted_on = null"; break; case Operation::OPERATION_DELETE: $markup .= "deleted_on = $now"; break; default: throw new LogicException("Opération spécifiées inattendue: " . $operation); } return $markup; } } No newline at end of file