]> git.mxchange.org Git - friendica.git/blob - tests/Util/Database/ExtendedPDO.php
Sadly mark incomplete Twitter/ContactEndpoint tests
[friendica.git] / tests / Util / Database / ExtendedPDO.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\Util\Database;
23
24 use PDO;
25 use PDOException;
26
27 /**
28  * This class extends native PDO one but allow nested transactions
29  * by using the SQL statements `SAVEPOINT', 'RELEASE SAVEPOINT' AND 'ROLLBACK SAVEPOINT'
30  */
31 class ExtendedPDO extends PDO
32 {
33         /**
34          * @var array Database drivers that support SAVEPOINT * statements.
35          */
36         protected static $_supportedDrivers = ["pgsql", "mysql"];
37
38         /**
39          * @var int the current transaction depth
40          */
41         protected $_transactionDepth = 0;
42
43         /**
44          * @return int
45          */
46         public function getTransactionDepth()
47         {
48                 return $this->_transactionDepth;
49         }
50
51         /**
52          * Test if database driver support savepoints
53          *
54          * @return bool
55          */
56         protected function hasSavepoint()
57         {
58                 return in_array($this->getAttribute(PDO::ATTR_DRIVER_NAME),
59                         self::$_supportedDrivers);
60         }
61
62
63         /**
64          * Start transaction
65          *
66          * @return bool|void
67          */
68         public function beginTransaction()
69         {
70                 if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
71                         parent::beginTransaction();
72                         $this->_transactionDepth = 0;
73                 } else {
74                         $this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
75                 }
76
77                 $this->_transactionDepth++;
78         }
79
80         /**
81          * Commit current transaction
82          *
83          * @return bool|void
84          */
85         public function commit()
86         {
87                 // We don't want to "really" commit something, so skip the most outer hierarchy
88                 if ($this->_transactionDepth <= 1 && $this->hasSavepoint()) {
89                         $this->_transactionDepth = $this->_transactionDepth <= 0 ? 0 : 1;
90                         return true;
91                 }
92
93                 $this->_transactionDepth--;
94
95                 $this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
96         }
97
98         /**
99          * Rollback current transaction,
100          *
101          * @throws PDOException if there is no transaction started
102          * @return bool|void
103          */
104         public function rollBack()
105         {
106                 $this->_transactionDepth--;
107
108                 if($this->_transactionDepth <= 0 || !$this->hasSavepoint()) {
109                         $this->_transactionDepth = 0;
110                         try {
111                                 parent::rollBack();
112                         } catch (PDOException $e) {
113                                 // this shouldn't happen, but it does ...
114                         }
115                 } else {
116                         $this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
117                 }
118         }
119 }