Merge "Don't fallback from uk to ru"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / utils / SavepointPostgres.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Database
20 */
21 use Psr\Log\LoggerInterface;
22
23 /**
24 * Manage savepoints within a transaction
25 * @ingroup Database
26 * @since 1.19
27 */
28 class SavepointPostgres {
29 /** @var DatabasePostgres Establish a savepoint within a transaction */
30 protected $dbw;
31 /** @var LoggerInterface */
32 protected $logger;
33 /** @var int */
34 protected $id;
35 /** @var bool */
36 protected $didbegin;
37
38 /**
39 * @param DatabasePostgres $dbw
40 * @param int $id
41 * @param LoggerInterface $logger
42 */
43 public function __construct( DatabasePostgres $dbw, $id, LoggerInterface $logger ) {
44 $this->dbw = $dbw;
45 $this->logger = $logger;
46 $this->id = $id;
47 $this->didbegin = false;
48 /* If we are not in a transaction, we need to be for savepoint trickery */
49 if ( !$dbw->trxLevel() ) {
50 $dbw->begin( __CLASS__, DatabasePostgres::TRANSACTION_INTERNAL );
51 $this->didbegin = true;
52 }
53 }
54
55 public function __destruct() {
56 if ( $this->didbegin ) {
57 $this->dbw->rollback();
58 $this->didbegin = false;
59 }
60 }
61
62 public function commit() {
63 if ( $this->didbegin ) {
64 $this->dbw->commit( __CLASS__, DatabasePostgres::FLUSHING_INTERNAL );
65 $this->didbegin = false;
66 }
67 }
68
69 protected function query( $keyword, $msg_ok, $msg_failed ) {
70 if ( $this->dbw->doQuery( $keyword . " " . $this->id ) !== false ) {
71 $this->logger->debug( sprintf( $msg_ok, $this->id ) );
72 } else {
73 $this->logger->debug( sprintf( $msg_failed, $this->id ) );
74 }
75 }
76
77 public function savepoint() {
78 $this->query( "SAVEPOINT",
79 "Transaction state: savepoint \"%s\" established.\n",
80 "Transaction state: establishment of savepoint \"%s\" FAILED.\n"
81 );
82 }
83
84 public function release() {
85 $this->query( "RELEASE",
86 "Transaction state: savepoint \"%s\" released.\n",
87 "Transaction state: release of savepoint \"%s\" FAILED.\n"
88 );
89 }
90
91 public function rollback() {
92 $this->query( "ROLLBACK TO",
93 "Transaction state: savepoint \"%s\" rolled back.\n",
94 "Transaction state: rollback of savepoint \"%s\" FAILED.\n"
95 );
96 }
97
98 public function __toString() {
99 return (string)$this->id;
100 }
101 }