Update copyright
[friendica.git/.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                 } else {
73                         $this->exec("SAVEPOINT LEVEL{$this->_transactionDepth}");
74                 }
75
76                 $this->_transactionDepth++;
77         }
78
79         /**
80          * Commit current transaction
81          *
82          * @return bool|void
83          */
84         public function commit()
85         {
86                 $this->_transactionDepth--;
87
88                 if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
89                         parent::commit();
90                 } else {
91                         $this->exec("RELEASE SAVEPOINT LEVEL{$this->_transactionDepth}");
92                 }
93         }
94
95         /**
96          * Rollback current transaction,
97          *
98          * @throws PDOException if there is no transaction started
99          * @return bool|void
100          */
101         public function rollBack()
102         {
103
104                 if ($this->_transactionDepth == 0) {
105                         throw new PDOException('Rollback error : There is no transaction started');
106                 }
107
108                 $this->_transactionDepth--;
109
110                 if($this->_transactionDepth == 0 || !$this->hasSavepoint()) {
111                         parent::rollBack();
112                 } else {
113                         $this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->_transactionDepth}");
114                 }
115         }
116 }