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

Merge branch 'oracle_support' of https://git.unicaen.fr/lib/unicaen/db-import into oracle_support

parents a37ec773 09ce0433
Loading
Loading
Loading
Loading
+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