From cb7e5bc17be4870ad7a14e3fdaf434fded751ee5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laurent=20L=C3=A9cluse?= <laurent.lecluse@unicaen.fr>
Date: Thu, 30 Jan 2025 09:25:34 +0100
Subject: [PATCH] =?UTF-8?q?Possibilit=C3=A9=20d'utiliser=20Connection=20de?=
 =?UTF-8?q?=20Doctrine=20pour=20faire=20des=20requ=C3=AAtes=20en=20modific?=
 =?UTF-8?q?ation=20(pb=20Oracle=20gestion=20des=20transactions)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CHANGELOG.md |  6 ++++
 src/Bdd.php  | 94 ++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 83 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b894bdd..2f538f1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+1.1.0 (30/01/2025)
+------------------
+
+- Possibilité d'utiliser Connection de Doctrine pour faire des requêtes en modification (pb Oracle gestion des transactions)
+
+
 1.0.6 (05/12/2024)
 ------------------
 
diff --git a/src/Bdd.php b/src/Bdd.php
index ff40719..58c1dfd 100644
--- a/src/Bdd.php
+++ b/src/Bdd.php
@@ -2,6 +2,8 @@
 
 namespace Unicaen\BddAdmin;
 
+use Doctrine\DBAL\Connection;
+use Doctrine\DBAL\Types\Types;
 use Unicaen\BddAdmin\Data\DataManager;
 use Unicaen\BddAdmin\Ddl\Ddl;
 use Unicaen\BddAdmin\Ddl\DdlDiff;
@@ -127,6 +129,8 @@ class Bdd
     /** @var array|Bdd[] */
     private array $bdds = [];
 
+    private ?Connection $doctrineConnection = null;
+
     private array $config;
 
     private ?DriverInterface $driver = null;
@@ -144,7 +148,7 @@ class Bdd
 
     protected bool $inCopy = false;
 
-    /** @var \Psr\Container\ContainerInterface|null  */
+    /** @var \Psr\Container\ContainerInterface|null */
     public $__container = null;
 
     private DataManager $data;
@@ -154,7 +158,7 @@ class Bdd
     public function __construct(array $options = [], ?string $connection = null)
     {
         if (!empty($options)) {
-            if (empty($connection)){
+            if (empty($connection)) {
                 $connection = $options[self::OPTION_CURRENT_CONNECTION] ?? 'default';
             }
 
@@ -165,7 +169,7 @@ class Bdd
             $this->setOptions($options);
 
             $this->connection = $connection;
-            
+
             $this->connect();
         }
 
@@ -177,6 +181,21 @@ class Bdd
 
 
 
+    public function getDoctrineConnection(): ?Connection
+    {
+        return $this->doctrineConnection;
+    }
+
+
+
+    public function setDoctrineConnection(?Connection $doctrineConnection): Bdd
+    {
+        $this->doctrineConnection = $doctrineConnection;
+        return $this;
+    }
+
+
+
     /**
      * Hack pour initialiser une classe à partir d'un container respectant Psr\Container\ContainerInterface
      * Si la classe n'est pas connue du container, alors elle est instanciée à l'aide de new
@@ -189,14 +208,14 @@ class Bdd
      */
     public function __newClass(string $classname): mixed
     {
-        if ($this->__container && method_exists($this->__container, 'has') && method_exists($this->__container, 'get')){
-            if ($this->__container->has($classname)){
+        if ($this->__container && method_exists($this->__container, 'has') && method_exists($this->__container, 'get')) {
+            if ($this->__container->has($classname)) {
                 return $this->__container->get($classname);
             }
         }
 
-        if (!class_exists($classname)){
-            throw new \Exception('La classe '.$classname.' n\'existe pas');
+        if (!class_exists($classname)) {
+            throw new \Exception('La classe ' . $classname . ' n\'existe pas');
         }
 
         return new $classname;
@@ -217,8 +236,8 @@ class Bdd
     {
         return $this->connection;
     }
-    
-    
+
+
 
     public function getBdds(): array
     {
@@ -279,7 +298,11 @@ class Bdd
 
     public function beginTransaction(): self
     {
-        $this->driver->beginTransaction();
+        if ($this->doctrineConnection) {
+            $this->doctrineConnection->beginTransaction();
+        } else {
+            $this->driver->beginTransaction();
+        }
 
         return $this;
     }
@@ -288,7 +311,11 @@ class Bdd
 
     public function commitTransaction(): self
     {
-        $this->driver->commitTransaction();
+        if ($this->doctrineConnection) {
+            $this->doctrineConnection->commit();
+        } else {
+            $this->driver->commitTransaction();
+        }
 
         return $this;
     }
@@ -297,7 +324,11 @@ class Bdd
 
     public function rollbackTransaction(): self
     {
-        $this->driver->rollbackTransaction();
+        if ($this->doctrineConnection) {
+            $this->doctrineConnection->rollback();
+        } else {
+            $this->driver->rollbackTransaction();
+        }
 
         return $this;
     }
@@ -306,7 +337,36 @@ class Bdd
 
     public function exec(string $sql, array $params = [], array $types = []): self
     {
-        $this->driver->exec($sql, $params, $types);
+        if ($this->doctrineConnection) {
+            foreach ($types as $ti => $tv) {
+                switch ($tv) {
+                    case self::TYPE_INT:
+                        $types[$ti] = Types::INTEGER;
+                        break;
+                    case self::TYPE_BOOL:
+                        $types[$ti] = Types::BOOLEAN;
+                        break;
+                    case self::TYPE_FLOAT:
+                        $types[$ti] = Types::FLOAT;
+                        break;
+                    case self::TYPE_STRING:
+                        $types[$ti] = Types::STRING;
+                        break;
+                    case self::TYPE_DATE:
+                        $types[$ti] = Types::DATETIME_IMMUTABLE;
+                        break;
+                    case self::TYPE_BLOB:
+                        $types[$ti] = Types::BLOB;
+                        break;
+                    case self::TYPE_CLOB:
+                        $types[$ti] = Types::TEXT;
+                        break;
+                }
+            }
+            $this->doctrineConnection->executeStatement($sql, $params);
+        } else {
+            $this->driver->exec($sql, $params, $types);
+        }
 
         return $this;
     }
@@ -329,7 +389,7 @@ class Bdd
     public function select(string $sql, array $params = [], array $options = []): array|null|SelectParser
     {
         $result = $this->driver->select($sql, $params, $options);
-        if (false === $result){
+        if (false === $result) {
             return null;
         }
         return $result;
@@ -825,7 +885,7 @@ class Bdd
 
 
 
-    public function install(bool $withData=true): self
+    public function install(bool $withData = true): self
     {
         $this->logTitle('Installation de la base de données');
 
@@ -854,7 +914,7 @@ class Bdd
 
 
 
-    public function update(bool $withData = true, bool $withMigration=true): self
+    public function update(bool $withData = true, bool $withMigration = true): self
     {
         $this->logTitle('Mise à jour de la base de données');
 
@@ -1252,7 +1312,7 @@ class Bdd
 
         $schDdl = $ddl->get(Ddl::SCHEMA);
         $sDdl   = $ddl->get(Ddl::SEQUENCE);
-        $tDdl = $ddl->get(Ddl::TABLE);
+        $tDdl   = $ddl->get(Ddl::TABLE);
 
         $this->drop();
         $this->create([Ddl::SCHEMA => $schDdl, Ddl::SEQUENCE => $sDdl, Ddl::TABLE => $tDdl]);
-- 
GitLab