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
21 use Psr\Log\LoggerInterface
;
24 * Manage savepoints within a transaction
28 class SavepointPostgres
{
29 /** @var DatabasePostgres Establish a savepoint within a transaction */
31 /** @var LoggerInterface */
39 * @param DatabasePostgres $dbw
41 * @param LoggerInterface $logger
43 public function __construct( DatabasePostgres
$dbw, $id, LoggerInterface
$logger ) {
45 $this->logger
= $logger;
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;
55 public function __destruct() {
56 if ( $this->didbegin
) {
57 $this->dbw
->rollback();
58 $this->didbegin
= false;
62 public function commit() {
63 if ( $this->didbegin
) {
64 $this->dbw
->commit( __CLASS__
, DatabasePostgres
::FLUSHING_INTERNAL
);
65 $this->didbegin
= false;
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
) );
73 $this->logger
->debug( sprintf( $msg_failed, $this->id
) );
77 public function savepoint() {
78 $this->query( "SAVEPOINT",
79 "Transaction state: savepoint \"%s\" established.\n",
80 "Transaction state: establishment of savepoint \"%s\" FAILED.\n"
84 public function release() {
85 $this->query( "RELEASE",
86 "Transaction state: savepoint \"%s\" released.\n",
87 "Transaction state: release of savepoint \"%s\" FAILED.\n"
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"
98 public function __toString() {
99 return (string)$this->id
;