aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/DbTransaction.php
blob: ed1f9ac84cb5de12b05215a29b586eed1347e145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php


namespace r7r\cms\sys;

class DbTransaction
{
    /** @var Database */
    private $db;

    /** @var bool */
    private $startedHere;

    /**
     * Start a new transaction.
     * @param Database $db
     */
    public function __construct(Database $db)
    {
        $this->db = $db;

        $this->startedHere = !$this->db->getPdo()->inTransaction();
        if ($this->startedHere) {
            $this->db->getPdo()->beginTransaction();
        }
    }

    /**
     * Commit the transaction.
     */
    public function commit(): void
    {
        if ($this->startedHere) {
            $this->db->getPdo()->commit();
        }
    }

    /**
     * Roll the transaction back.
     */
    public function rollback(): void
    {
        if ($this->startedHere) {
            $this->db->getPdo()->rollBack();
        }
    }
}