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.
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.
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
22 namespace Wikimedia\Rdbms
;
24 use Psr\Log\LoggerInterface
;
27 * Manage savepoints within a transaction
30 * @deprecated since 1.31, use IDatabase::startAtomic() and such instead.
32 class SavepointPostgres
{
33 /** @var DatabasePostgres Establish a savepoint within a transaction */
35 /** @var LoggerInterface */
43 * @param DatabasePostgres $dbw
45 * @param LoggerInterface $logger
47 public function __construct( DatabasePostgres
$dbw, $id, LoggerInterface
$logger ) {
49 $this->logger
= $logger;
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;
59 public function __destruct() {
60 if ( $this->didbegin
) {
61 $this->dbw
->rollback();
62 $this->didbegin
= false;
66 public function commit() {
67 if ( $this->didbegin
) {
68 $this->dbw
->commit( __CLASS__
, DatabasePostgres
::FLUSHING_INTERNAL
);
69 $this->didbegin
= false;
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
) );
77 $this->logger
->debug( sprintf( $msg_failed, $this->id
) );
81 public function savepoint() {
82 $this->query( "SAVEPOINT",
83 "Transaction state: savepoint \"%s\" established.\n",
84 "Transaction state: establishment of savepoint \"%s\" FAILED.\n"
88 public function release() {
89 $this->query( "RELEASE",
90 "Transaction state: savepoint \"%s\" released.\n",
91 "Transaction state: release of savepoint \"%s\" FAILED.\n"
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"
102 public function __toString() {
103 return (string)$this->id
;