aboutsummaryrefslogtreecommitdiffstats
path: root/include/dba/dba_transaction.php
blob: 02e9945ca67ce1730ccde6e58b030e30f27a7131 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
 * Class to represent a database transaction.
 *
 * A database transaction is initiated upon construction of an object of this
 * class. The transaction will be automatically rolled back upon destruction
 * unless it has been explicitly committed by calling the `commit` method.
 *
 * Wrapping multiple database operation within a transaction ensures that all
 * (or none) of the operations are successfully completed at the same time.
 *
 * If a transaction is already active when constructing an object of this
 * class, it will _not_ try to initiate a transaction, but constructs an object
 * that will in practice be a stub. This prevents that "nested" transactions
 * will cause problems with the existing active transaction.
 *
 * It also means that any rollbacks or commits perfomed on the "nested"
 * transaction will be ignored, and postponed to the outer transaction is
 * committed or rolled back.
 *
 * Also note that any modification to the database schema will implicitly
 * commit active transactions in most cases, so be careful about relying on
 * transactions in those cases.
 *
 * @Note This class assumes the actual underlying database driver is PDO.
 */
class DbaTransaction {
	private bool $committed = false;
	private bool $active = false;

	/**
	 * Creates a database transaction object.
	 *
	 * If a transaction is already active for this db connection,
	 * no transaction is initiated, and the constructed object will
	 * not perform any commit or rollback actions.
	 */
	public function __construct(private dba_driver $dba) {
		if (! $this->dba->db->inTransaction()) {
			$this->active = $this->dba->db->beginTransaction();
		}
	}

	/**
	 * Roll back the transaction if it is active and not already committed.
	 */
	public function __destruct() {
		if ($this->active && ! $this->committed) {
			$this->dba->db->rollBack();
		}
	}

	/**
	 * Commit the transaction if active.
	 *
	 * This will also mark the transaction as committed, preventing it from
	 * being attempted rolled back on destruction.
	 */
	public function commit(): void {
		if ($this->active && ! $this->committed) {
			$this->committed = $this->dba->db->commit();
		}
	}
}