SessionManager: Change behavior of getSessionById()
[mediawiki.git] / includes / db / DatabasePostgres.php
blob4d9891e886337597cfe37300f2e2816e76f1d2c1
1 <?php
2 /**
3 * This is the Postgres database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Database
24 class PostgresField implements Field {
25 private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname,
26 $has_default, $default;
28 /**
29 * @param DatabaseBase $db
30 * @param string $table
31 * @param string $field
32 * @return null|PostgresField
34 static function fromText( $db, $table, $field ) {
35 $q = <<<SQL
36 SELECT
37 attnotnull, attlen, conname AS conname,
38 atthasdef,
39 adsrc,
40 COALESCE(condeferred, 'f') AS deferred,
41 COALESCE(condeferrable, 'f') AS deferrable,
42 CASE WHEN typname = 'int2' THEN 'smallint'
43 WHEN typname = 'int4' THEN 'integer'
44 WHEN typname = 'int8' THEN 'bigint'
45 WHEN typname = 'bpchar' THEN 'char'
46 ELSE typname END AS typname
47 FROM pg_class c
48 JOIN pg_namespace n ON (n.oid = c.relnamespace)
49 JOIN pg_attribute a ON (a.attrelid = c.oid)
50 JOIN pg_type t ON (t.oid = a.atttypid)
51 LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
52 LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
53 WHERE relkind = 'r'
54 AND nspname=%s
55 AND relname=%s
56 AND attname=%s;
57 SQL;
59 $table = $db->tableName( $table, 'raw' );
60 $res = $db->query(
61 sprintf( $q,
62 $db->addQuotes( $db->getCoreSchema() ),
63 $db->addQuotes( $table ),
64 $db->addQuotes( $field )
67 $row = $db->fetchObject( $res );
68 if ( !$row ) {
69 return null;
71 $n = new PostgresField;
72 $n->type = $row->typname;
73 $n->nullable = ( $row->attnotnull == 'f' );
74 $n->name = $field;
75 $n->tablename = $table;
76 $n->max_length = $row->attlen;
77 $n->deferrable = ( $row->deferrable == 't' );
78 $n->deferred = ( $row->deferred == 't' );
79 $n->conname = $row->conname;
80 $n->has_default = ( $row->atthasdef === 't' );
81 $n->default = $row->adsrc;
83 return $n;
86 function name() {
87 return $this->name;
90 function tableName() {
91 return $this->tablename;
94 function type() {
95 return $this->type;
98 function isNullable() {
99 return $this->nullable;
102 function maxLength() {
103 return $this->max_length;
106 function is_deferrable() {
107 return $this->deferrable;
110 function is_deferred() {
111 return $this->deferred;
114 function conname() {
115 return $this->conname;
119 * @since 1.19
120 * @return bool|mixed
122 function defaultValue() {
123 if ( $this->has_default ) {
124 return $this->default;
125 } else {
126 return false;
132 * Manage savepoints within a transaction
133 * @ingroup Database
134 * @since 1.19
136 class SavepointPostgres {
137 /** @var DatabasePostgres Establish a savepoint within a transaction */
138 protected $dbw;
139 protected $id;
140 protected $didbegin;
143 * @param DatabaseBase $dbw
144 * @param int $id
146 public function __construct( $dbw, $id ) {
147 $this->dbw = $dbw;
148 $this->id = $id;
149 $this->didbegin = false;
150 /* If we are not in a transaction, we need to be for savepoint trickery */
151 if ( !$dbw->trxLevel() ) {
152 $dbw->begin( "FOR SAVEPOINT" );
153 $this->didbegin = true;
157 public function __destruct() {
158 if ( $this->didbegin ) {
159 $this->dbw->rollback();
160 $this->didbegin = false;
164 public function commit() {
165 if ( $this->didbegin ) {
166 $this->dbw->commit();
167 $this->didbegin = false;
171 protected function query( $keyword, $msg_ok, $msg_failed ) {
172 if ( $this->dbw->doQuery( $keyword . " " . $this->id ) !== false ) {
173 } else {
174 wfDebug( sprintf( $msg_failed, $this->id ) );
178 public function savepoint() {
179 $this->query( "SAVEPOINT",
180 "Transaction state: savepoint \"%s\" established.\n",
181 "Transaction state: establishment of savepoint \"%s\" FAILED.\n"
185 public function release() {
186 $this->query( "RELEASE",
187 "Transaction state: savepoint \"%s\" released.\n",
188 "Transaction state: release of savepoint \"%s\" FAILED.\n"
192 public function rollback() {
193 $this->query( "ROLLBACK TO",
194 "Transaction state: savepoint \"%s\" rolled back.\n",
195 "Transaction state: rollback of savepoint \"%s\" FAILED.\n"
199 public function __toString() {
200 return (string)$this->id;
205 * @ingroup Database
207 class DatabasePostgres extends Database {
208 /** @var resource */
209 protected $mLastResult = null;
211 /** @var int The number of rows affected as an integer */
212 protected $mAffectedRows = null;
214 /** @var int */
215 private $mInsertId = null;
217 /** @var float|string */
218 private $numericVersion = null;
220 /** @var string Connect string to open a PostgreSQL connection */
221 private $connectString;
223 /** @var string */
224 private $mCoreSchema;
226 function getType() {
227 return 'postgres';
230 function cascadingDeletes() {
231 return true;
234 function cleanupTriggers() {
235 return true;
238 function strictIPs() {
239 return true;
242 function realTimestamps() {
243 return true;
246 function implicitGroupby() {
247 return false;
250 function implicitOrderby() {
251 return false;
254 function searchableIPs() {
255 return true;
258 function functionalIndexes() {
259 return true;
262 function hasConstraint( $name ) {
263 $sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
264 "WHERE c.connamespace = n.oid AND conname = '" .
265 pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" .
266 pg_escape_string( $this->mConn, $this->getCoreSchema() ) . "'";
267 $res = $this->doQuery( $sql );
269 return $this->numRows( $res );
273 * Usually aborts on failure
274 * @param string $server
275 * @param string $user
276 * @param string $password
277 * @param string $dbName
278 * @throws DBConnectionError|Exception
279 * @return DatabaseBase|null
281 function open( $server, $user, $password, $dbName ) {
282 # Test for Postgres support, to avoid suppressed fatal error
283 if ( !function_exists( 'pg_connect' ) ) {
284 throw new DBConnectionError(
285 $this,
286 "Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
287 "option? (Note: if you recently installed PHP, you may need to restart your\n" .
288 "webserver and database)\n"
292 global $wgDBport;
294 if ( !strlen( $user ) ) { # e.g. the class is being loaded
295 return null;
298 $this->mServer = $server;
299 $port = $wgDBport;
300 $this->mUser = $user;
301 $this->mPassword = $password;
302 $this->mDBname = $dbName;
304 $connectVars = array(
305 'dbname' => $dbName,
306 'user' => $user,
307 'password' => $password
309 if ( $server != false && $server != '' ) {
310 $connectVars['host'] = $server;
312 if ( $port != false && $port != '' ) {
313 $connectVars['port'] = $port;
315 if ( $this->mFlags & DBO_SSL ) {
316 $connectVars['sslmode'] = 1;
319 $this->connectString = $this->makeConnectionString( $connectVars, PGSQL_CONNECT_FORCE_NEW );
320 $this->close();
321 $this->installErrorHandler();
323 try {
324 $this->mConn = pg_connect( $this->connectString );
325 } catch ( Exception $ex ) {
326 $this->restoreErrorHandler();
327 throw $ex;
330 $phpError = $this->restoreErrorHandler();
332 if ( !$this->mConn ) {
333 wfDebug( "DB connection error\n" );
334 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " .
335 substr( $password, 0, 3 ) . "...\n" );
336 wfDebug( $this->lastError() . "\n" );
337 throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
340 $this->mOpened = true;
342 global $wgCommandLineMode;
343 # If called from the command-line (e.g. importDump), only show errors
344 if ( $wgCommandLineMode ) {
345 $this->doQuery( "SET client_min_messages = 'ERROR'" );
348 $this->query( "SET client_encoding='UTF8'", __METHOD__ );
349 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__ );
350 $this->query( "SET timezone = 'GMT'", __METHOD__ );
351 $this->query( "SET standard_conforming_strings = on", __METHOD__ );
352 if ( $this->getServerVersion() >= 9.0 ) {
353 $this->query( "SET bytea_output = 'escape'", __METHOD__ ); // PHP bug 53127
356 global $wgDBmwschema;
357 $this->determineCoreSchema( $wgDBmwschema );
359 return $this->mConn;
363 * Postgres doesn't support selectDB in the same way MySQL does. So if the
364 * DB name doesn't match the open connection, open a new one
365 * @param string $db
366 * @return bool
368 function selectDB( $db ) {
369 if ( $this->mDBname !== $db ) {
370 return (bool)$this->open( $this->mServer, $this->mUser, $this->mPassword, $db );
371 } else {
372 return true;
376 function makeConnectionString( $vars ) {
377 $s = '';
378 foreach ( $vars as $name => $value ) {
379 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
382 return $s;
386 * Closes a database connection, if it is open
387 * Returns success, true if already closed
388 * @return bool
390 protected function closeConnection() {
391 return pg_close( $this->mConn );
394 public function doQuery( $sql ) {
395 if ( function_exists( 'mb_convert_encoding' ) ) {
396 $sql = mb_convert_encoding( $sql, 'UTF-8' );
398 // Clear previously left over PQresult
399 while ( $res = pg_get_result( $this->mConn ) ) {
400 pg_free_result( $res );
402 if ( pg_send_query( $this->mConn, $sql ) === false ) {
403 throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
405 $this->mLastResult = pg_get_result( $this->mConn );
406 $this->mAffectedRows = null;
407 if ( pg_result_error( $this->mLastResult ) ) {
408 return false;
411 return $this->mLastResult;
414 protected function dumpError() {
415 $diags = array(
416 PGSQL_DIAG_SEVERITY,
417 PGSQL_DIAG_SQLSTATE,
418 PGSQL_DIAG_MESSAGE_PRIMARY,
419 PGSQL_DIAG_MESSAGE_DETAIL,
420 PGSQL_DIAG_MESSAGE_HINT,
421 PGSQL_DIAG_STATEMENT_POSITION,
422 PGSQL_DIAG_INTERNAL_POSITION,
423 PGSQL_DIAG_INTERNAL_QUERY,
424 PGSQL_DIAG_CONTEXT,
425 PGSQL_DIAG_SOURCE_FILE,
426 PGSQL_DIAG_SOURCE_LINE,
427 PGSQL_DIAG_SOURCE_FUNCTION
429 foreach ( $diags as $d ) {
430 wfDebug( sprintf( "PgSQL ERROR(%d): %s\n",
431 $d, pg_result_error_field( $this->mLastResult, $d ) ) );
435 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
436 if ( $tempIgnore ) {
437 /* Check for constraint violation */
438 if ( $errno === '23505' ) {
439 parent::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
441 return;
444 /* Transaction stays in the ERROR state until rolled back */
445 if ( $this->mTrxLevel ) {
446 $ignore = $this->ignoreErrors( true );
447 $this->rollback( __METHOD__ );
448 $this->ignoreErrors( $ignore );
450 parent::reportQueryError( $error, $errno, $sql, $fname, false );
453 function queryIgnore( $sql, $fname = __METHOD__ ) {
454 return $this->query( $sql, $fname, true );
458 * @param stdClass|ResultWrapper $res
459 * @throws DBUnexpectedError
461 function freeResult( $res ) {
462 if ( $res instanceof ResultWrapper ) {
463 $res = $res->result;
465 MediaWiki\suppressWarnings();
466 $ok = pg_free_result( $res );
467 MediaWiki\restoreWarnings();
468 if ( !$ok ) {
469 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
474 * @param ResultWrapper|stdClass $res
475 * @return stdClass
476 * @throws DBUnexpectedError
478 function fetchObject( $res ) {
479 if ( $res instanceof ResultWrapper ) {
480 $res = $res->result;
482 MediaWiki\suppressWarnings();
483 $row = pg_fetch_object( $res );
484 MediaWiki\restoreWarnings();
485 # @todo FIXME: HACK HACK HACK HACK debug
487 # @todo hashar: not sure if the following test really trigger if the object
488 # fetching failed.
489 if ( pg_last_error( $this->mConn ) ) {
490 throw new DBUnexpectedError(
491 $this,
492 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) )
496 return $row;
499 function fetchRow( $res ) {
500 if ( $res instanceof ResultWrapper ) {
501 $res = $res->result;
503 MediaWiki\suppressWarnings();
504 $row = pg_fetch_array( $res );
505 MediaWiki\restoreWarnings();
506 if ( pg_last_error( $this->mConn ) ) {
507 throw new DBUnexpectedError(
508 $this,
509 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) )
513 return $row;
516 function numRows( $res ) {
517 if ( $res instanceof ResultWrapper ) {
518 $res = $res->result;
520 MediaWiki\suppressWarnings();
521 $n = pg_num_rows( $res );
522 MediaWiki\restoreWarnings();
523 if ( pg_last_error( $this->mConn ) ) {
524 throw new DBUnexpectedError(
525 $this,
526 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) )
530 return $n;
533 function numFields( $res ) {
534 if ( $res instanceof ResultWrapper ) {
535 $res = $res->result;
538 return pg_num_fields( $res );
541 function fieldName( $res, $n ) {
542 if ( $res instanceof ResultWrapper ) {
543 $res = $res->result;
546 return pg_field_name( $res, $n );
550 * Return the result of the last call to nextSequenceValue();
551 * This must be called after nextSequenceValue().
553 * @return int|null
555 function insertId() {
556 return $this->mInsertId;
560 * @param mixed $res
561 * @param int $row
562 * @return bool
564 function dataSeek( $res, $row ) {
565 if ( $res instanceof ResultWrapper ) {
566 $res = $res->result;
569 return pg_result_seek( $res, $row );
572 function lastError() {
573 if ( $this->mConn ) {
574 if ( $this->mLastResult ) {
575 return pg_result_error( $this->mLastResult );
576 } else {
577 return pg_last_error();
579 } else {
580 return 'No database connection';
584 function lastErrno() {
585 if ( $this->mLastResult ) {
586 return pg_result_error_field( $this->mLastResult, PGSQL_DIAG_SQLSTATE );
587 } else {
588 return false;
592 function affectedRows() {
593 if ( !is_null( $this->mAffectedRows ) ) {
594 // Forced result for simulated queries
595 return $this->mAffectedRows;
597 if ( empty( $this->mLastResult ) ) {
598 return 0;
601 return pg_affected_rows( $this->mLastResult );
605 * Estimate rows in dataset
606 * Returns estimated count, based on EXPLAIN output
607 * This is not necessarily an accurate estimate, so use sparingly
608 * Returns -1 if count cannot be found
609 * Takes same arguments as Database::select()
611 * @param string $table
612 * @param string $vars
613 * @param string $conds
614 * @param string $fname
615 * @param array $options
616 * @return int
618 function estimateRowCount( $table, $vars = '*', $conds = '',
619 $fname = __METHOD__, $options = array()
621 $options['EXPLAIN'] = true;
622 $res = $this->select( $table, $vars, $conds, $fname, $options );
623 $rows = -1;
624 if ( $res ) {
625 $row = $this->fetchRow( $res );
626 $count = array();
627 if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
628 $rows = (int)$count[1];
632 return $rows;
636 * Returns information about an index
637 * If errors are explicitly ignored, returns NULL on failure
639 * @param string $table
640 * @param string $index
641 * @param string $fname
642 * @return bool|null
644 function indexInfo( $table, $index, $fname = __METHOD__ ) {
645 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
646 $res = $this->query( $sql, $fname );
647 if ( !$res ) {
648 return null;
650 foreach ( $res as $row ) {
651 if ( $row->indexname == $this->indexName( $index ) ) {
652 return $row;
656 return false;
660 * Returns is of attributes used in index
662 * @since 1.19
663 * @param string $index
664 * @param bool|string $schema
665 * @return array
667 function indexAttributes( $index, $schema = false ) {
668 if ( $schema === false ) {
669 $schema = $this->getCoreSchema();
672 * A subquery would be not needed if we didn't care about the order
673 * of attributes, but we do
675 $sql = <<<__INDEXATTR__
677 SELECT opcname,
678 attname,
679 i.indoption[s.g] as option,
680 pg_am.amname
681 FROM
682 (SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
683 FROM
684 pg_index isub
685 JOIN pg_class cis
686 ON cis.oid=isub.indexrelid
687 JOIN pg_namespace ns
688 ON cis.relnamespace = ns.oid
689 WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
690 pg_attribute,
691 pg_opclass opcls,
692 pg_am,
693 pg_class ci
694 JOIN pg_index i
695 ON ci.oid=i.indexrelid
696 JOIN pg_class ct
697 ON ct.oid = i.indrelid
698 JOIN pg_namespace n
699 ON ci.relnamespace = n.oid
700 WHERE
701 ci.relname='$index' AND n.nspname='$schema'
702 AND attrelid = ct.oid
703 AND i.indkey[s.g] = attnum
704 AND i.indclass[s.g] = opcls.oid
705 AND pg_am.oid = opcls.opcmethod
706 __INDEXATTR__;
707 $res = $this->query( $sql, __METHOD__ );
708 $a = array();
709 if ( $res ) {
710 foreach ( $res as $row ) {
711 $a[] = array(
712 $row->attname,
713 $row->opcname,
714 $row->amname,
715 $row->option );
717 } else {
718 return null;
721 return $a;
724 function indexUnique( $table, $index, $fname = __METHOD__ ) {
725 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
726 " AND indexdef LIKE 'CREATE UNIQUE%(" .
727 $this->strencode( $this->indexName( $index ) ) .
728 ")'";
729 $res = $this->query( $sql, $fname );
730 if ( !$res ) {
731 return null;
734 return $res->numRows() > 0;
738 * Change the FOR UPDATE option as necessary based on the join conditions. Then pass
739 * to the parent function to get the actual SQL text.
741 * In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
742 * can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to do
743 * so causes a DB error. This wrapper checks which tables can be locked and adjusts it accordingly.
745 * MySQL uses "ORDER BY NULL" as an optimization hint, but that syntax is illegal in PostgreSQL.
746 * @see DatabaseBase::selectSQLText
748 function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
749 $options = array(), $join_conds = array()
751 if ( is_array( $options ) ) {
752 $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
753 if ( $forUpdateKey !== false && $join_conds ) {
754 unset( $options[$forUpdateKey] );
756 foreach ( $join_conds as $table_cond => $join_cond ) {
757 if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
758 $options['FOR UPDATE'][] = $table_cond;
763 if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
764 unset( $options['ORDER BY'] );
768 return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
772 * INSERT wrapper, inserts an array into a table
774 * $args may be a single associative array, or an array of these with numeric keys,
775 * for multi-row insert (Postgres version 8.2 and above only).
777 * @param string $table Name of the table to insert to.
778 * @param array $args Items to insert into the table.
779 * @param string $fname Name of the function, for profiling
780 * @param array|string $options String or array. Valid options: IGNORE
781 * @return bool Success of insert operation. IGNORE always returns true.
783 function insert( $table, $args, $fname = __METHOD__, $options = array() ) {
784 if ( !count( $args ) ) {
785 return true;
788 $table = $this->tableName( $table );
789 if ( !isset( $this->numericVersion ) ) {
790 $this->getServerVersion();
793 if ( !is_array( $options ) ) {
794 $options = array( $options );
797 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
798 $multi = true;
799 $keys = array_keys( $args[0] );
800 } else {
801 $multi = false;
802 $keys = array_keys( $args );
805 // If IGNORE is set, we use savepoints to emulate mysql's behavior
806 $savepoint = null;
807 if ( in_array( 'IGNORE', $options ) ) {
808 $savepoint = new SavepointPostgres( $this, 'mw' );
809 $olde = error_reporting( 0 );
810 // For future use, we may want to track the number of actual inserts
811 // Right now, insert (all writes) simply return true/false
812 $numrowsinserted = 0;
815 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
817 if ( $multi ) {
818 if ( $this->numericVersion >= 8.2 && !$savepoint ) {
819 $first = true;
820 foreach ( $args as $row ) {
821 if ( $first ) {
822 $first = false;
823 } else {
824 $sql .= ',';
826 $sql .= '(' . $this->makeList( $row ) . ')';
828 $res = (bool)$this->query( $sql, $fname, $savepoint );
829 } else {
830 $res = true;
831 $origsql = $sql;
832 foreach ( $args as $row ) {
833 $tempsql = $origsql;
834 $tempsql .= '(' . $this->makeList( $row ) . ')';
836 if ( $savepoint ) {
837 $savepoint->savepoint();
840 $tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
842 if ( $savepoint ) {
843 $bar = pg_last_error();
844 if ( $bar != false ) {
845 $savepoint->rollback();
846 } else {
847 $savepoint->release();
848 $numrowsinserted++;
852 // If any of them fail, we fail overall for this function call
853 // Note that this will be ignored if IGNORE is set
854 if ( !$tempres ) {
855 $res = false;
859 } else {
860 // Not multi, just a lone insert
861 if ( $savepoint ) {
862 $savepoint->savepoint();
865 $sql .= '(' . $this->makeList( $args ) . ')';
866 $res = (bool)$this->query( $sql, $fname, $savepoint );
867 if ( $savepoint ) {
868 $bar = pg_last_error();
869 if ( $bar != false ) {
870 $savepoint->rollback();
871 } else {
872 $savepoint->release();
873 $numrowsinserted++;
877 if ( $savepoint ) {
878 error_reporting( $olde );
879 $savepoint->commit();
881 // Set the affected row count for the whole operation
882 $this->mAffectedRows = $numrowsinserted;
884 // IGNORE always returns true
885 return true;
888 return $res;
892 * INSERT SELECT wrapper
893 * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
894 * Source items may be literals rather then field names, but strings should
895 * be quoted with Database::addQuotes()
896 * $conds may be "*" to copy the whole table
897 * srcTable may be an array of tables.
898 * @todo FIXME: Implement this a little better (seperate select/insert)?
900 * @param string $destTable
901 * @param array|string $srcTable
902 * @param array $varMap
903 * @param array $conds
904 * @param string $fname
905 * @param array $insertOptions
906 * @param array $selectOptions
907 * @return bool
909 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
910 $insertOptions = array(), $selectOptions = array() ) {
911 $destTable = $this->tableName( $destTable );
913 if ( !is_array( $insertOptions ) ) {
914 $insertOptions = array( $insertOptions );
918 * If IGNORE is set, we use savepoints to emulate mysql's behavior
919 * Ignore LOW PRIORITY option, since it is MySQL-specific
921 $savepoint = null;
922 if ( in_array( 'IGNORE', $insertOptions ) ) {
923 $savepoint = new SavepointPostgres( $this, 'mw' );
924 $olde = error_reporting( 0 );
925 $numrowsinserted = 0;
926 $savepoint->savepoint();
929 if ( !is_array( $selectOptions ) ) {
930 $selectOptions = array( $selectOptions );
932 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
933 if ( is_array( $srcTable ) ) {
934 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
935 } else {
936 $srcTable = $this->tableName( $srcTable );
939 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
940 " SELECT $startOpts " . implode( ',', $varMap ) .
941 " FROM $srcTable $useIndex";
943 if ( $conds != '*' ) {
944 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
947 $sql .= " $tailOpts";
949 $res = (bool)$this->query( $sql, $fname, $savepoint );
950 if ( $savepoint ) {
951 $bar = pg_last_error();
952 if ( $bar != false ) {
953 $savepoint->rollback();
954 } else {
955 $savepoint->release();
956 $numrowsinserted++;
958 error_reporting( $olde );
959 $savepoint->commit();
961 // Set the affected row count for the whole operation
962 $this->mAffectedRows = $numrowsinserted;
964 // IGNORE always returns true
965 return true;
968 return $res;
971 function tableName( $name, $format = 'quoted' ) {
972 # Replace reserved words with better ones
973 switch ( $name ) {
974 case 'user':
975 return $this->realTableName( 'mwuser', $format );
976 case 'text':
977 return $this->realTableName( 'pagecontent', $format );
978 default:
979 return $this->realTableName( $name, $format );
983 /* Don't cheat on installer */
984 function realTableName( $name, $format = 'quoted' ) {
985 return parent::tableName( $name, $format );
989 * Return the next in a sequence, save the value for retrieval via insertId()
991 * @param string $seqName
992 * @return int|null
994 function nextSequenceValue( $seqName ) {
995 $safeseq = str_replace( "'", "''", $seqName );
996 $res = $this->query( "SELECT nextval('$safeseq')" );
997 $row = $this->fetchRow( $res );
998 $this->mInsertId = $row[0];
1000 return $this->mInsertId;
1004 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
1006 * @param string $seqName
1007 * @return int
1009 function currentSequenceValue( $seqName ) {
1010 $safeseq = str_replace( "'", "''", $seqName );
1011 $res = $this->query( "SELECT currval('$safeseq')" );
1012 $row = $this->fetchRow( $res );
1013 $currval = $row[0];
1015 return $currval;
1018 # Returns the size of a text field, or -1 for "unlimited"
1019 function textFieldSize( $table, $field ) {
1020 $table = $this->tableName( $table );
1021 $sql = "SELECT t.typname as ftype,a.atttypmod as size
1022 FROM pg_class c, pg_attribute a, pg_type t
1023 WHERE relname='$table' AND a.attrelid=c.oid AND
1024 a.atttypid=t.oid and a.attname='$field'";
1025 $res = $this->query( $sql );
1026 $row = $this->fetchObject( $res );
1027 if ( $row->ftype == 'varchar' ) {
1028 $size = $row->size - 4;
1029 } else {
1030 $size = $row->size;
1033 return $size;
1036 function limitResult( $sql, $limit, $offset = false ) {
1037 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
1040 function wasDeadlock() {
1041 return $this->lastErrno() == '40P01';
1044 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
1045 $newName = $this->addIdentifierQuotes( $newName );
1046 $oldName = $this->addIdentifierQuotes( $oldName );
1048 return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
1049 "(LIKE $oldName INCLUDING DEFAULTS)", $fname );
1052 function listTables( $prefix = null, $fname = __METHOD__ ) {
1053 $eschema = $this->addQuotes( $this->getCoreSchema() );
1054 $result = $this->query( "SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
1055 $endArray = array();
1057 foreach ( $result as $table ) {
1058 $vars = get_object_vars( $table );
1059 $table = array_pop( $vars );
1060 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
1061 $endArray[] = $table;
1065 return $endArray;
1068 function timestamp( $ts = 0 ) {
1069 return wfTimestamp( TS_POSTGRES, $ts );
1073 * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
1074 * to http://www.php.net/manual/en/ref.pgsql.php
1076 * Parsing a postgres array can be a tricky problem, he's my
1077 * take on this, it handles multi-dimensional arrays plus
1078 * escaping using a nasty regexp to determine the limits of each
1079 * data-item.
1081 * This should really be handled by PHP PostgreSQL module
1083 * @since 1.19
1084 * @param string $text Postgreql array returned in a text form like {a,b}
1085 * @param string $output
1086 * @param int $limit
1087 * @param int $offset
1088 * @return string
1090 function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
1091 if ( false === $limit ) {
1092 $limit = strlen( $text ) - 1;
1093 $output = array();
1095 if ( '{}' == $text ) {
1096 return $output;
1098 do {
1099 if ( '{' != $text[$offset] ) {
1100 preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
1101 $text, $match, 0, $offset );
1102 $offset += strlen( $match[0] );
1103 $output[] = ( '"' != $match[1][0]
1104 ? $match[1]
1105 : stripcslashes( substr( $match[1], 1, -1 ) ) );
1106 if ( '},' == $match[3] ) {
1107 return $output;
1109 } else {
1110 $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
1112 } while ( $limit > $offset );
1114 return $output;
1118 * Return aggregated value function call
1119 * @param array $valuedata
1120 * @param string $valuename
1121 * @return array
1123 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1124 return $valuedata;
1128 * @return string Wikitext of a link to the server software's web site
1130 public function getSoftwareLink() {
1131 return '[{{int:version-db-postgres-url}} PostgreSQL]';
1135 * Return current schema (executes SELECT current_schema())
1136 * Needs transaction
1138 * @since 1.19
1139 * @return string Default schema for the current session
1141 function getCurrentSchema() {
1142 $res = $this->query( "SELECT current_schema()", __METHOD__ );
1143 $row = $this->fetchRow( $res );
1145 return $row[0];
1149 * Return list of schemas which are accessible without schema name
1150 * This is list does not contain magic keywords like "$user"
1151 * Needs transaction
1153 * @see getSearchPath()
1154 * @see setSearchPath()
1155 * @since 1.19
1156 * @return array List of actual schemas for the current sesson
1158 function getSchemas() {
1159 $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
1160 $row = $this->fetchRow( $res );
1161 $schemas = array();
1163 /* PHP pgsql support does not support array type, "{a,b}" string is returned */
1165 return $this->pg_array_parse( $row[0], $schemas );
1169 * Return search patch for schemas
1170 * This is different from getSchemas() since it contain magic keywords
1171 * (like "$user").
1172 * Needs transaction
1174 * @since 1.19
1175 * @return array How to search for table names schemas for the current user
1177 function getSearchPath() {
1178 $res = $this->query( "SHOW search_path", __METHOD__ );
1179 $row = $this->fetchRow( $res );
1181 /* PostgreSQL returns SHOW values as strings */
1183 return explode( ",", $row[0] );
1187 * Update search_path, values should already be sanitized
1188 * Values may contain magic keywords like "$user"
1189 * @since 1.19
1191 * @param array $search_path List of schemas to be searched by default
1193 function setSearchPath( $search_path ) {
1194 $this->query( "SET search_path = " . implode( ", ", $search_path ) );
1198 * Determine default schema for MediaWiki core
1199 * Adjust this session schema search path if desired schema exists
1200 * and is not alread there.
1202 * We need to have name of the core schema stored to be able
1203 * to query database metadata.
1205 * This will be also called by the installer after the schema is created
1207 * @since 1.19
1209 * @param string $desiredSchema
1211 function determineCoreSchema( $desiredSchema ) {
1212 $this->begin( __METHOD__ );
1213 if ( $this->schemaExists( $desiredSchema ) ) {
1214 if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
1215 $this->mCoreSchema = $desiredSchema;
1216 wfDebug( "Schema \"" . $desiredSchema . "\" already in the search path\n" );
1217 } else {
1219 * Prepend our schema (e.g. 'mediawiki') in front
1220 * of the search path
1221 * Fixes bug 15816
1223 $search_path = $this->getSearchPath();
1224 array_unshift( $search_path,
1225 $this->addIdentifierQuotes( $desiredSchema ) );
1226 $this->setSearchPath( $search_path );
1227 $this->mCoreSchema = $desiredSchema;
1228 wfDebug( "Schema \"" . $desiredSchema . "\" added to the search path\n" );
1230 } else {
1231 $this->mCoreSchema = $this->getCurrentSchema();
1232 wfDebug( "Schema \"" . $desiredSchema . "\" not found, using current \"" .
1233 $this->mCoreSchema . "\"\n" );
1235 /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
1236 $this->commit( __METHOD__ );
1240 * Return schema name fore core MediaWiki tables
1242 * @since 1.19
1243 * @return string Core schema name
1245 function getCoreSchema() {
1246 return $this->mCoreSchema;
1250 * @return string Version information from the database
1252 function getServerVersion() {
1253 if ( !isset( $this->numericVersion ) ) {
1254 $versionInfo = pg_version( $this->mConn );
1255 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1256 // Old client, abort install
1257 $this->numericVersion = '7.3 or earlier';
1258 } elseif ( isset( $versionInfo['server'] ) ) {
1259 // Normal client
1260 $this->numericVersion = $versionInfo['server'];
1261 } else {
1262 // Bug 16937: broken pgsql extension from PHP<5.3
1263 $this->numericVersion = pg_parameter_status( $this->mConn, 'server_version' );
1267 return $this->numericVersion;
1271 * Query whether a given relation exists (in the given schema, or the
1272 * default mw one if not given)
1273 * @param string $table
1274 * @param array|string $types
1275 * @param bool|string $schema
1276 * @return bool
1278 function relationExists( $table, $types, $schema = false ) {
1279 if ( !is_array( $types ) ) {
1280 $types = array( $types );
1282 if ( !$schema ) {
1283 $schema = $this->getCoreSchema();
1285 $table = $this->realTableName( $table, 'raw' );
1286 $etable = $this->addQuotes( $table );
1287 $eschema = $this->addQuotes( $schema );
1288 $sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1289 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1290 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1291 $res = $this->query( $sql );
1292 $count = $res ? $res->numRows() : 0;
1294 return (bool)$count;
1298 * For backward compatibility, this function checks both tables and
1299 * views.
1300 * @param string $table
1301 * @param string $fname
1302 * @param bool|string $schema
1303 * @return bool
1305 function tableExists( $table, $fname = __METHOD__, $schema = false ) {
1306 return $this->relationExists( $table, array( 'r', 'v' ), $schema );
1309 function sequenceExists( $sequence, $schema = false ) {
1310 return $this->relationExists( $sequence, 'S', $schema );
1313 function triggerExists( $table, $trigger ) {
1314 $q = <<<SQL
1315 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1316 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1317 AND tgrelid=pg_class.oid
1318 AND nspname=%s AND relname=%s AND tgname=%s
1319 SQL;
1320 $res = $this->query(
1321 sprintf(
1323 $this->addQuotes( $this->getCoreSchema() ),
1324 $this->addQuotes( $table ),
1325 $this->addQuotes( $trigger )
1328 if ( !$res ) {
1329 return null;
1331 $rows = $res->numRows();
1333 return $rows;
1336 function ruleExists( $table, $rule ) {
1337 $exists = $this->selectField( 'pg_rules', 'rulename',
1338 array(
1339 'rulename' => $rule,
1340 'tablename' => $table,
1341 'schemaname' => $this->getCoreSchema()
1345 return $exists === $rule;
1348 function constraintExists( $table, $constraint ) {
1349 $sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
1350 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1351 $this->addQuotes( $this->getCoreSchema() ),
1352 $this->addQuotes( $table ),
1353 $this->addQuotes( $constraint )
1355 $res = $this->query( $sql );
1356 if ( !$res ) {
1357 return null;
1359 $rows = $res->numRows();
1361 return $rows;
1365 * Query whether a given schema exists. Returns true if it does, false if it doesn't.
1366 * @param string $schema
1367 * @return bool
1369 function schemaExists( $schema ) {
1370 $exists = $this->selectField( '"pg_catalog"."pg_namespace"', 1,
1371 array( 'nspname' => $schema ), __METHOD__ );
1373 return (bool)$exists;
1377 * Returns true if a given role (i.e. user) exists, false otherwise.
1378 * @param string $roleName
1379 * @return bool
1381 function roleExists( $roleName ) {
1382 $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
1383 array( 'rolname' => $roleName ), __METHOD__ );
1385 return (bool)$exists;
1388 function fieldInfo( $table, $field ) {
1389 return PostgresField::fromText( $this, $table, $field );
1393 * pg_field_type() wrapper
1394 * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
1395 * @param int $index Field number, starting from 0
1396 * @return string
1398 function fieldType( $res, $index ) {
1399 if ( $res instanceof ResultWrapper ) {
1400 $res = $res->result;
1403 return pg_field_type( $res, $index );
1407 * @param string $b
1408 * @return Blob
1410 function encodeBlob( $b ) {
1411 return new PostgresBlob( pg_escape_bytea( $b ) );
1414 function decodeBlob( $b ) {
1415 if ( $b instanceof PostgresBlob ) {
1416 $b = $b->fetch();
1417 } elseif ( $b instanceof Blob ) {
1418 return $b->fetch();
1421 return pg_unescape_bytea( $b );
1424 function strencode( $s ) {
1425 // Should not be called by us
1427 return pg_escape_string( $this->mConn, $s );
1431 * @param null|bool|Blob $s
1432 * @return int|string
1434 function addQuotes( $s ) {
1435 if ( is_null( $s ) ) {
1436 return 'NULL';
1437 } elseif ( is_bool( $s ) ) {
1438 return intval( $s );
1439 } elseif ( $s instanceof Blob ) {
1440 if ( $s instanceof PostgresBlob ) {
1441 $s = $s->fetch();
1442 } else {
1443 $s = pg_escape_bytea( $this->mConn, $s->fetch() );
1445 return "'$s'";
1448 return "'" . pg_escape_string( $this->mConn, $s ) . "'";
1452 * Postgres specific version of replaceVars.
1453 * Calls the parent version in Database.php
1455 * @param string $ins SQL string, read from a stream (usually tables.sql)
1456 * @return string SQL string
1458 protected function replaceVars( $ins ) {
1459 $ins = parent::replaceVars( $ins );
1461 if ( $this->numericVersion >= 8.3 ) {
1462 // Thanks for not providing backwards-compatibility, 8.3
1463 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
1466 if ( $this->numericVersion <= 8.1 ) { // Our minimum version
1467 $ins = str_replace( 'USING gin', 'USING gist', $ins );
1470 return $ins;
1474 * Various select options
1476 * @param array $options An associative array of options to be turned into
1477 * an SQL query, valid keys are listed in the function.
1478 * @return array
1480 function makeSelectOptions( $options ) {
1481 $preLimitTail = $postLimitTail = '';
1482 $startOpts = $useIndex = '';
1484 $noKeyOptions = array();
1485 foreach ( $options as $key => $option ) {
1486 if ( is_numeric( $key ) ) {
1487 $noKeyOptions[$option] = true;
1491 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1493 $preLimitTail .= $this->makeOrderBy( $options );
1495 // if ( isset( $options['LIMIT'] ) ) {
1496 // $tailOpts .= $this->limitResult( '', $options['LIMIT'],
1497 // isset( $options['OFFSET'] ) ? $options['OFFSET']
1498 // : false );
1499 // }
1501 if ( isset( $options['FOR UPDATE'] ) ) {
1502 $postLimitTail .= ' FOR UPDATE OF ' .
1503 implode( ', ', array_map( array( &$this, 'tableName' ), $options['FOR UPDATE'] ) );
1504 } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1505 $postLimitTail .= ' FOR UPDATE';
1508 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1509 $startOpts .= 'DISTINCT';
1512 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1515 function getDBname() {
1516 return $this->mDBname;
1519 function getServer() {
1520 return $this->mServer;
1523 function buildConcat( $stringList ) {
1524 return implode( ' || ', $stringList );
1527 public function buildGroupConcatField(
1528 $delimiter, $table, $field, $conds = '', $options = array(), $join_conds = array()
1530 $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
1532 return '(' . $this->selectSQLText( $table, $fld, $conds, null, array(), $join_conds ) . ')';
1535 public function getSearchEngine() {
1536 return 'SearchPostgres';
1539 public function streamStatementEnd( &$sql, &$newLine ) {
1540 # Allow dollar quoting for function declarations
1541 if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
1542 if ( $this->delimiter ) {
1543 $this->delimiter = false;
1544 } else {
1545 $this->delimiter = ';';
1549 return parent::streamStatementEnd( $sql, $newLine );
1553 * Check to see if a named lock is available. This is non-blocking.
1554 * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1556 * @param string $lockName Name of lock to poll
1557 * @param string $method Name of method calling us
1558 * @return bool
1559 * @since 1.20
1561 public function lockIsFree( $lockName, $method ) {
1562 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1563 $result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
1564 WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
1565 $row = $this->fetchObject( $result );
1567 return ( $row->lockstatus === 't' );
1571 * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1572 * @param string $lockName
1573 * @param string $method
1574 * @param int $timeout
1575 * @return bool
1577 public function lock( $lockName, $method, $timeout = 5 ) {
1578 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1579 for ( $attempts = 1; $attempts <= $timeout; ++$attempts ) {
1580 $result = $this->query(
1581 "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
1582 $row = $this->fetchObject( $result );
1583 if ( $row->lockstatus === 't' ) {
1584 return true;
1585 } else {
1586 sleep( 1 );
1589 wfDebug( __METHOD__ . " failed to acquire lock\n" );
1591 return false;
1595 * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKSFROM
1596 * PG DOCS: http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1597 * @param string $lockName
1598 * @param string $method
1599 * @return bool
1601 public function unlock( $lockName, $method ) {
1602 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1603 $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
1604 $row = $this->fetchObject( $result );
1606 return ( $row->lockstatus === 't' );
1610 * @param string $lockName
1611 * @return string Integer
1613 private function bigintFromLockName( $lockName ) {
1614 return Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
1616 } // end DatabasePostgres class
1618 class PostgresBlob extends Blob {