Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[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
22 namespace Wikimedia\Rdbms;
23
24 use Psr\Log\LoggerInterface;
25
26 /**
27 * Manage savepoints within a transaction
28 * @ingroup Database
29 * @since 1.19
30 * @deprecated since 1.31, use IDatabase::startAtomic() and such instead.
31 */
32 class SavepointPostgres {
33 /** @var DatabasePostgres Establish a savepoint within a transaction */
34 protected $dbw;
35 /** @var LoggerInterface */
36 protected $logger;
37 /** @var int */
38 protected $id;
39 /** @var bool */
40 protected $didbegin;
41
42 /**
43 * @param DatabasePostgres $dbw
44 * @param int $id
45 * @param LoggerInterface $logger
46 */
47 public function __construct( DatabasePostgres $dbw, $id, LoggerInterface $logger ) {
48 $this->dbw = $dbw;
49 $this->logger = $logger;
50 $this->id = $id;
51 $this->didbegin = false;
52 /* If we are not in a transaction, we need to be for savepoint trickery */
53 if ( !$dbw->trxLevel() ) {
54 $dbw->begin( __CLASS__, DatabasePostgres::TRANSACTION_INTERNAL );
55 $this->didbegin = true;
56 }
57 }
58
59 public function __destruct() {
60 if ( $this->didbegin ) {
61 $this->dbw->rollback();
62 $this->didbegin = false;
63 }
64 }
65
66 public function commit() {
67 if ( $this->didbegin ) {
68 $this->dbw->commit( __CLASS__, DatabasePostgres::FLUSHING_INTERNAL );
69 $this->didbegin = false;
70 }
71 }
72
73 protected function query( $keyword, $msg_ok, $msg_failed ) {
74 if ( $this->dbw->doQuery( $keyword . " " . $this->id ) !== false ) {
75 $this->logger->debug( sprintf( $msg_ok, $this->id ) );
76 } else {
77 $this->logger->debug( sprintf( $msg_failed, $this->id ) );
78 }
79 }
80
81 public function savepoint() {
82 $this->query( "SAVEPOINT",
83 "Transaction state: savepoint \"%s\" established.\n",
84 "Transaction state: establishment of savepoint \"%s\" FAILED.\n"
85 );
86 }
87
88 public function release() {
89 $this->query( "RELEASE",
90 "Transaction state: savepoint \"%s\" released.\n",
91 "Transaction state: release of savepoint \"%s\" FAILED.\n"
92 );
93 }
94
95 public function rollback() {
96 $this->query( "ROLLBACK TO",
97 "Transaction state: savepoint \"%s\" rolled back.\n",
98 "Transaction state: rollback of savepoint \"%s\" FAILED.\n"
99 );
100 }
101
102 public function __toString() {
103 return (string)$this->id;
104 }
105 }