aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/DbTransaction.php
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-10-05 21:44:27 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-10-05 21:44:27 +0200
commit01842f99b65b06d2647470c3b867719e72dabde7 (patch)
tree6ee373d5df46cf1097b06fa37bcfb0dc9b09d811 /ratatoeskr/sys/DbTransaction.php
parent5e347e4efaa81c2108256dc927208cd55dc10baa (diff)
downloadratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.gz
ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.tar.bz2
ratatoeskr-cms-01842f99b65b06d2647470c3b867719e72dabde7.zip
Introduce some new database wrappers
This will allow us to avoid using globals and use the autoloader in the future.
Diffstat (limited to 'ratatoeskr/sys/DbTransaction.php')
-rw-r--r--ratatoeskr/sys/DbTransaction.php47
1 files changed, 47 insertions, 0 deletions
diff --git a/ratatoeskr/sys/DbTransaction.php b/ratatoeskr/sys/DbTransaction.php
new file mode 100644
index 0000000..ed1f9ac
--- /dev/null
+++ b/ratatoeskr/sys/DbTransaction.php
@@ -0,0 +1,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();
+ }
+ }
+}