diff --git a/CHANGELOG.md b/CHANGELOG.md index b894bdd4992d432dc0acf3143f304df9624a62ed..2f538f1d699be308f7d5887340ece02daa56f2de 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 ff407195cc0d114bf7d0e38f7bab95247dd05142..58c1dfd4251a0029af9bf05e354a634d0e32110a 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]);