This commit is contained in:
2022-10-05 15:23:28 +02:00
parent 58b1778544
commit a967e1ed17
194 changed files with 1580 additions and 1386 deletions

View File

@@ -13,11 +13,10 @@ namespace Chill\MainBundle\Service\Import;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Exception;
use LogicException;
use Psr\Log\LoggerInterface;
use RuntimeException;
use function array_key_exists;
use function count;
@@ -77,7 +76,7 @@ final class GeographicalUnitBaseImporter
string $unitName,
string $unitKey,
string $geomAsWKT,
int $srid = null
?int $srid = null
): void {
$this->initialize();
@@ -87,7 +86,7 @@ final class GeographicalUnitBaseImporter
'unitName' => $unitName,
'unitKey' => $unitKey,
'geomAsWKT' => $geomAsWKT,
'srid' => $srid
'srid' => $srid,
];
if (100 <= count($this->waitingForInsert)) {
@@ -138,8 +137,10 @@ final class GeographicalUnitBaseImporter
}
$statement = $this->cachingStatements[$forNumber];
try {
$i = 0;
foreach ($this->waitingForInsert as $insert) {
$statement->bindValue(++$i, $insert['layerKey'], Types::STRING);
$statement->bindValue(++$i, $insert['layerName'], Types::JSON);
@@ -151,8 +152,8 @@ final class GeographicalUnitBaseImporter
$affected = $statement->executeStatement();
if ($affected === 0) {
throw new \RuntimeException('no row affected');
if (0 === $affected) {
throw new RuntimeException('no row affected');
}
} catch (Exception $e) {
throw $e;
@@ -182,10 +183,10 @@ final class GeographicalUnitBaseImporter
private function updateGeographicalUnitTable(): void
{
$this->defaultConnection->transactional(
function() {
function () {
// 0) create new layers
$this->defaultConnection->executeStatement(
"
"
WITH unique_layers AS (
SELECT DISTINCT layerKey, layerName FROM geographical_unit_temp
)
@@ -195,14 +196,15 @@ final class GeographicalUnitBaseImporter
layerName,
layerKey
FROM unique_layers
ON CONFLICT (refid)
ON CONFLICT (refid)
DO UPDATE SET name=EXCLUDED.name
");
"
);
//1) Add new units
$this->logger->info(self::LOG_PREFIX . 'upsert new units');
$affected = $this->defaultConnection->executeStatement("INSERT INTO chill_main_geographical_unit
(id, geom, unitname, layer_id, unitrefid)
$affected = $this->defaultConnection->executeStatement("INSERT INTO chill_main_geographical_unit
(id, geom, unitname, layer_id, unitrefid)
SELECT
nextval('chill_main_geographical_unit_id_seq'),
geom,
@@ -210,7 +212,7 @@ final class GeographicalUnitBaseImporter
layer.id,
unitKey
FROM geographical_unit_temp JOIN chill_main_geographical_unit_layer AS layer ON layer.refid = layerKey
ON CONFLICT (layer_id, unitrefid)
ON CONFLICT (layer_id, unitrefid)
DO UPDATE
SET geom = EXCLUDED.geom, unitname = EXCLUDED.unitname
");
@@ -219,9 +221,9 @@ final class GeographicalUnitBaseImporter
//3) Delete units
$this->logger->info(self::LOG_PREFIX . 'soft delete adresses');
$affected = $this->defaultConnection->executeStatement('WITH to_delete AS (
SELECT cmgu.id
SELECT cmgu.id
FROM chill_main_geographical_unit AS cmgu
JOIN chill_main_geographical_unit_layer AS cmgul ON cmgul.id = cmgu.layer_id
JOIN chill_main_geographical_unit_layer AS cmgul ON cmgul.id = cmgu.layer_id
JOIN geographical_unit_temp AS gut ON cmgul.refid = gut.layerKey AND cmgu.unitrefid = gut.unitKey
)
DELETE FROM chill_main_geographical_unit