From ebbd9e06c06512ba9980cf12cc9a3efcbdbdf3ac Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 10 Apr 2025 15:27:19 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- php/src/db/pgsql/Pgsql.php | 79 ++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 12 deletions(-) diff --git a/php/src/db/pgsql/Pgsql.php b/php/src/db/pgsql/Pgsql.php index 7484cd8..1f3dd1e 100644 --- a/php/src/db/pgsql/Pgsql.php +++ b/php/src/db/pgsql/Pgsql.php @@ -7,6 +7,7 @@ use nulib\db\_private\Tvalues; use nulib\db\IDatabase; use nulib\db\ITransactor; use nulib\php\func; +use nulib\ValueException; class Pgsql implements IDatabase { use Tvalues; @@ -163,39 +164,93 @@ class Pgsql implements IDatabase { return $this->db; } - function exec($query, ?array $params = null) { + /** + * @return resource|false + */ + function _exec(string $query) { + return pg_query($this->db(), $query); + } + + function exec($query, ?array $params=null) { // TODO: Implement exec() method. } - function willUpdate(...$transactors): \nulib\db\ITransactor { - // TODO: Implement willUpdate() method. + /** @var ITransactor[] */ + protected ?array $transactors = null; + + function willUpdate(...$transactors): self { + foreach ($transactors as $transactor) { + if ($transactor instanceof ITransactor) { + $this->transactors[] = $transactor; + $transactor->willUpdate(); + } else { + throw ValueException::invalid_type($transactor, ITransactor::class); + } + } + return $this; } - function inTransaction(): bool { - // TODO: Implement inTransaction() method. + function inTransaction(?bool &$inerror=null): bool { + $status = pg_transaction_status($this->db()); + if ($status === PGSQL_TRANSACTION_ACTIVE || $status === PGSQL_TRANSACTION_INTRANS) { + $inerror = false; + return true; + } elseif ($status === PGSQL_TRANSACTION_INERROR) { + $inerror = true; + return true; + } else { + return false; + } } - function beginTransaction(?callable $func = null, bool $commit = true): void { - // TODO: Implement beginTransaction() method. + function beginTransaction(?callable $func=null, bool $commit=true): void { + $this->_exec("begin"); + if ($this->transactors !== null) { + foreach ($this->transactors as $transactor) { + $transactor->beginTransaction(); + } + } + if ($func !== null) { + $commited = false; + try { + func::call($func, $this); + if ($commit) { + $this->commit(); + $commited = true; + } + } finally { + if ($commit && !$commited) $this->rollback(); + } + } } function commit(): void { - // TODO: Implement commit() method. + $this->_exec("commit"); + if ($this->transactors !== null) { + foreach ($this->transactors as $transactor) { + $transactor->commit(); + } + } } function rollback(): void { - // TODO: Implement rollback() method. + $this->_exec("rollback"); + if ($this->transactors !== null) { + foreach ($this->transactors as $transactor) { + $transactor->rollback(); + } + } } - function get($query, ?array $params = null, bool $entireRow = false) { + function get($query, ?array $params=null, bool $entireRow=false) { // TODO: Implement get() method. } - function one($query, ?array $params = null): ?array { + function one($query, ?array $params=null): ?array { // TODO: Implement one() method. } - function all($query, ?array $params = null, $primaryKeys = null): iterable { + function all($query, ?array $params=null, $primaryKeys=null): iterable { // TODO: Implement all() method. } }