Commit 8730ba44 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Possibilité d'ajouter un 'where' dans la config d'un import ou d'une destination

parent ded23755
Loading
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -117,6 +117,7 @@ return [
                // - 'table'              : nom de la table source contenant les données à importer
                // - 'select'             : select SQL de mise en forme des données source à importer (NB: antinomique avec 'table')
                // - 'source_code_column' : nom de la colonne dans la table/vue source contenant l'identifiant unique
                // - 'where'              : filtre SQL éventuel à appliquer aux données sources.
                //
                'source' => [
                    'name'               => 'TABLE PAYS DE APOGÉE',
@@ -124,6 +125,7 @@ return [
                    //'table'              => 'PAYS', // Équivalent de SELECT * FROM PAYS;
                    'connection'         => 'apogee',
                    'source_code_column' => 'COD_PAY',
                    //'where'            => "TEM_OUV_DRT_SSO_PAY = 'N'",
                ],
                //
                // Configuration de la destination des données importées :
@@ -169,12 +171,14 @@ return [
                // - 'select'             : select SQL de mise en forme des données source à importer (NB: antinomique avec 'table')
                // - 'connection'         : identifiant de la connexion à la bdd source (cf. clé 'connections' plus haut)
                // - 'source_code_column' : nom de la colonne dans la table/vue source contenant l'identifiant unique
                // - 'where'              : filtre éventuel à apliquer aux données sources 
                //
                'source' => [
                    'name'               => 'TABLE MES_PAYS_TEMPORAIRES',
                    'table'              => 'MES_PAYS_TEMPORAIRES',
                    'connection'         => 'default',
                    'source_code_column' => 'COD_PAY',
                    //'where'            => "TEM_OUV_DRT_SSO_PAY = 'N'",
                ],

                //
@@ -183,12 +187,15 @@ return [
                // - 'connection'         : identifiant de la connexion à la bdd destination (cf. clé 'connections' plus haut)
                // - 'table'              : nom de la table destination vers laquelle les données sont synchronisées
                // - 'source_code_column' : nom de la colonne dans la table destination contenant l'identifiant unique
                // - 'where'              : filtre SQL éventuel permettant de restreindre les données destinations concernées
                //                          (les noms de colonnes doivent être préfixés par l'alias de la table destination 'd')
                //
                'destination' => [
                    'name'               => 'TABLE PAYS',
                    'table'              => 'PAYS',
                    'connection'         => 'default',
                    'source_code_column' => 'COD_PAY',
                    //'where'            => "d.TEM_OUV_DRT_SSO_PAY = 'N'",
                    //
                    // Forçage éventuel du nom de la table où seront inscrits les logs (créée automatiquement si nécessaire).
                    // En l'absence de ce forçage, le nom de la table sera 'import_log'.
+26 −11
Original line number Diff line number Diff line
@@ -198,12 +198,17 @@ abstract class CodeGenerator implements CodeGeneratorInterface
    public function generateSQLForSelectFromSource(SourceInterface $source)
    {
        if ($source->getSelect()) {
            return $source->getSelect();
            $query = $source->getSelect();
        } else {
            $sourceTable = $source->getTable();
            $query = $this->tableHelper->generateSQLForSelectFromTable($sourceTable);
        }

        $sourceTable = $source->getTable();
        if ($where = $source->getWhere()) {
            $query = sprintf('SELECT * FROM (%s) tmp WHERE (%s)', $query, $where);
        }

        return $this->tableHelper->generateSQLForSelectFromTable($sourceTable);
        return $query;
    }

    /**
@@ -313,6 +318,7 @@ abstract class CodeGenerator implements CodeGeneratorInterface
    {
        $destinationTable = $destination->getTable();
        $idColumnSequence = $destination->getIdColumnSequence();
        $where = $destination->getWhere();
        $sourceCodeColumn = $source->getSourceCodeColumn();
        $columns = $source->getColumns();

@@ -331,19 +337,22 @@ abstract class CodeGenerator implements CodeGeneratorInterface
                $sql = $this->tableHelper->generateSQLForUpdateOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns);
                        $columns,
                        $where);
                break;
            case Operation::OPERATION_DELETE;
                $sql = $this->tableHelper->generateSQLForDeleteOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns);
                        $columns,
                        $where);
                break;
            case Operation::OPERATION_UNDELETE;
                $sql = $this->tableHelper->generateSQLForUndeleteOperationInDestinationTable(
                        $destinationTable,
                        $sourceCodeColumn,
                        $columns);
                        $columns,
                        $where);
                break;
            default:
                throw new RuntimeException("Opération inattendue");
@@ -386,9 +395,10 @@ EOS;

    /**
     * @param array $importObservRow
     * @param string $where
     * @return string
     */
    protected function generateSQLForSelectingDiffViewFromImportObservRow(array $importObservRow)
    protected function generateSQLForSelectingDiffViewFromImportObservRow(array $importObservRow, $where = null)
    {
        $id = $importObservRow['id'];
        $tableName = $importObservRow['tableName'];
@@ -408,8 +418,11 @@ EOS;
        // Ex: "v.COLONNE = 'VALEUR'" ou "v.COLONNE IS NULL".
        $toValueCond = 'v.' . $sColName . ($toValue === null ? ' is null' : (" = '" . $toValue . "'"));

        // Eventuel filtre.
        $andWhere = $filter ? 'and ' . $filter : '';
        // Eventuels filtres : celui de la table IMPORT_OBSERV + celui spécifié dans la config de la destination.
        $wheres = [];
        if ($filter) $wheres[] = sprintf('(%s)', $filter);
        if ($where) $wheres[] = sprintf('(%s)', str_ireplace('d.', 't.', $where));
        $andWhere = $wheres ? 'and ' . implode(' and ', $wheres) : '';

        // Colonne 'detail'. Ex: "coalesce(t.RESULTAT::varchar, '') || '>' || coalesce(v.RESULTAT::varchar, '')"
        $detail =
@@ -432,16 +445,18 @@ EOS;
    }

    /**
     * @param DestinationInterface $destination
     * @param array $importObservRows
     * @return string
     */
    public function generateSQLForInsertionIntoImportObservResult(array $importObservRows)
    public function generateSQLForInsertionIntoImportObservResult(DestinationInterface $destination, array $importObservRows)
    {
        $where = $destination->getWhere();
        $now = $this->platform->getNowExpression();

        $selects = [];
        foreach ($importObservRows as $importObservRow) {
            $selects[] = $this->generateSQLForSelectingDiffViewFromImportObservRow($importObservRow);
            $selects[] = $this->generateSQLForSelectingDiffViewFromImportObservRow($importObservRow, $where);
        }
        $selects = implode(PHP_EOL . 'UNION ALL' . PHP_EOL, $selects);

+6 −3
Original line number Diff line number Diff line
@@ -528,23 +528,26 @@ EOT;
     * @param string $destinationTable
     * @param string $sourceCodeColumn
     * @param array $columns
     * @param string $where
     * @return string
     */
    abstract public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
    abstract public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null);

    /**
     * @param string $destinationTable
     * @param string $sourceCodeColumn
     * @param array $columns
     * @param string $where
     * @return string
     */
    abstract public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
    abstract public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null);

    /**
     * @param string $destinationTable
     * @param string $sourceCodeColumn
     * @param array $columns
     * @param string $where
     * @return string
     */
    abstract public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns);
    abstract public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null);
}
 No newline at end of file
+8 −7
Original line number Diff line number Diff line
@@ -80,25 +80,25 @@ class TableHelper extends \UnicaenDbImport\CodeGenerator\Helper\TableHelper
    /**
     * {@inheritDoc}
     */
    public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns)
    public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        return $this->generateSQLUpdate(Operation::OPERATION_UPDATE, $destinationTable, $sourceCodeColumn, $columns);
        return $this->generateSQLUpdate(Operation::OPERATION_UPDATE, $destinationTable, $sourceCodeColumn, $columns, $where);
    }

    /**
     * {@inheritDoc}
     */
    public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns)
    public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        return $this->generateSQLUpdate(Operation::OPERATION_UNDELETE, $destinationTable, $sourceCodeColumn, $columns);
        return $this->generateSQLUpdate(Operation::OPERATION_UNDELETE, $destinationTable, $sourceCodeColumn, $columns, $where);
    }

    /**
     * {@inheritDoc}
     */
    public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns)
    public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        return $this->generateSQLUpdate(Operation::OPERATION_DELETE, $destinationTable, $sourceCodeColumn, $columns);
        return $this->generateSQLUpdate(Operation::OPERATION_DELETE, $destinationTable, $sourceCodeColumn, $columns, $where);
    }

    /**
@@ -106,9 +106,10 @@ class TableHelper extends \UnicaenDbImport\CodeGenerator\Helper\TableHelper
     * @param string $destinationTable
     * @param string $sourceCodeColumn
     * @param array $columns
     * @param string $where
     * @return string
     */
    protected function generateSQLUpdate($operation, $destinationTable, $sourceCodeColumn, array $columns)
    protected function generateSQLUpdate($operation, $destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        $diffViewName = $this->generateDiffViewName($destinationTable);
        $setters = $this->generateSQLUpdateSetters($operation, $columns);
+13 −9
Original line number Diff line number Diff line
@@ -35,25 +35,25 @@ class TableHelper extends \UnicaenDbImport\CodeGenerator\Helper\TableHelper
    /**
     * {@inheritDoc}
     */
    public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns)
    public function generateSQLForUpdateOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        return $this->generateSQLUpdate(Operation::OPERATION_UPDATE, $destinationTable, $sourceCodeColumn, $columns);
        return $this->generateSQLUpdate(Operation::OPERATION_UPDATE, $destinationTable, $sourceCodeColumn, $columns, $where);
    }

    /**
     * {@inheritDoc}
     */
    public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns)
    public function generateSQLForUndeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        return $this->generateSQLUpdate(Operation::OPERATION_UNDELETE, $destinationTable, $sourceCodeColumn, $columns);
        return $this->generateSQLUpdate(Operation::OPERATION_UNDELETE, $destinationTable, $sourceCodeColumn, $columns, $where);
    }

    /**
     * {@inheritDoc}
     */
    public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns)
    public function generateSQLForDeleteOperationInDestinationTable($destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        return $this->generateSQLUpdate(Operation::OPERATION_DELETE, $destinationTable, $sourceCodeColumn, $columns);
        return $this->generateSQLUpdate(Operation::OPERATION_DELETE, $destinationTable, $sourceCodeColumn, $columns, $where);
    }

    /**
@@ -61,20 +61,24 @@ class TableHelper extends \UnicaenDbImport\CodeGenerator\Helper\TableHelper
     * @param string $destinationTable
     * @param string $sourceCodeColumn
     * @param array $columns
     * @param string $where
     * @return string
     */
    protected function generateSQLUpdate($operation, $destinationTable, $sourceCodeColumn, array $columns)
    protected function generateSQLUpdate($operation, $destinationTable, $sourceCodeColumn, array $columns, $where = null)
    {
        $diffViewName = $this->generateDiffViewName($destinationTable);
        $setters = $this->generateSQLUpdateSetters($operation, $columns);
        $where = "diff.operation = '$operation' AND d.$sourceCodeColumn = diff.$sourceCodeColumn";
        $wheres = "diff.operation = '$operation' AND d.$sourceCodeColumn = diff.$sourceCodeColumn";
        if ($where) {
            $wheres .= sprintf(" AND (%s)", $where);
        }

        // instruction de mise à jour de la table destination
        $markup = '';
        $markup .= "UPDATE $destinationTable d SET " . PHP_EOL;
        $markup .= $setters . PHP_EOL;
        $markup .= "FROM (SELECT * FROM $diffViewName) AS diff " . PHP_EOL;
        $markup .= "WHERE $where";
        $markup .= "WHERE $wheres";

        return $markup;
    }
Loading