Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ services:
resource: '../src/Controller/'
tags: ['controller.service_arguments']


App\Service\FileProcessor:
arguments:
$projectDir: '%kernel.project_dir%'
Expand Down
29 changes: 29 additions & 0 deletions migrations/Version20250507004714.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250507004714 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add support for original address in RPPS db';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE rpps ADD original_address LONGTEXT DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE rpps DROP original_address');
}
}
144 changes: 144 additions & 0 deletions src/Doctrine/PointWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

namespace App\Doctrine;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\SQLParserUtils;
use Doctrine\DBAL\Types\Type;
use Throwable;

class PointWrapper extends Connection
{
public function __construct(array $params, Driver $driver, Configuration $config, EventManager $eventManager)
{
parent::__construct($params, $driver, $config, $eventManager);
}

public function prepare($sql)
{
try {
Expand All @@ -23,4 +35,136 @@ public function prepare($sql)

return $stmt;
}

/**
* Executes an SQL statement with the given parameters and returns the number of affected rows.
*
* Could be used for:
* - DML statements: INSERT, UPDATE, DELETE, etc.
* - DDL statements: CREATE, DROP, ALTER, etc.
* - DCL statements: GRANT, REVOKE, etc.
* - Session control statements: ALTER SESSION, SET, DECLARE, etc.
* - Other statements that don't yield a row set.
*
* This method supports PDO binding types as well as DBAL mapping types.
*
* @param string $sql SQL statement
* @param array<int, mixed>|array<string, mixed> $params Statement parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return int|string the number of affected rows
*
* @throws Exception
*/
public function executeStatement($sql, array $params = [], array $types = [])
{
$logger = $this->_config->getSQLLogger();
if ($logger) {
$logger->startQuery($sql, $params, $types);
}

try {
if ($params) {
[$sql, $params, $types] = SQLParserUtils::expandListParameters($sql, $params, $types);

$stmt = $this->prepare($sql);

if ($types) {
$this->bindTypedValues($stmt, $params, $types);
$stmt->execute();
} else {
$stmt->execute($params);
}

$result = $stmt->rowCount();
} else {
$result = $this->exec($sql);
}
} catch (Throwable $e) {
$this->handleExceptionDuringQuery(
$e,
$sql,
$params,
$types
);
}

if ($logger) {
$logger->stopQuery();
}

return $result;
}

/**
* Binds a set of parameters, some or all of which are typed with a PDO binding type
* or DBAL mapping type, to a given statement.
*
* @internal duck-typing used on the $stmt parameter to support driver statements as well as
* raw PDOStatement instances
*
* @param Driver\Statement $stmt Prepared statement
* @param array<int, mixed>|array<string, mixed> $params Statement parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return void
*
* This is based on bindTypedValues
*/
private function bindTypedValues($stmt, array $params, array $types)
{
// Check whether parameters are positional or named. Mixing is not allowed, just like in PDO.
if (is_int(key($params))) {
// Positional parameters
$typeOffset = array_key_exists(0, $types) ? -1 : 0;
$bindIndex = 1;
foreach ($params as $value) {
$typeIndex = $bindIndex + $typeOffset;
if (isset($types[$typeIndex])) {
$type = $types[$typeIndex];
[$value, $bindingType] = $this->getBindingInfo($value, $type);
$stmt->bindValue($bindIndex, $value, $bindingType);
} else {
$stmt->bindValue($bindIndex, $value);
}

++$bindIndex;
}
} else {
// Named parameters
foreach ($params as $name => $value) {
if (isset($types[$name])) {
$type = $types[$name];
[$value, $bindingType] = $this->getBindingInfo($value, $type);
$stmt->bindValue($name, $value, $bindingType);
} else {
$stmt->bindValue($name, $value);
}
}
}
}

/**
* Gets the binding type of a given type. The given type can be a PDO or DBAL mapping type.
*
* @param mixed $value the value to bind
* @param int|string|Type|null $type the type to bind (PDO or DBAL)
*
* @return array{mixed, int} [0] => the (escaped) value, [1] => the binding type
*/
private function getBindingInfo($value, $type): array
{
if (is_string($type)) {
$type = Type::getType($type);
}

if ($type instanceof Type) {
$value = $type->convertToDatabaseValue($value, $this->getDatabasePlatform());
$bindingType = $type->getBindingType();
} else {
$bindingType = $type ?? ParameterType::STRING;
}

return [$value, $bindingType];
}
}
4 changes: 4 additions & 0 deletions src/Doctrine/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Statement extends BaseStatement

public function bindValue($param, $value, $type = ParameterType::STRING)
{
if (is_string($value) && str_starts_with($value, 'ST_GeomFromText')) {
$value = $this->connexion->fetchOne("SELECT $value;");
}

if ($this->connexion && PointType::POINT === $type) {
$lat = $value['latitude'] ?? 0;
$lng = $value['longitude'] ?? 0;
Expand Down
13 changes: 13 additions & 0 deletions src/Entity/RPPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ class RPPS extends BaseEntity implements ImportableEntityInterface
#[ORM\Column(type: 'float', nullable: true)]
protected ?float $latitude = null;

#[ORM\Column(type: 'text', nullable: true)]
protected ?string $originalAddress = null;

#[ORM\Column(type: PointType::POINT, nullable: false)]
private array $coordinates = [];

Expand Down Expand Up @@ -638,4 +641,14 @@ public function setCoordinates(array $coordinates): void
{
$this->coordinates = $coordinates;
}

public function getOriginalAddress(): ?string
{
return $this->originalAddress;
}

public function setOriginalAddress(?string $originalAddress): void
{
$this->originalAddress = $originalAddress;
}
}
11 changes: 11 additions & 0 deletions src/Service/RPPSService.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ protected function processRPPS(array $data): ?RPPS
$rpps->setZipcode($data[35]);
$rpps->setCity($data[37]);

$originalAddress = $data[28] . ' ' . $data[31] . ' ' . $data[32] . ' ' . $data[33] . ' ' . $data[35] . ' ' . $data[37];

if ($originalAddress !== $rpps->getOriginalAddress()) {
$rpps->setLatitude(null);
$rpps->setLongitude(null);
}
$rpps->setOriginalAddress($originalAddress);

$cityEntity = $this->findCityEntity($data[35], $data[37]);
if ($cityEntity) {
$rpps->setCityEntity($cityEntity);
Expand All @@ -232,6 +240,9 @@ protected function processRPPS(array $data): ?RPPS

$this->entities[$rpps->getIdRpps()] = $rpps;

$this->em->persist($rpps);
$this->em->flush();

return $rpps;
}

Expand Down