[TikiWiki-commits] [Git][tikiwiki/tiki][master] [ENH] Expose native PDO transaction methods in PdoDb

Benoit Grégoire (@benoitg) via TikiWiki-cvs <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <697cfd3062de5_3b1841248278d@gitlab-sidekiq-low-urgency-cpu-bound-v2-6ccf9f486c-778n8.mail>

Benoit Grégoire pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
3fa9b8b7 by David Maene at 2026-01-30T18:40:33+00:00
[ENH] Expose native PDO transaction methods in PdoDb
---
* [ENH] Add unit tests for PdoDb transaction methods

* initialize Classe Testes

* [ENH] TikiDb_Transaction: Implement rollback method

See merge request tikiwiki/tiki!8887

- - - - -


3 changed files:

- lib/core/TikiDb/Bridge.php
- lib/core/TikiDb/PdoDb.php
- + lib/test/Core/TikiDb/PdoDbTransactionTest.php


Changes:

=====================================
lib/core/TikiDb/Bridge.php
=====================================
@@ -141,4 +141,19 @@ class TikiDb_Bridge extends TikiDb
     {
         return self::get()->table($tableName, $autoIncrement);
     }
+
+    public function beginTransaction(): bool
+    {
+        return self::get()->beginTransaction();
+    }
+
+    public function commit(): bool|null
+    {
+        return self::get()->commit();
+    }
+
+    public function rollback(): bool|null
+    {
+        return self::get()->rollback();
+    }
 }


=====================================
lib/core/TikiDb/PdoDb.php
=====================================
@@ -229,4 +229,25 @@ class PdoDb extends TikiDb
 
         self::$queryLogHandlerInstalled = true;
     }
+
+    public function beginTransaction(): bool
+    {
+        return $this->db->beginTransaction();
+    }
+
+    public function commit(): bool|null
+    {
+        if ($this->db->inTransaction()) {
+            return $this->db->commit();
+        }
+        return null;
+    }
+
+    public function rollback(): bool|null
+    {
+        if ($this->db->inTransaction()) {
+            return $this->db->rollback();
+        }
+        return null;
+    }
 }


=====================================
lib/test/Core/TikiDb/PdoDbTransactionTest.php
=====================================
@@ -0,0 +1,91 @@
+<?php
+
+// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
+//
+// All Rights Reserved. See copyright.txt for details and a complete list of authors.
+// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
+
+namespace Tiki\Test\Core\TikiDb;
+
+use PHPUnit\Framework\TestCase;
+use TikiDb_Initializer;
+use Tiki\TikiDb\PdoDb;
+
+class PdoDbTransactionTest extends TestCase
+{
+    protected const TABLE = 'test_items';
+
+    /**
+     * @var PdoDb
+     */
+    protected $db;
+
+    protected function setUp(): void
+    {
+        $this->db = $this->getTikiDb();
+        $this->tableCreate();
+    }
+
+    protected function tearDown(): void
+    {
+        $this->tableDrop();
+    }
+
+    public function testCommitTransaction()
+    {
+        $this->db->beginTransaction();
+        $this->db->query("INSERT INTO `" . self::TABLE . "` (name) VALUES (?)", ['Apple']);
+        $this->db->commit();
+
+        $result = $this->db->fetchAll("SELECT * FROM `" . self::TABLE . "`");
+        $this->assertCount(1, $result);
+        $this->assertEquals('Apple', $result[0]['name']);
+    }
+
+    public function testRollbackTransaction()
+    {
+        $this->db->beginTransaction();
+        $this->db->query("INSERT INTO `" . self::TABLE . "` (name) VALUES (?)", ['Banana']);
+        $this->db->rollback();
+
+        $result = $this->db->fetchAll("SELECT * FROM `" . self::TABLE . "`");
+        $this->assertCount(0, $result);
+    }
+
+    public function testCommitWithoutTransactionReturnsNull()
+    {
+        $this->assertNull($this->db->commit());
+    }
+
+    public function testRollbackWithoutTransactionReturnsNull()
+    {
+        $this->assertNull($this->db->rollback());
+    }
+
+    protected function tableCreate(): void
+    {
+        $this->tableDrop();
+        $this->db->query("CREATE TABLE `" . self::TABLE . "` (`id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;");
+    }
+
+    protected function tableDrop(): void
+    {
+        $this->db->query("DROP TABLE IF EXISTS `" . self::TABLE . "`");
+    }
+
+    protected function getTikiDb(): PdoDb
+    {
+        include TIKI_PATH . '/lib/test/local.php';
+
+        $initializer = new TikiDb_Initializer();
+        $initializer->setPreferredConnector('pdo');
+
+        return $initializer->getConnection([
+            'host' => $host_tiki,
+            'user' => $user_tiki,
+            'pass' => $pass_tiki,
+            'dbs'  => $dbs_tiki,
+            'charset' => $client_charset,
+        ]);
+    }
+}



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/3fa9b8b7723c1f6bc57264251c65ec14c5b30626

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/3fa9b8b7723c1f6bc57264251c65ec14c5b30626
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.