Skip to content
Snippets Groups Projects
Commit 93f87e41 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Gestion des CLOB par l'import

parent 55b0ed2b
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,16 @@ namespace UnicaenImport\Entity\Schema;
class Column
{
/**
* @var string
*/
public $table;
/**
* @var string
*/
public $name;
/**
* Type de données
*
......@@ -61,4 +71,11 @@ class Column
*/
public $importActif;
function __toString()
{
return $this->name;
}
}
\ No newline at end of file
<?php
namespace UnicaenImport\Service;
use UnicaenImport\Entity\Schema\Column;
use UnicaenImport\Exception\Exception;
use UnicaenImport\Entity\Differentiel\Query;
use UnicaenImport\Options\Traits\ModuleOptionsAwareTrait;
......@@ -361,10 +362,13 @@ class QueryGeneratorService extends AbstractService
return $result;
}
/**
* Teste que la séquence correspondant à la table spécifiée existe bien.
*
* @param string $table
*
* @throws \Doctrine\DBAL\DBALException En cas d'erreur en BDD
* @throws Exception Si la séquence n'existe pas.
*
......@@ -471,7 +475,7 @@ class QueryGeneratorService extends AbstractService
} else {
// on recherche si la table dépend d'une table qui, elle, serait annualisée
foreach ($schema as $columnName => $column) {
/* @var $column \Import\Entity\Schema\Column */
/* @var $column \UnicaenImport\Entity\Schema\Column */
if (!empty($column->refTableName)) {
$refSchema = $this->getServiceSchema()->getSchema($column->refTableName);
if (!empty($refSchema) && array_key_exists(self::ANNEE_COLUMN_NAME, $refSchema)) {
......@@ -493,6 +497,9 @@ class QueryGeneratorService extends AbstractService
// on génère ensuite la bonne requête !!!
$cols = $this->getCols($tableName);
foreach ($cols as $id => $col) {
$cols[$id] = $schema[$col];
}
$sql = "CREATE OR REPLACE FORCE VIEW V_DIFF_$tableName AS
select diff.* from (SELECT
COALESCE( D.id, S.id ) id,
......@@ -503,8 +510,20 @@ CASE
WHEN S.source_code IS NOT NULL AND D.source_code IS NOT NULL AND (D.histo_destruction IS NULL OR D.histo_destruction > SYSDATE) THEN 'update'
WHEN S.source_code IS NULL AND D.source_code IS NOT NULL AND (D.histo_destruction IS NULL OR D.histo_destruction > SYSDATE)$delCond THEN 'delete'
WHEN S.source_code IS NOT NULL AND D.source_code IS NOT NULL AND D.histo_destruction IS NOT NULL AND D.histo_destruction <= SYSDATE THEN 'undelete' END import_action,
" . $this->formatColQuery($cols, ' CASE WHEN S.source_code IS NULL AND D.source_code IS NOT NULL THEN D.:column ELSE S.:column END :column', ",\n ") . ",
" . $this->formatColQuery($cols, ' CASE WHEN D.:column <> S.:column OR (D.:column IS NULL AND S.:column IS NOT NULL) OR (D.:column IS NOT NULL AND S.:column IS NULL) THEN 1 ELSE 0 END U_:column', ",\n ") . "
" . $this->formatColQuery($cols, function (Column $col) {
if ($col->dataType == 'CLOB') {
return ' CASE WHEN S.source_code IS NULL AND D.source_code IS NOT NULL THEN D.:column ELSE to_clob(S.:column) END :column';
} else {
return ' CASE WHEN S.source_code IS NULL AND D.source_code IS NOT NULL THEN D.:column ELSE S.:column END :column';
}
}, ",\n ") . ",
" . $this->formatColQuery($cols, function (Column $col) {
if ($col->dataType == 'CLOB') {
return ' CASE WHEN dbms_lob.compare(D.:column,S.:column) <> 0 OR (D.:column IS NULL AND S.:column IS NOT NULL) OR (D.:column IS NOT NULL AND S.:column IS NULL) THEN 1 ELSE 0 END U_:column';
} else {
return ' CASE WHEN D.:column <> S.:column OR (D.:column IS NULL AND S.:column IS NOT NULL) OR (D.:column IS NOT NULL AND S.:column IS NULL) THEN 1 ELSE 0 END U_:column';
}
}, ",\n ") . "
FROM
$tableName D$depJoin
FULL JOIN SRC_$tableName S ON S.source_id = D.source_id AND S.source_code = D.source_code$joinCond
......@@ -512,7 +531,13 @@ WHERE
(S.source_code IS NOT NULL AND D.source_code IS NOT NULL AND D.histo_destruction IS NOT NULL AND D.histo_destruction <= SYSDATE)
OR (S.source_code IS NULL AND D.source_code IS NOT NULL AND (D.histo_destruction IS NULL OR D.histo_destruction > SYSDATE))
OR (S.source_code IS NOT NULL AND D.source_code IS NULL)
OR " . $this->formatColQuery($cols, 'D.:column <> S.:column OR (D.:column IS NULL AND S.:column IS NOT NULL) OR (D.:column IS NOT NULL AND S.:column IS NULL)', "\n OR ") . "
OR " . $this->formatColQuery($cols, function (Column $col) {
if ($col->dataType == 'CLOB') {
return 'dbms_lob.compare(D.:column,S.:column) <> 0 OR (D.:column IS NULL AND S.:column IS NOT NULL) OR (D.:column IS NOT NULL AND S.:column IS NULL)';
} else {
return 'D.:column <> S.:column OR (D.:column IS NULL AND S.:column IS NOT NULL) OR (D.:column IS NOT NULL AND S.:column IS NULL)';
}
}, "\n OR ") . "
) diff JOIN source on source.id = diff.source_id WHERE import_action IS NOT NULL AND source.importable = 1";
return $sql;
......@@ -613,7 +638,13 @@ WHERE
{
$res = [];
foreach ($cols as $col) {
$res[] = str_replace(':column', $col, $format);
if (is_callable($format)) {
$f = $format($col);
} else {
$f = $format;
}
$res[] = str_replace(':column', $col, $f);
}
return implode($separator, $res);
......
......@@ -54,6 +54,8 @@ class SchemaService extends AbstractService
$sc = [];
foreach( $d as $col ){
$column = new Column;
$column->table = $col['TABLE_NAME'];
$column->name = $col['COLUMN_NAME'];
$column->dataType = $col['DATA_TYPE'];
$column->length = (null === $col['LENGTH']) ? null : (integer)$col['LENGTH'];
$column->nullable = $col['NULLABLE'] == '1';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment