MediaWiki REL1_31
SavepointPostgres.php
Go to the documentation of this file.
1<?php
22namespace Wikimedia\Rdbms;
23
24use Psr\Log\LoggerInterface;
25
34 protected $dbw;
36 protected $logger;
38 protected $id;
40 protected $didbegin;
41
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}
begin( $fname=__METHOD__, $mode=self::TRANSACTION_EXPLICIT)
Begin a transaction.
trxLevel()
Gets the current transaction level.
Definition Database.php:577
Manage savepoints within a transaction.
DatabasePostgres $dbw
Establish a savepoint within a transaction.
query( $keyword, $msg_ok, $msg_failed)
__construct(DatabasePostgres $dbw, $id, LoggerInterface $logger)
For a write query
Definition database.txt:26