MediaWiki REL1_30
SavepointPostgres.php
Go to the documentation of this file.
1<?php
22namespace Wikimedia\Rdbms;
23
24use Psr\Log\LoggerInterface;
25
33 protected $dbw;
35 protected $logger;
37 protected $id;
39 protected $didbegin;
40
46 public function __construct( DatabasePostgres $dbw, $id, LoggerInterface $logger ) {
47 $this->dbw = $dbw;
48 $this->logger = $logger;
49 $this->id = $id;
50 $this->didbegin = false;
51 /* If we are not in a transaction, we need to be for savepoint trickery */
52 if ( !$dbw->trxLevel() ) {
53 $dbw->begin( __CLASS__, DatabasePostgres::TRANSACTION_INTERNAL );
54 $this->didbegin = true;
55 }
56 }
57
58 public function __destruct() {
59 if ( $this->didbegin ) {
60 $this->dbw->rollback();
61 $this->didbegin = false;
62 }
63 }
64
65 public function commit() {
66 if ( $this->didbegin ) {
67 $this->dbw->commit( __CLASS__, DatabasePostgres::FLUSHING_INTERNAL );
68 $this->didbegin = false;
69 }
70 }
71
72 protected function query( $keyword, $msg_ok, $msg_failed ) {
73 if ( $this->dbw->doQuery( $keyword . " " . $this->id ) !== false ) {
74 $this->logger->debug( sprintf( $msg_ok, $this->id ) );
75 } else {
76 $this->logger->debug( sprintf( $msg_failed, $this->id ) );
77 }
78 }
79
80 public function savepoint() {
81 $this->query( "SAVEPOINT",
82 "Transaction state: savepoint \"%s\" established.\n",
83 "Transaction state: establishment of savepoint \"%s\" FAILED.\n"
84 );
85 }
86
87 public function release() {
88 $this->query( "RELEASE",
89 "Transaction state: savepoint \"%s\" released.\n",
90 "Transaction state: release of savepoint \"%s\" FAILED.\n"
91 );
92 }
93
94 public function rollback() {
95 $this->query( "ROLLBACK TO",
96 "Transaction state: savepoint \"%s\" rolled back.\n",
97 "Transaction state: rollback of savepoint \"%s\" FAILED.\n"
98 );
99 }
100
101 public function __toString() {
102 return (string)$this->id;
103 }
104}
begin( $fname=__METHOD__, $mode=self::TRANSACTION_EXPLICIT)
Begin a transaction.
trxLevel()
Gets the current transaction level.
Definition Database.php:472
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