]> git.mxchange.org Git - friendica.git/blob - tests/Util/Database/ExtendedPDO.php
- Fixing SystemResource
[friendica.git] / tests / Util / Database / ExtendedPDO.php
1 <?php
2
3 namespace Friendica\Test\Util\Database;
4
5 use PDO;
6 use PDOException;
7
8 /**
9  * This class extends native PDO one but allow nested transactions
10  * by using the SQL statements `SAVEPOINT', 'RELEASE SAVEPOINT' AND 'ROLLBACK SAVEPOINT'
11  */
12 class ExtendedPDO extends PDO
13 {
14         /**
15          * @var array Database drivers that support SAVEPOINT * statements.
16          */
17         protected static $_supportedDrivers = array("pgsql", "mysql");
18
19         /**
20          * @var int the current transaction depth
21          */
22         protected $_transactionDepth = 0;
23
24         /**
25          * @return int
26          */
27         public function getTransactionDepth()
28         {
29                 return $this->_transactionDepth;
30         }
31
32         /**
33          * Test if database driver support savepoints
34          *
35          * @return bool
36          */
37         protected function hasSavepoint()
38         {
39                 return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME),
40                         self::$_supportedDrivers);
41         }
42
43
44         /**
45          * Start transaction
46          *
47          * @return bool|void
48          */
49         public function beginTransaction()
50         {
51                 if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
52                         parent::beginTransaction();
53                 } else {
54                         $this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
55                 }
56
57                 $this->_transactionDepth++;
58         }
59
60         /**
61          * Commit current transaction
62          *
63          * @return bool|void
64          */
65         public function commit()
66         {
67                 $this->_transactionDepth--;
68
69                 if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
70                         parent::commit();
71                 } else {
72                         $this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
73                 }
74         }
75
76         /**
77          * Rollback current transaction,
78          *
79          * @throws PDOException if there is no transaction started
80          * @return bool|void
81          */
82         public function rollBack()
83         {
84
85                 if ($this->_transactionDepth == 0) {
86                         throw new PDOException('Rollback error : There is no transaction started');
87                 }
88
89                 $this->_transactionDepth--;
90
91                 if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
92                         parent::rollBack();
93                 } else {
94                         $this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
95                 }
96         }
97 }