Commit 588adc8f authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Utilisation privilégiée des classes de plateforme Doctrine.

parent bbc4b939
Loading
Loading
Loading
Loading
+4 −12
Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/AbstractHelper.php: 4 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace UnicaenDbImport\CodeGenerator\Common\Helper;

use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * Classe mère des helpers de génération de code SQL.
 *
@@ -10,19 +12,9 @@ namespace UnicaenDbImport\CodeGenerator\Common\Helper;
abstract class AbstractHelper
{
    /**
     * @return string
     */
    abstract protected function getVarcharTypeSQLSnippet();

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

    /**
     * @return string
     * @return AbstractPlatform
     */
    abstract protected function getNowExpressionSQLSnippet();
    abstract protected function getPlatform();

    /**
     * @param int    $length
+23 −17
Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/FunctionCreationHelper.php: 23 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

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}".
 *
@@ -11,6 +13,8 @@ namespace UnicaenDbImport\CodeGenerator\Common\Helper;
 */
abstract class FunctionCreationHelper extends AbstractHelper
{
    use PostgreSQLCommonsTrait;

    /**
     * @param string $destinationTable
     * @param string $sourceCodeColumn
@@ -46,10 +50,12 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    protected function generateFunctionCreationSQLSnippet($destinationTable, $sourceCodeColumn, array $columns)
    {
        $varchar = $this->getPlatform()->getVarcharTypeDeclarationSQL([]);

        $res = '';
        $res .= 'CREATE OR REPLACE FUNCTION ' . $this->generateFunctionName($destinationTable) . '(' . PHP_EOL;
        $res .= $this->indent(4, $this->generateSignatureArgsList($sourceCodeColumn, $columns)) . PHP_EOL;
        $res .= ') RETURNS ' . $this->getVarcharTypeSQLSnippet() . ' AS';
        $res .= ') RETURNS ' . $varchar . ' AS';

        return $res;
    }
@@ -61,20 +67,25 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    protected function generateSignatureArgsList($sourceCodeColumn, array $columns)
    {
        $clob = $this->getPlatform()->getClobTypeDeclarationSQL([]);
        $varchar = $this->getPlatform()->getVarcharTypeDeclarationSQL([]);
        $timestamp = $this->getPlatform()->getDateTimeTzTypeDeclarationSQL([]);

        $sourceCols = array_merge([$sourceCodeColumn], $columns);
        $destinCols = array_merge([$sourceCodeColumn], $columns);

        $sourceColsWithTypes = array_map(function ($col) {
            return 'src_' . $col . ' ' . $this->getVarcharTypeSQLSnippet();
        $sourceColsWithTypes = array_map(function ($col) use ($clob) {
            return 'src_' . $col . ' ' . $clob;
        }, $sourceCols);
        $destinColsWithTypes = array_map(function ($col) {
            return 'dest_' . $col . ' ' . $this->getVarcharTypeSQLSnippet();
        $destinColsWithTypes = array_map(function ($col) use ($clob) {
            return 'dest_' . $col . ' ' . $clob;
        }, $destinCols);


        $res = '';
        $res .= implode(', ', $sourceColsWithTypes) . ',' . PHP_EOL;
        $res .= implode(', ', $destinColsWithTypes) . ', dest_deleted_on ' . $this->getTimestampTypeSQLSnippet() . ',' . PHP_EOL;
        $res .= 'import_hash ' . $this->getVarcharTypeSQLSnippet();
        $res .= implode(', ', $destinColsWithTypes) . ', dest_deleted_on ' . $timestamp . ',' . PHP_EOL;
        $res .= 'import_hash ' . $varchar;

        return $res;
    }
@@ -97,7 +108,7 @@ abstract class FunctionCreationHelper extends AbstractHelper
        $res .= $this->generateUpdateOperationMarkup($destinationTable, $sourceCodeColumn, $columns)   . PHP_EOL . PHP_EOL;
        $res .= $this->generateUndeleteOperationMarkup($destinationTable, $sourceCodeColumn, $columns) . PHP_EOL . PHP_EOL;
        $res .= $this->generateDeleteOperationMarkup($destinationTable, $sourceCodeColumn)   . PHP_EOL . PHP_EOL;
        $res .= "return operation;";
        $res .= "RETURN operation;";

        return $res;
    }
@@ -115,11 +126,6 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    abstract protected function generateMetaRequestAffectationForInsertOperation($destinationTable, $sourceCodeColumn, $columns);

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

    /**
     * @param $destinationTable
     * @param $sourceCodeColumn
@@ -150,7 +156,7 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    protected function generateInsertIntoImportRegisterTableForInsertOperation($destinationTable, $sourceCodeColumn)
    {
        $now = $this->getNowExpressionSQLSnippet();
        $now = $this->getPlatform()->getNowExpression();

        $res = '';
        $res .=
@@ -168,7 +174,7 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    protected function generateInsertIntoImportRegisterTableForUpdateOperation($destinationTable, $sourceCodeColumn, $field)
    {
        $now = $this->getNowExpressionSQLSnippet();
        $now = $this->getPlatform()->getNowExpression();

        $res = '';
        $res .=
@@ -185,7 +191,7 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    protected function generateInsertIntoImportRegisterTableForUndeleteOperation($destinationTable, $sourceCodeColumn)
    {
        $now = $this->getNowExpressionSQLSnippet();
        $now = $this->getPlatform()->getNowExpression();

        $res = '';
        $res .=
@@ -202,7 +208,7 @@ abstract class FunctionCreationHelper extends AbstractHelper
     */
    protected function generateInsertIntoImportRegisterTableForDeleteOperation($destinationTable, $sourceCodeColumn)
    {
        $now = $this->getNowExpressionSQLSnippet();
        $now = $this->getPlatform()->getNowExpression();

        $res = '';
        $res .=
+6 −17
Changes for src/UnicaenDbImport/CodeGenerator/Common/Helper/TableValidationHelper.php: 6 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -57,18 +57,7 @@ abstract class TableValidationHelper extends AbstractHelper
     * @param string $tableName
     * @return string
     */
    public function generateSQLForHistoColumnsValidation($tableName)
    {
        $timestampType = strtolower($this->getTimestampTypeSQLSnippet());

        $columnsAndTypes = [
            'created_on' => $timestampType,
            'updated_on' => $timestampType,
            'deleted_on' => $timestampType,
        ];

        return $this->generateSQLForColumnsValidation($tableName, $columnsAndTypes);
    }
    abstract public function generateSQLForHistoColumnsValidation($tableName);

    /**
     * @param string $tableName
@@ -81,14 +70,14 @@ abstract class TableValidationHelper extends AbstractHelper
            return null;
        }

        $timestampType = $this->getTimestampTypeSQLSnippet();
        $now = $this->getNowExpressionSQLSnippet();
        $timestamp = strtolower($this->getPlatform()->getDateTimeTzTypeDeclarationSQL([]));
        $now = $this->getPlatform()->getNowExpression();

        $message = "Assurez-vous que les colonnes d'historique suivantes sont présentes dans la table '$tableName' : " . PHP_EOL;
        $message .= <<<EOT
ALTER TABLE $tableName ADD created_on $timestampType DEFAULT $now NOT NULL ;
ALTER TABLE $tableName ADD updated_on $timestampType ;
ALTER TABLE $tableName ADD deleted_on $timestampType ;
ALTER TABLE $tableName ADD created_on $timestamp DEFAULT $now NOT NULL ;
ALTER TABLE $tableName ADD updated_on $timestamp ;
ALTER TABLE $tableName ADD deleted_on $timestamp ;
EOT;

        return new RuntimeException($message);
+2 −2
Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCallingHelper.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -29,13 +29,13 @@ class FunctionCallingHelper extends \UnicaenDbImport\CodeGenerator\Common\Helper
        $sourceCols = $this->generateColumnNamesWithPrefix($sourceCols, 'src.',  $columnsToChar);
        $destinCols = $this->generateColumnNamesWithPrefix($destinCols, 'dest.', $columnsToChar);

        $uuid = $this->importHash ?: Uuid::uuid1()->toString();
        $importHash = $this->importHash ?: Uuid::uuid1()->toString();

        $res = '';
        $res .= $functionName . '(' . PHP_EOL;
        $res .= '    ' . implode(', ', $sourceCols) . ',' . PHP_EOL;
        $res .= '    ' . implode(', ', $destinCols) . ',' . PHP_EOL;
        $res .= '    ' . '$$' . $uuid . '$$' . PHP_EOL;
        $res .= '    ' . '$$' . $importHash . '$$' . PHP_EOL;
        $res .= ")";

        return $res;
+14 −14
Changes for src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/FunctionCreationHelper.php: 14 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -48,14 +48,7 @@ EOT;

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

        return $res;
    }

    protected function generateSQLForImportRegisterUpdateForHash()
    {
        $res = "UPDATE import_reg SET executed_on = now() WHERE import_hash = $$', import_hash, '$$ ;";
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        return $res;
    }
@@ -63,12 +56,12 @@ EOT;
    protected function generateMetaRequestAffectationForUpdateOperation($destinationTable, $sourceCodeColumn, $field)
    {
        $res = '';
        $res .= "sql = concat('UPDATE $destinationTable SET $field = $$', src_$field, '$$, updated_on = now() " .
        $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 .= "sql = concat(sql, ' " . $this->generateSQLForImportRegisterUpdateForHash() . "');";
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        return $res;
    }
@@ -80,11 +73,11 @@ EOT;
        foreach ($columns as $col) {
            $res .= "$col = $$', src_$col, '$$, ";
        }
        $res .= "updated_on = now(), deleted_on = null WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');";
        $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 .= "sql = concat(sql, ' " . $this->generateSQLForImportRegisterUpdateForHash() . "');";
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        return $res;
    }
@@ -92,12 +85,19 @@ EOT;
    protected function generateMetaRequestAffectationForDeleteOperation($destinationTable, $sourceCodeColumn)
    {
        $res = '';
        $res .= "sql = concat('UPDATE $destinationTable SET deleted_on = now() WHERE $sourceCodeColumn = $$', dest_$sourceCodeColumn, '$$ ;');";
        $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 .= "sql = concat(sql, ' " . $this->generateSQLForImportRegisterUpdateForHash() . "');";
        $res .= $this->generateRegisterExecutionDateUpdateConcat();

        return $res;
    }

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

        return "sql = concat(sql, ' UPDATE import_reg SET executed_on = $now WHERE import_hash = ''', import_hash, ''' ;');";
    }
}
 No newline at end of file
Loading