2004-06-07 02:59:58 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2006-12-25 22:54:01 +00:00
|
|
|
* This is the Postgres database abstraction layer.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2012-04-26 08:47:10 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-01 21:13:44 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
namespace Wikimedia\Rdbms;
|
|
|
|
|
|
2016-10-02 04:51:51 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2016-10-01 07:07:19 +00:00
|
|
|
use Wikimedia\WaitConditionLoop;
|
2017-02-07 04:49:57 +00:00
|
|
|
use MediaWiki;
|
|
|
|
|
use Exception;
|
2010-08-01 21:13:44 +00:00
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Database
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2016-09-28 23:08:15 +00:00
|
|
|
class DatabasePostgres extends Database {
|
2016-09-19 17:37:57 +00:00
|
|
|
/** @var int|bool */
|
|
|
|
|
protected $port;
|
|
|
|
|
|
2013-12-27 00:10:41 +00:00
|
|
|
/** @var resource */
|
|
|
|
|
protected $mLastResult = null;
|
|
|
|
|
/** @var int The number of rows affected as an integer */
|
|
|
|
|
protected $mAffectedRows = null;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $mInsertId = null;
|
|
|
|
|
/** @var float|string */
|
|
|
|
|
private $numericVersion = null;
|
|
|
|
|
/** @var string Connect string to open a PostgreSQL connection */
|
|
|
|
|
private $connectString;
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $mCoreSchema;
|
2016-11-10 05:35:57 +00:00
|
|
|
/** @var string[] Map of (reserved table name => alternate table name) */
|
|
|
|
|
private $keywordTableMap = [];
|
2004-06-07 02:59:58 +00:00
|
|
|
|
2016-11-10 05:35:57 +00:00
|
|
|
/**
|
|
|
|
|
* @see Database::__construct()
|
|
|
|
|
* @param array $params Additional parameters include:
|
|
|
|
|
* - keywordTableMap : Map of reserved table names to alternative table names to use
|
|
|
|
|
*/
|
2016-09-19 17:37:57 +00:00
|
|
|
public function __construct( array $params ) {
|
|
|
|
|
$this->port = isset( $params['port'] ) ? $params['port'] : false;
|
2016-11-10 05:35:57 +00:00
|
|
|
$this->keywordTableMap = isset( $params['keywordTableMap'] )
|
|
|
|
|
? $params['keywordTableMap']
|
|
|
|
|
: [];
|
|
|
|
|
|
2016-09-19 23:34:32 +00:00
|
|
|
parent::__construct( $params );
|
2016-09-19 17:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getType() {
|
2010-01-08 00:31:24 +00:00
|
|
|
return 'postgres';
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function implicitGroupby() {
|
2007-01-02 21:34:42 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function implicitOrderby() {
|
2007-09-02 18:03:10 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function hasConstraint( $name ) {
|
2016-10-17 18:21:40 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
2014-03-15 11:32:44 +00:00
|
|
|
$sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
|
2013-11-20 10:13:51 +00:00
|
|
|
"WHERE c.connamespace = n.oid AND conname = '" .
|
2016-10-17 18:21:40 +00:00
|
|
|
pg_escape_string( $conn, $name ) . "' AND n.nspname = '" .
|
|
|
|
|
pg_escape_string( $conn, $this->getCoreSchema() ) . "'";
|
2014-03-15 11:32:44 +00:00
|
|
|
$res = $this->doQuery( $sql );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2010-10-14 20:53:04 +00:00
|
|
|
return $this->numRows( $res );
|
2007-09-23 22:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function open( $server, $user, $password, $dbName ) {
|
2006-08-13 14:35:51 +00:00
|
|
|
# Test for Postgres support, to avoid suppressed fatal error
|
2004-07-10 03:09:26 +00:00
|
|
|
if ( !function_exists( 'pg_connect' ) ) {
|
2013-11-20 10:13:51 +00:00
|
|
|
throw new DBConnectionError(
|
|
|
|
|
$this,
|
|
|
|
|
"Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
|
|
|
|
|
"option? (Note: if you recently installed PHP, you may need to restart your\n" .
|
|
|
|
|
"webserver and database)\n"
|
|
|
|
|
);
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-06-07 02:59:58 +00:00
|
|
|
$this->mServer = $server;
|
|
|
|
|
$this->mUser = $user;
|
|
|
|
|
$this->mPassword = $password;
|
|
|
|
|
$this->mDBname = $dbName;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$connectVars = [
|
2017-05-10 01:27:28 +00:00
|
|
|
// pg_connect() user $user as the default database. Since a database is *required*,
|
|
|
|
|
// at least pick a "don't care" database that is more likely to exist. This case
|
|
|
|
|
// arrises when LoadBalancer::getConnection( $i, [], '' ) is used.
|
|
|
|
|
'dbname' => strlen( $dbName ) ? $dbName : 'postgres',
|
2008-10-06 00:45:18 +00:00
|
|
|
'user' => $user,
|
2010-09-05 18:35:34 +00:00
|
|
|
'password' => $password
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( $server != false && $server != '' ) {
|
2008-10-06 00:45:18 +00:00
|
|
|
$connectVars['host'] = $server;
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
2016-09-19 17:37:57 +00:00
|
|
|
if ( (int)$this->port > 0 ) {
|
|
|
|
|
$connectVars['port'] = (int)$this->port;
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
2016-09-23 19:41:22 +00:00
|
|
|
if ( $this->mFlags & self::DBO_SSL ) {
|
2012-08-15 20:44:41 +00:00
|
|
|
$connectVars['sslmode'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->connectString = $this->makeConnectionString( $connectVars );
|
2012-03-08 21:44:03 +00:00
|
|
|
$this->close();
|
2008-10-06 00:45:18 +00:00
|
|
|
$this->installErrorHandler();
|
2013-11-15 15:13:19 +00:00
|
|
|
|
|
|
|
|
try {
|
2016-10-18 01:21:46 +00:00
|
|
|
// Use new connections to let LoadBalancer/LBFactory handle reuse
|
|
|
|
|
$this->mConn = pg_connect( $this->connectString, PGSQL_CONNECT_FORCE_NEW );
|
2013-11-15 15:13:19 +00:00
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
|
$this->restoreErrorHandler();
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-06 00:45:18 +00:00
|
|
|
$phpError = $this->restoreErrorHandler();
|
2006-06-28 18:05:08 +00:00
|
|
|
|
2010-06-09 11:44:05 +00:00
|
|
|
if ( !$this->mConn ) {
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug(
|
2016-10-18 01:21:46 +00:00
|
|
|
"DB connection error\n" .
|
2016-09-19 17:37:57 +00:00
|
|
|
"Server: $server, Database: $dbName, User: $user, Password: " .
|
2016-10-18 01:21:46 +00:00
|
|
|
substr( $password, 0, 3 ) . "...\n"
|
|
|
|
|
);
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug( $this->lastError() . "\n" );
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mOpened = true;
|
|
|
|
|
|
2010-09-05 18:35:34 +00:00
|
|
|
# If called from the command-line (e.g. importDump), only show errors
|
2016-09-19 17:37:57 +00:00
|
|
|
if ( $this->cliMode ) {
|
2009-02-23 21:23:14 +00:00
|
|
|
$this->doQuery( "SET client_min_messages = 'ERROR'" );
|
2006-08-18 16:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
2011-02-23 13:51:00 +00:00
|
|
|
$this->query( "SET client_encoding='UTF8'", __METHOD__ );
|
|
|
|
|
$this->query( "SET datestyle = 'ISO, YMD'", __METHOD__ );
|
|
|
|
|
$this->query( "SET timezone = 'GMT'", __METHOD__ );
|
2011-10-18 21:09:52 +00:00
|
|
|
$this->query( "SET standard_conforming_strings = on", __METHOD__ );
|
2013-02-03 11:10:34 +00:00
|
|
|
if ( $this->getServerVersion() >= 9.0 ) {
|
|
|
|
|
$this->query( "SET bytea_output = 'escape'", __METHOD__ ); // PHP bug 53127
|
|
|
|
|
}
|
2009-02-23 21:23:14 +00:00
|
|
|
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->determineCoreSchema( $this->mSchema );
|
2016-10-25 18:56:41 +00:00
|
|
|
// The schema to be used is now in the search path; no need for explicit qualification
|
2016-10-29 05:14:26 +00:00
|
|
|
$this->mSchema = '';
|
2007-09-19 02:31:28 +00:00
|
|
|
|
2004-06-07 02:59:58 +00:00
|
|
|
return $this->mConn;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2017-05-10 01:27:28 +00:00
|
|
|
public function databasesAreIndependent() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 16:01:22 +00:00
|
|
|
/**
|
|
|
|
|
* Postgres doesn't support selectDB in the same way MySQL does. So if the
|
|
|
|
|
* DB name doesn't match the open connection, open a new one
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $db
|
|
|
|
|
* @return bool
|
2017-05-10 01:27:28 +00:00
|
|
|
* @throws DBUnexpectedError
|
2011-02-23 16:01:22 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function selectDB( $db ) {
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
if ( $this->mDBname !== $db ) {
|
|
|
|
|
return (bool)$this->open( $this->mServer, $this->mUser, $this->mPassword, $db );
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-02-23 16:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
/**
|
|
|
|
|
* @param string[] $vars
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function makeConnectionString( $vars ) {
|
2008-10-06 00:45:18 +00:00
|
|
|
$s = '';
|
|
|
|
|
foreach ( $vars as $name => $value ) {
|
|
|
|
|
$s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-10-06 00:45:18 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-28 14:42:08 +00:00
|
|
|
protected function closeConnection() {
|
2016-10-17 18:21:40 +00:00
|
|
|
return $this->mConn ? pg_close( $this->mConn ) : true;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2012-03-30 00:10:52 +00:00
|
|
|
public function doQuery( $sql ) {
|
2016-10-17 18:21:40 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
2016-01-29 19:42:44 +00:00
|
|
|
$sql = mb_convert_encoding( $sql, 'UTF-8' );
|
2015-09-27 05:56:29 +00:00
|
|
|
// Clear previously left over PQresult
|
2016-10-17 18:21:40 +00:00
|
|
|
while ( $res = pg_get_result( $conn ) ) {
|
2015-09-27 05:56:29 +00:00
|
|
|
pg_free_result( $res );
|
2015-10-14 09:42:00 +00:00
|
|
|
}
|
2016-10-17 18:21:40 +00:00
|
|
|
if ( pg_send_query( $conn, $sql ) === false ) {
|
2012-03-30 03:06:43 +00:00
|
|
|
throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
|
|
|
|
|
}
|
2016-10-17 18:21:40 +00:00
|
|
|
$this->mLastResult = pg_get_result( $conn );
|
2012-03-09 17:24:57 +00:00
|
|
|
$this->mAffectedRows = null;
|
2012-03-30 03:06:43 +00:00
|
|
|
if ( pg_result_error( $this->mLastResult ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-09-06 07:05:14 +00:00
|
|
|
return $this->mLastResult;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-03-18 19:44:43 +00:00
|
|
|
protected function dumpError() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$diags = [
|
2013-11-20 06:58:22 +00:00
|
|
|
PGSQL_DIAG_SEVERITY,
|
|
|
|
|
PGSQL_DIAG_SQLSTATE,
|
|
|
|
|
PGSQL_DIAG_MESSAGE_PRIMARY,
|
|
|
|
|
PGSQL_DIAG_MESSAGE_DETAIL,
|
|
|
|
|
PGSQL_DIAG_MESSAGE_HINT,
|
|
|
|
|
PGSQL_DIAG_STATEMENT_POSITION,
|
|
|
|
|
PGSQL_DIAG_INTERNAL_POSITION,
|
|
|
|
|
PGSQL_DIAG_INTERNAL_QUERY,
|
|
|
|
|
PGSQL_DIAG_CONTEXT,
|
|
|
|
|
PGSQL_DIAG_SOURCE_FILE,
|
|
|
|
|
PGSQL_DIAG_SOURCE_LINE,
|
|
|
|
|
PGSQL_DIAG_SOURCE_FUNCTION
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-03-30 03:06:43 +00:00
|
|
|
foreach ( $diags as $d ) {
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug( sprintf( "PgSQL ERROR(%d): %s\n",
|
2013-11-20 10:13:51 +00:00
|
|
|
$d, pg_result_error_field( $this->mLastResult, $d ) ) );
|
2012-03-30 03:06:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
|
2012-03-30 03:06:43 +00:00
|
|
|
if ( $tempIgnore ) {
|
|
|
|
|
/* Check for constraint violation */
|
|
|
|
|
if ( $errno === '23505' ) {
|
|
|
|
|
parent::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-03-30 03:06:43 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-19 18:42:48 +00:00
|
|
|
/* Transaction stays in the ERROR state until rolled back */
|
2014-05-30 06:42:41 +00:00
|
|
|
if ( $this->mTrxLevel ) {
|
|
|
|
|
$this->rollback( __METHOD__ );
|
2015-03-19 18:42:48 +00:00
|
|
|
}
|
2012-03-30 03:06:43 +00:00
|
|
|
parent::reportQueryError( $error, $errno, $sql, $fname, false );
|
2012-03-08 21:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function freeResult( $res ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-07-04 15:00:30 +00:00
|
|
|
$ok = pg_free_result( $res );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2011-07-04 15:00:30 +00:00
|
|
|
if ( !$ok ) {
|
2010-09-05 18:35:34 +00:00
|
|
|
throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function fetchObject( $res ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-07-04 15:00:30 +00:00
|
|
|
$row = pg_fetch_object( $res );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo FIXME: HACK HACK HACK HACK debug
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo hashar: not sure if the following test really trigger if the object
|
2007-05-15 02:59:20 +00:00
|
|
|
# fetching failed.
|
2016-10-17 18:21:40 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
if ( pg_last_error( $conn ) ) {
|
2013-11-20 10:13:51 +00:00
|
|
|
throw new DBUnexpectedError(
|
|
|
|
|
$this,
|
2016-10-17 18:21:40 +00:00
|
|
|
'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
|
2013-11-20 10:13:51 +00:00
|
|
|
);
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2004-06-07 02:59:58 +00:00
|
|
|
return $row;
|
|
|
|
|
}
|
2004-06-10 13:02:27 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function fetchRow( $res ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-07-04 15:00:30 +00:00
|
|
|
$row = pg_fetch_array( $res );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2016-10-17 18:21:40 +00:00
|
|
|
|
|
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
if ( pg_last_error( $conn ) ) {
|
2013-11-20 10:13:51 +00:00
|
|
|
throw new DBUnexpectedError(
|
|
|
|
|
$this,
|
2016-10-17 18:21:40 +00:00
|
|
|
'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
|
2013-11-20 10:13:51 +00:00
|
|
|
);
|
2006-03-07 01:10:39 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2004-06-10 13:02:27 +00:00
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function numRows( $res ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-07-04 15:00:30 +00:00
|
|
|
$n = pg_num_rows( $res );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2016-10-17 18:21:40 +00:00
|
|
|
|
|
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
if ( pg_last_error( $conn ) ) {
|
2013-11-20 10:13:51 +00:00
|
|
|
throw new DBUnexpectedError(
|
|
|
|
|
$this,
|
2016-10-17 18:21:40 +00:00
|
|
|
'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
|
2013-11-20 10:13:51 +00:00
|
|
|
);
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2004-06-07 02:59:58 +00:00
|
|
|
return $n;
|
|
|
|
|
}
|
2010-09-05 18:35:34 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function numFields( $res ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-07-05 19:42:18 +00:00
|
|
|
return pg_num_fields( $res );
|
|
|
|
|
}
|
2010-09-05 18:35:34 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function fieldName( $res, $n ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-07-05 19:42:18 +00:00
|
|
|
return pg_field_name( $res, $n );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2013-03-26 20:28:11 +00:00
|
|
|
* Return the result of the last call to nextSequenceValue();
|
|
|
|
|
* This must be called after nextSequenceValue().
|
|
|
|
|
*
|
2013-12-27 00:10:41 +00:00
|
|
|
* @return int|null
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function insertId() {
|
2004-07-10 03:09:26 +00:00
|
|
|
return $this->mInsertId;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2004-07-10 03:09:26 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function dataSeek( $res, $row ) {
|
2007-07-05 19:42:18 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-07-05 19:42:18 +00:00
|
|
|
return pg_result_seek( $res, $row );
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function lastError() {
|
2006-06-27 00:53:46 +00:00
|
|
|
if ( $this->mConn ) {
|
2012-03-30 03:06:43 +00:00
|
|
|
if ( $this->mLastResult ) {
|
|
|
|
|
return pg_result_error( $this->mLastResult );
|
|
|
|
|
} else {
|
|
|
|
|
return pg_last_error();
|
|
|
|
|
}
|
2006-06-27 00:53:46 +00:00
|
|
|
}
|
2016-10-18 01:21:46 +00:00
|
|
|
|
|
|
|
|
return $this->getLastPHPError() ?: 'No database connection';
|
2006-06-27 00:53:46 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function lastErrno() {
|
2012-03-30 03:06:43 +00:00
|
|
|
if ( $this->mLastResult ) {
|
|
|
|
|
return pg_result_error_field( $this->mLastResult, PGSQL_DIAG_SQLSTATE );
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-07-21 19:34:45 +00:00
|
|
|
}
|
2004-07-24 07:24:04 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function affectedRows() {
|
2008-09-06 07:05:14 +00:00
|
|
|
if ( !is_null( $this->mAffectedRows ) ) {
|
|
|
|
|
// Forced result for simulated queries
|
|
|
|
|
return $this->mAffectedRows;
|
|
|
|
|
}
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( empty( $this->mLastResult ) ) {
|
2007-05-15 02:59:20 +00:00
|
|
|
return 0;
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-05-16 20:29:05 +00:00
|
|
|
return pg_affected_rows( $this->mLastResult );
|
2004-06-11 14:32:05 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2007-04-07 17:46:17 +00:00
|
|
|
/**
|
|
|
|
|
* Estimate rows in dataset
|
|
|
|
|
* Returns estimated count, based on EXPLAIN output
|
|
|
|
|
* This is not necessarily an accurate estimate, so use sparingly
|
|
|
|
|
* Returns -1 if count cannot be found
|
|
|
|
|
* Takes same arguments as Database::select()
|
2013-12-27 01:54:51 +00:00
|
|
|
*
|
|
|
|
|
* @param string $table
|
|
|
|
|
* @param string $vars
|
|
|
|
|
* @param string $conds
|
|
|
|
|
* @param string $fname
|
|
|
|
|
* @param array $options
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return int
|
2007-04-07 17:46:17 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function estimateRowCount( $table, $vars = '*', $conds = '',
|
2016-02-17 09:09:32 +00:00
|
|
|
$fname = __METHOD__, $options = []
|
2013-11-20 10:13:51 +00:00
|
|
|
) {
|
2007-04-07 17:46:17 +00:00
|
|
|
$options['EXPLAIN'] = true;
|
|
|
|
|
$res = $this->select( $table, $vars, $conds, $fname, $options );
|
|
|
|
|
$rows = -1;
|
|
|
|
|
if ( $res ) {
|
|
|
|
|
$row = $this->fetchRow( $res );
|
2016-02-17 09:09:32 +00:00
|
|
|
$count = [];
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
|
2015-01-22 15:36:18 +00:00
|
|
|
$rows = (int)$count[1];
|
2007-04-07 17:46:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-04-07 17:46:17 +00:00
|
|
|
return $rows;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function indexInfo( $table, $index, $fname = __METHOD__ ) {
|
2004-06-09 16:13:46 +00:00
|
|
|
$sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
|
2004-07-10 03:09:26 +00:00
|
|
|
$res = $this->query( $sql, $fname );
|
2004-06-07 02:59:58 +00:00
|
|
|
if ( !$res ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2010-10-13 23:11:40 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-01-19 13:56:08 +00:00
|
|
|
if ( $row->indexname == $this->indexName( $index ) ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
return $row;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2004-07-10 03:09:26 +00:00
|
|
|
return false;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function indexAttributes( $index, $schema = false ) {
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $schema === false ) {
|
2012-04-13 18:55:14 +00:00
|
|
|
$schema = $this->getCoreSchema();
|
2013-04-20 20:28:52 +00:00
|
|
|
}
|
2012-04-13 18:55:14 +00:00
|
|
|
/*
|
|
|
|
|
* A subquery would be not needed if we didn't care about the order
|
|
|
|
|
* of attributes, but we do
|
|
|
|
|
*/
|
|
|
|
|
$sql = <<<__INDEXATTR__
|
|
|
|
|
|
|
|
|
|
SELECT opcname,
|
|
|
|
|
attname,
|
|
|
|
|
i.indoption[s.g] as option,
|
|
|
|
|
pg_am.amname
|
|
|
|
|
FROM
|
2012-04-19 09:59:55 +00:00
|
|
|
(SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
|
2012-04-13 18:55:14 +00:00
|
|
|
FROM
|
|
|
|
|
pg_index isub
|
|
|
|
|
JOIN pg_class cis
|
|
|
|
|
ON cis.oid=isub.indexrelid
|
|
|
|
|
JOIN pg_namespace ns
|
|
|
|
|
ON cis.relnamespace = ns.oid
|
|
|
|
|
WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
|
|
|
|
|
pg_attribute,
|
|
|
|
|
pg_opclass opcls,
|
|
|
|
|
pg_am,
|
|
|
|
|
pg_class ci
|
|
|
|
|
JOIN pg_index i
|
|
|
|
|
ON ci.oid=i.indexrelid
|
|
|
|
|
JOIN pg_class ct
|
|
|
|
|
ON ct.oid = i.indrelid
|
|
|
|
|
JOIN pg_namespace n
|
|
|
|
|
ON ci.relnamespace = n.oid
|
|
|
|
|
WHERE
|
|
|
|
|
ci.relname='$index' AND n.nspname='$schema'
|
|
|
|
|
AND attrelid = ct.oid
|
|
|
|
|
AND i.indkey[s.g] = attnum
|
|
|
|
|
AND i.indclass[s.g] = opcls.oid
|
|
|
|
|
AND pg_am.oid = opcls.opcmethod
|
|
|
|
|
__INDEXATTR__;
|
2013-02-03 18:47:42 +00:00
|
|
|
$res = $this->query( $sql, __METHOD__ );
|
2016-02-17 09:09:32 +00:00
|
|
|
$a = [];
|
2012-04-13 18:55:14 +00:00
|
|
|
if ( $res ) {
|
|
|
|
|
foreach ( $res as $row ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$a[] = [
|
2012-04-13 18:55:14 +00:00
|
|
|
$row->attname,
|
|
|
|
|
$row->opcname,
|
|
|
|
|
$row->amname,
|
2016-02-17 09:09:32 +00:00
|
|
|
$row->option ];
|
2012-04-13 18:55:14 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-04-13 18:55:14 +00:00
|
|
|
return $a;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function indexUnique( $table, $index, $fname = __METHOD__ ) {
|
2013-04-13 11:36:24 +00:00
|
|
|
$sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
|
2010-09-05 18:00:33 +00:00
|
|
|
" AND indexdef LIKE 'CREATE UNIQUE%(" .
|
2009-01-19 13:56:08 +00:00
|
|
|
$this->strencode( $this->indexName( $index ) ) .
|
2009-01-15 14:20:28 +00:00
|
|
|
")'";
|
2004-09-09 07:12:11 +00:00
|
|
|
$res = $this->query( $sql, $fname );
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( !$res ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-12-27 00:52:09 +00:00
|
|
|
return $res->numRows() > 0;
|
2004-09-09 07:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function selectSQLText(
|
2016-09-19 17:37:57 +00:00
|
|
|
$table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
|
2014-02-05 10:20:17 +00:00
|
|
|
) {
|
2016-09-19 17:37:57 +00:00
|
|
|
// Change the FOR UPDATE option as necessary based on the join conditions. Then pass
|
|
|
|
|
// to the parent function to get the actual SQL text.
|
|
|
|
|
// In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
|
|
|
|
|
// can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to
|
|
|
|
|
// do so causes a DB error. This wrapper checks which tables can be locked and adjusts it
|
|
|
|
|
// accordingly.
|
|
|
|
|
// MySQL uses "ORDER BY NULL" as an optimization hint, but that is illegal in PostgreSQL.
|
2014-01-26 16:49:16 +00:00
|
|
|
if ( is_array( $options ) ) {
|
2014-04-27 07:24:25 +00:00
|
|
|
$forUpdateKey = array_search( 'FOR UPDATE', $options, true );
|
2014-01-26 16:49:16 +00:00
|
|
|
if ( $forUpdateKey !== false && $join_conds ) {
|
|
|
|
|
unset( $options[$forUpdateKey] );
|
|
|
|
|
|
2014-01-28 16:52:57 +00:00
|
|
|
foreach ( $join_conds as $table_cond => $join_cond ) {
|
2014-01-26 16:49:16 +00:00
|
|
|
if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
|
2014-01-28 16:52:57 +00:00
|
|
|
$options['FOR UPDATE'][] = $table_cond;
|
2014-01-26 16:49:16 +00:00
|
|
|
}
|
2013-06-20 19:20:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-08 20:09:25 +00:00
|
|
|
|
|
|
|
|
if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
|
|
|
|
|
unset( $options['ORDER BY'] );
|
|
|
|
|
}
|
2013-06-20 19:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-15 12:14:20 +00:00
|
|
|
/**
|
|
|
|
|
* INSERT wrapper, inserts an array into a table
|
|
|
|
|
*
|
2008-04-14 07:45:50 +00:00
|
|
|
* $args may be a single associative array, or an array of these with numeric keys,
|
2007-05-15 12:14:20 +00:00
|
|
|
* for multi-row insert (Postgres version 8.2 and above only).
|
|
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table Name of the table to insert to.
|
|
|
|
|
* @param array $args Items to insert into the table.
|
|
|
|
|
* @param string $fname Name of the function, for profiling
|
|
|
|
|
* @param array|string $options String or array. Valid options: IGNORE
|
2007-05-15 12:14:20 +00:00
|
|
|
* @return bool Success of insert operation. IGNORE always returns true.
|
|
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
|
2008-05-02 15:28:10 +00:00
|
|
|
if ( !count( $args ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-15 12:14:20 +00:00
|
|
|
$table = $this->tableName( $table );
|
2013-12-27 00:10:41 +00:00
|
|
|
if ( !isset( $this->numericVersion ) ) {
|
2010-07-25 22:09:34 +00:00
|
|
|
$this->getServerVersion();
|
2007-05-15 12:14:20 +00:00
|
|
|
}
|
2004-07-18 08:48:43 +00:00
|
|
|
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( !is_array( $options ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$options = [ $options ];
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2004-09-01 12:27:57 +00:00
|
|
|
|
2007-05-15 12:14:20 +00:00
|
|
|
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
|
2007-05-15 02:59:20 +00:00
|
|
|
$multi = true;
|
2007-05-15 12:14:20 +00:00
|
|
|
$keys = array_keys( $args[0] );
|
2010-09-05 18:35:34 +00:00
|
|
|
} else {
|
2007-05-15 02:59:20 +00:00
|
|
|
$multi = false;
|
2007-05-15 12:14:20 +00:00
|
|
|
$keys = array_keys( $args );
|
2007-05-15 02:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-05 17:00:01 +00:00
|
|
|
// If IGNORE is set, we use savepoints to emulate mysql's behavior
|
2016-09-19 17:37:57 +00:00
|
|
|
$savepoint = $olde = null;
|
|
|
|
|
$numrowsinserted = 0;
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( in_array( 'IGNORE', $options ) ) {
|
2016-09-19 17:37:57 +00:00
|
|
|
$savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
|
2007-05-15 12:14:20 +00:00
|
|
|
$olde = error_reporting( 0 );
|
2008-07-05 17:00:01 +00:00
|
|
|
// For future use, we may want to track the number of actual inserts
|
|
|
|
|
// Right now, insert (all writes) simply return true/false
|
2008-07-04 15:57:44 +00:00
|
|
|
}
|
2008-07-05 17:00:01 +00:00
|
|
|
|
2007-05-15 02:59:20 +00:00
|
|
|
$sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
|
|
|
|
|
|
|
|
|
|
if ( $multi ) {
|
2013-12-27 00:10:41 +00:00
|
|
|
if ( $this->numericVersion >= 8.2 && !$savepoint ) {
|
2007-05-15 02:59:20 +00:00
|
|
|
$first = true;
|
2007-05-15 12:14:20 +00:00
|
|
|
foreach ( $args as $row ) {
|
2007-05-15 02:59:20 +00:00
|
|
|
if ( $first ) {
|
|
|
|
|
$first = false;
|
|
|
|
|
} else {
|
|
|
|
|
$sql .= ',';
|
|
|
|
|
}
|
|
|
|
|
$sql .= '(' . $this->makeList( $row ) . ')';
|
|
|
|
|
}
|
2012-03-30 00:10:52 +00:00
|
|
|
$res = (bool)$this->query( $sql, $fname, $savepoint );
|
2010-09-05 18:35:34 +00:00
|
|
|
} else {
|
2007-05-15 02:59:20 +00:00
|
|
|
$res = true;
|
|
|
|
|
$origsql = $sql;
|
2007-05-15 12:14:20 +00:00
|
|
|
foreach ( $args as $row ) {
|
2007-05-15 02:59:20 +00:00
|
|
|
$tempsql = $origsql;
|
|
|
|
|
$tempsql .= '(' . $this->makeList( $row ) . ')';
|
2008-07-05 17:00:01 +00:00
|
|
|
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( $savepoint ) {
|
|
|
|
|
$savepoint->savepoint();
|
2008-07-05 17:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-30 00:10:52 +00:00
|
|
|
$tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
|
2008-07-05 17:00:01 +00:00
|
|
|
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( $savepoint ) {
|
2016-02-09 16:38:21 +00:00
|
|
|
$bar = pg_result_error( $this->mLastResult );
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( $bar != false ) {
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->rollback();
|
2010-09-05 18:35:34 +00:00
|
|
|
} else {
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->release();
|
2008-07-05 17:00:01 +00:00
|
|
|
$numrowsinserted++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If any of them fail, we fail overall for this function call
|
|
|
|
|
// Note that this will be ignored if IGNORE is set
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( !$tempres ) {
|
2007-05-15 02:59:20 +00:00
|
|
|
$res = false;
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2007-05-15 02:59:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-05 18:35:34 +00:00
|
|
|
} else {
|
2008-07-05 17:00:01 +00:00
|
|
|
// Not multi, just a lone insert
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( $savepoint ) {
|
|
|
|
|
$savepoint->savepoint();
|
2008-07-05 17:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-15 12:14:20 +00:00
|
|
|
$sql .= '(' . $this->makeList( $args ) . ')';
|
2012-03-30 00:10:52 +00:00
|
|
|
$res = (bool)$this->query( $sql, $fname, $savepoint );
|
|
|
|
|
if ( $savepoint ) {
|
2016-02-09 16:38:21 +00:00
|
|
|
$bar = pg_result_error( $this->mLastResult );
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( $bar != false ) {
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->rollback();
|
2010-09-05 18:35:34 +00:00
|
|
|
} else {
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->release();
|
2008-07-05 17:00:01 +00:00
|
|
|
$numrowsinserted++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2012-03-30 00:10:52 +00:00
|
|
|
if ( $savepoint ) {
|
2013-12-27 00:52:09 +00:00
|
|
|
error_reporting( $olde );
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->commit();
|
2008-07-05 17:00:01 +00:00
|
|
|
|
2008-09-06 07:05:14 +00:00
|
|
|
// Set the affected row count for the whole operation
|
|
|
|
|
$this->mAffectedRows = $numrowsinserted;
|
|
|
|
|
|
2008-07-05 17:00:01 +00:00
|
|
|
// IGNORE always returns true
|
2007-05-15 12:14:20 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2007-05-15 02:59:20 +00:00
|
|
|
|
|
|
|
|
return $res;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2009-10-27 05:33:31 +00:00
|
|
|
/**
|
|
|
|
|
* INSERT SELECT wrapper
|
2016-08-07 10:27:38 +00:00
|
|
|
* $varMap must be an associative array of the form [ 'dest1' => 'source1', ... ]
|
2013-11-20 10:13:51 +00:00
|
|
|
* Source items may be literals rather then field names, but strings should
|
|
|
|
|
* be quoted with Database::addQuotes()
|
2009-10-27 05:33:31 +00:00
|
|
|
* $conds may be "*" to copy the whole table
|
|
|
|
|
* srcTable may be an array of tables.
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: Implement this a little better (seperate select/insert)?
|
2013-12-27 01:54:51 +00:00
|
|
|
*
|
|
|
|
|
* @param string $destTable
|
|
|
|
|
* @param array|string $srcTable
|
|
|
|
|
* @param array $varMap
|
|
|
|
|
* @param array $conds
|
|
|
|
|
* @param string $fname
|
|
|
|
|
* @param array $insertOptions
|
|
|
|
|
* @param array $selectOptions
|
2017-06-09 16:58:09 +00:00
|
|
|
* @param array $selectJoinConds
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2010-09-05 18:35:34 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function nativeInsertSelect(
|
|
|
|
|
$destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
|
2017-06-09 16:58:09 +00:00
|
|
|
$insertOptions = [], $selectOptions = [], $selectJoinConds = []
|
2016-10-29 05:14:26 +00:00
|
|
|
) {
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( !is_array( $insertOptions ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$insertOptions = [ $insertOptions ];
|
2009-10-27 05:33:31 +00:00
|
|
|
}
|
2012-06-18 18:39:08 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If IGNORE is set, we use savepoints to emulate mysql's behavior
|
|
|
|
|
* Ignore LOW PRIORITY option, since it is MySQL-specific
|
|
|
|
|
*/
|
2016-09-19 17:37:57 +00:00
|
|
|
$savepoint = $olde = null;
|
|
|
|
|
$numrowsinserted = 0;
|
2012-06-18 18:39:08 +00:00
|
|
|
if ( in_array( 'IGNORE', $insertOptions ) ) {
|
2016-09-19 17:37:57 +00:00
|
|
|
$savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
|
2012-06-18 18:39:08 +00:00
|
|
|
$olde = error_reporting( 0 );
|
|
|
|
|
$savepoint->savepoint();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-09 16:58:09 +00:00
|
|
|
$res = parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
|
|
|
|
|
$insertOptions, $selectOptions, $selectJoinConds );
|
2009-10-27 05:33:31 +00:00
|
|
|
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $savepoint ) {
|
2016-02-09 16:38:21 +00:00
|
|
|
$bar = pg_result_error( $this->mLastResult );
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $bar != false ) {
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->rollback();
|
2009-10-27 05:33:31 +00:00
|
|
|
} else {
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->release();
|
2009-10-27 05:33:31 +00:00
|
|
|
$numrowsinserted++;
|
|
|
|
|
}
|
2013-12-27 00:52:09 +00:00
|
|
|
error_reporting( $olde );
|
2012-03-30 00:10:52 +00:00
|
|
|
$savepoint->commit();
|
2009-10-27 05:33:31 +00:00
|
|
|
|
|
|
|
|
// Set the affected row count for the whole operation
|
|
|
|
|
$this->mAffectedRows = $numrowsinserted;
|
|
|
|
|
|
|
|
|
|
// IGNORE always returns true
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
|
}
|
2010-09-05 18:00:33 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function tableName( $name, $format = 'quoted' ) {
|
2016-10-25 18:56:41 +00:00
|
|
|
// Replace reserved words with better ones
|
|
|
|
|
$name = $this->remappedTableName( $name );
|
|
|
|
|
|
|
|
|
|
return parent::tableName( $name, $format );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string Value of $name or remapped name if $name is a reserved keyword
|
|
|
|
|
*/
|
|
|
|
|
public function remappedTableName( $name ) {
|
2016-11-10 05:35:57 +00:00
|
|
|
return isset( $this->keywordTableMap[$name] ) ? $this->keywordTableMap[$name] : $name;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-25 18:56:41 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string $format
|
|
|
|
|
* @return string Qualified and encoded (if requested) table name
|
|
|
|
|
*/
|
|
|
|
|
public function realTableName( $name, $format = 'quoted' ) {
|
2012-04-13 18:55:14 +00:00
|
|
|
return parent::tableName( $name, $format );
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function nextSequenceValue( $seqName ) {
|
2010-12-04 09:27:02 +00:00
|
|
|
$safeseq = str_replace( "'", "''", $seqName );
|
2006-07-05 03:59:54 +00:00
|
|
|
$res = $this->query( "SELECT nextval('$safeseq')" );
|
|
|
|
|
$row = $this->fetchRow( $res );
|
2017-05-26 21:38:07 +00:00
|
|
|
$this->mInsertId = is_null( $row[0] ) ? null : (int)$row[0];
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2006-07-05 03:59:54 +00:00
|
|
|
return $this->mInsertId;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2004-06-07 02:59:58 +00:00
|
|
|
|
2007-12-18 15:44:18 +00:00
|
|
|
/**
|
2009-11-27 15:19:05 +00:00
|
|
|
* Return the current value of a sequence. Assumes it has been nextval'ed in this session.
|
2013-12-27 00:10:41 +00:00
|
|
|
*
|
|
|
|
|
* @param string $seqName
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return int
|
2007-12-18 15:44:18 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function currentSequenceValue( $seqName ) {
|
2010-12-04 09:27:02 +00:00
|
|
|
$safeseq = str_replace( "'", "''", $seqName );
|
2007-12-18 15:44:18 +00:00
|
|
|
$res = $this->query( "SELECT currval('$safeseq')" );
|
|
|
|
|
$row = $this->fetchRow( $res );
|
|
|
|
|
$currval = $row[0];
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-12-18 15:44:18 +00:00
|
|
|
return $currval;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function textFieldSize( $table, $field ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
$table = $this->tableName( $table );
|
2004-09-11 14:08:52 +00:00
|
|
|
$sql = "SELECT t.typname as ftype,a.atttypmod as size
|
2006-01-07 13:09:30 +00:00
|
|
|
FROM pg_class c, pg_attribute a, pg_type t
|
|
|
|
|
WHERE relname='$table' AND a.attrelid=c.oid AND
|
2004-09-11 14:08:52 +00:00
|
|
|
a.atttypid=t.oid and a.attname='$field'";
|
2013-04-13 11:36:24 +00:00
|
|
|
$res = $this->query( $sql );
|
2010-09-05 18:35:34 +00:00
|
|
|
$row = $this->fetchObject( $res );
|
|
|
|
|
if ( $row->ftype == 'varchar' ) {
|
|
|
|
|
$size = $row->size - 4;
|
2004-09-11 14:08:52 +00:00
|
|
|
} else {
|
2010-09-05 18:35:34 +00:00
|
|
|
$size = $row->size;
|
2004-09-11 14:08:52 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2004-07-10 03:09:26 +00:00
|
|
|
return $size;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function limitResult( $sql, $limit, $offset = false ) {
|
2010-09-05 18:35:34 +00:00
|
|
|
return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
|
2004-07-15 15:09:32 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function wasDeadlock() {
|
2006-12-25 22:54:01 +00:00
|
|
|
return $this->lastErrno() == '40P01';
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
2004-08-10 11:12:18 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function duplicateTableStructure(
|
2016-09-19 17:37:57 +00:00
|
|
|
$oldName, $newName, $temporary = false, $fname = __METHOD__
|
|
|
|
|
) {
|
2011-04-12 18:54:51 +00:00
|
|
|
$newName = $this->addIdentifierQuotes( $newName );
|
|
|
|
|
$oldName = $this->addIdentifierQuotes( $oldName );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-11-20 10:13:51 +00:00
|
|
|
return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
|
2017-05-17 02:20:42 +00:00
|
|
|
"(LIKE $oldName INCLUDING DEFAULTS INCLUDING INDEXES)", $fname );
|
2009-11-06 10:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function listTables( $prefix = null, $fname = __METHOD__ ) {
|
2012-03-09 17:24:57 +00:00
|
|
|
$eschema = $this->addQuotes( $this->getCoreSchema() );
|
2016-09-19 17:37:57 +00:00
|
|
|
$result = $this->query(
|
|
|
|
|
"SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
|
2016-02-17 09:09:32 +00:00
|
|
|
$endArray = [];
|
2011-10-24 19:28:31 +00:00
|
|
|
|
2013-04-20 20:28:52 +00:00
|
|
|
foreach ( $result as $table ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
$vars = get_object_vars( $table );
|
2011-10-24 19:28:31 +00:00
|
|
|
$table = array_pop( $vars );
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
|
2011-10-24 19:28:31 +00:00
|
|
|
$endArray[] = $table;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $endArray;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function timestamp( $ts = 0 ) {
|
2016-09-22 04:43:32 +00:00
|
|
|
$ct = new ConvertibleTimestamp( $ts );
|
2016-09-19 17:37:57 +00:00
|
|
|
|
|
|
|
|
return $ct->getTimestamp( TS_POSTGRES );
|
2004-09-08 20:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-13 19:26:20 +00:00
|
|
|
/**
|
2012-03-09 17:24:57 +00:00
|
|
|
* Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
|
2016-10-13 05:34:26 +00:00
|
|
|
* to https://secure.php.net/manual/en/ref.pgsql.php
|
2012-03-09 18:55:12 +00:00
|
|
|
*
|
|
|
|
|
* Parsing a postgres array can be a tricky problem, he's my
|
|
|
|
|
* take on this, it handles multi-dimensional arrays plus
|
|
|
|
|
* escaping using a nasty regexp to determine the limits of each
|
|
|
|
|
* data-item.
|
2012-03-09 17:24:57 +00:00
|
|
|
*
|
|
|
|
|
* This should really be handled by PHP PostgreSQL module
|
|
|
|
|
*
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $text Postgreql array returned in a text form like {a,b}
|
2017-02-07 04:49:57 +00:00
|
|
|
* @param string[] $output
|
2016-09-19 17:37:57 +00:00
|
|
|
* @param int|bool $limit
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param int $offset
|
2017-02-07 04:49:57 +00:00
|
|
|
* @return string[]
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( false === $limit ) {
|
2013-04-13 11:36:24 +00:00
|
|
|
$limit = strlen( $text ) - 1;
|
2016-02-17 09:09:32 +00:00
|
|
|
$output = [];
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( '{}' == $text ) {
|
2012-03-09 19:05:29 +00:00
|
|
|
return $output;
|
|
|
|
|
}
|
2012-03-09 17:24:57 +00:00
|
|
|
do {
|
2013-05-22 15:59:59 +00:00
|
|
|
if ( '{' != $text[$offset] ) {
|
2012-03-09 17:24:57 +00:00
|
|
|
preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
|
|
|
|
|
$text, $match, 0, $offset );
|
|
|
|
|
$offset += strlen( $match[0] );
|
2013-05-22 15:59:59 +00:00
|
|
|
$output[] = ( '"' != $match[1][0]
|
2013-11-20 06:58:22 +00:00
|
|
|
? $match[1]
|
|
|
|
|
: stripcslashes( substr( $match[1], 1, -1 ) ) );
|
2012-03-09 19:05:29 +00:00
|
|
|
if ( '},' == $match[3] ) {
|
2012-03-09 17:24:57 +00:00
|
|
|
return $output;
|
2012-03-09 19:05:29 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2013-04-13 11:36:24 +00:00
|
|
|
$offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
|
2012-03-09 19:05:29 +00:00
|
|
|
}
|
2012-03-09 17:24:57 +00:00
|
|
|
} while ( $limit > $offset );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-03-09 17:24:57 +00:00
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-16 17:56:00 +00:00
|
|
|
public function aggregateValue( $valuedata, $valuename = 'value' ) {
|
2006-03-07 01:10:39 +00:00
|
|
|
return $valuedata;
|
|
|
|
|
}
|
2004-09-09 12:04:39 +00:00
|
|
|
|
2013-05-15 04:13:02 +00:00
|
|
|
public function getSoftwareLink() {
|
2014-01-09 01:50:21 +00:00
|
|
|
return '[{{int:version-db-postgres-url}} PostgreSQL]';
|
2004-09-08 20:36:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2012-03-09 17:24:57 +00:00
|
|
|
/**
|
|
|
|
|
* Return current schema (executes SELECT current_schema())
|
|
|
|
|
* Needs transaction
|
|
|
|
|
*
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2013-12-27 00:10:41 +00:00
|
|
|
* @return string Default schema for the current session
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getCurrentSchema() {
|
2013-02-03 18:47:42 +00:00
|
|
|
$res = $this->query( "SELECT current_schema()", __METHOD__ );
|
2012-03-09 17:24:57 +00:00
|
|
|
$row = $this->fetchRow( $res );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-03-09 17:24:57 +00:00
|
|
|
return $row[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return list of schemas which are accessible without schema name
|
|
|
|
|
* This is list does not contain magic keywords like "$user"
|
|
|
|
|
* Needs transaction
|
|
|
|
|
*
|
2013-03-11 03:16:28 +00:00
|
|
|
* @see getSearchPath()
|
|
|
|
|
* @see setSearchPath()
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2014-07-24 17:42:45 +00:00
|
|
|
* @return array List of actual schemas for the current sesson
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getSchemas() {
|
2013-02-03 18:47:42 +00:00
|
|
|
$res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
|
2012-03-09 17:24:57 +00:00
|
|
|
$row = $this->fetchRow( $res );
|
2016-02-17 09:09:32 +00:00
|
|
|
$schemas = [];
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-03-09 17:24:57 +00:00
|
|
|
/* PHP pgsql support does not support array type, "{a,b}" string is returned */
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-02-03 18:47:42 +00:00
|
|
|
return $this->pg_array_parse( $row[0], $schemas );
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return search patch for schemas
|
|
|
|
|
* This is different from getSchemas() since it contain magic keywords
|
|
|
|
|
* (like "$user").
|
|
|
|
|
* Needs transaction
|
|
|
|
|
*
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2013-12-27 01:54:51 +00:00
|
|
|
* @return array How to search for table names schemas for the current user
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getSearchPath() {
|
2013-02-03 18:47:42 +00:00
|
|
|
$res = $this->query( "SHOW search_path", __METHOD__ );
|
2012-03-09 17:24:57 +00:00
|
|
|
$row = $this->fetchRow( $res );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-03-09 17:24:57 +00:00
|
|
|
/* PostgreSQL returns SHOW values as strings */
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-02-03 18:47:42 +00:00
|
|
|
return explode( ",", $row[0] );
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update search_path, values should already be sanitized
|
|
|
|
|
* Values may contain magic keywords like "$user"
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2012-03-09 17:24:57 +00:00
|
|
|
*
|
2014-04-19 11:55:27 +00:00
|
|
|
* @param array $search_path List of schemas to be searched by default
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
private function setSearchPath( $search_path ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
$this->query( "SET search_path = " . implode( ", ", $search_path ) );
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-21 20:06:05 +00:00
|
|
|
* Determine default schema for the current application
|
2012-03-09 17:24:57 +00:00
|
|
|
* Adjust this session schema search path if desired schema exists
|
|
|
|
|
* and is not alread there.
|
|
|
|
|
*
|
|
|
|
|
* We need to have name of the core schema stored to be able
|
|
|
|
|
* to query database metadata.
|
|
|
|
|
*
|
|
|
|
|
* This will be also called by the installer after the schema is created
|
|
|
|
|
*
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2014-01-06 18:24:44 +00:00
|
|
|
*
|
|
|
|
|
* @param string $desiredSchema
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 22:09:52 +00:00
|
|
|
public function determineCoreSchema( $desiredSchema ) {
|
2016-08-14 04:26:12 +00:00
|
|
|
$this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
|
2014-01-06 18:24:44 +00:00
|
|
|
if ( $this->schemaExists( $desiredSchema ) ) {
|
|
|
|
|
if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
|
|
|
|
|
$this->mCoreSchema = $desiredSchema;
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug(
|
|
|
|
|
"Schema \"" . $desiredSchema . "\" already in the search path\n" );
|
2012-03-09 17:24:57 +00:00
|
|
|
} else {
|
|
|
|
|
/**
|
2012-04-12 03:47:47 +00:00
|
|
|
* Prepend our schema (e.g. 'mediawiki') in front
|
2012-03-09 17:24:57 +00:00
|
|
|
* of the search path
|
2017-02-20 22:32:12 +00:00
|
|
|
* Fixes T17816
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
|
|
|
|
$search_path = $this->getSearchPath();
|
2012-04-12 03:47:47 +00:00
|
|
|
array_unshift( $search_path,
|
2014-01-06 18:24:44 +00:00
|
|
|
$this->addIdentifierQuotes( $desiredSchema ) );
|
2012-04-12 03:47:47 +00:00
|
|
|
$this->setSearchPath( $search_path );
|
2014-01-06 18:24:44 +00:00
|
|
|
$this->mCoreSchema = $desiredSchema;
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug(
|
|
|
|
|
"Schema \"" . $desiredSchema . "\" added to the search path\n" );
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->mCoreSchema = $this->getCurrentSchema();
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug(
|
|
|
|
|
"Schema \"" . $desiredSchema . "\" not found, using current \"" .
|
2013-11-20 10:13:51 +00:00
|
|
|
$this->mCoreSchema . "\"\n" );
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
/* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
|
2016-10-17 18:21:40 +00:00
|
|
|
$this->commit( __METHOD__, self::FLUSHING_INTERNAL );
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-21 20:06:05 +00:00
|
|
|
* Return schema name for core application tables
|
2012-03-09 17:24:57 +00:00
|
|
|
*
|
2012-04-13 18:55:14 +00:00
|
|
|
* @since 1.19
|
2014-07-24 17:42:45 +00:00
|
|
|
* @return string Core schema name
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getCoreSchema() {
|
2012-03-09 17:24:57 +00:00
|
|
|
return $this->mCoreSchema;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getServerVersion() {
|
2013-12-27 00:10:41 +00:00
|
|
|
if ( !isset( $this->numericVersion ) ) {
|
2016-10-17 18:21:40 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
$versionInfo = pg_version( $conn );
|
2010-07-25 22:09:34 +00:00
|
|
|
if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
|
|
|
|
|
// Old client, abort install
|
2013-12-27 00:10:41 +00:00
|
|
|
$this->numericVersion = '7.3 or earlier';
|
2010-07-25 22:09:34 +00:00
|
|
|
} elseif ( isset( $versionInfo['server'] ) ) {
|
|
|
|
|
// Normal client
|
2013-12-27 00:10:41 +00:00
|
|
|
$this->numericVersion = $versionInfo['server'];
|
2010-07-25 22:09:34 +00:00
|
|
|
} else {
|
2017-02-20 22:32:12 +00:00
|
|
|
// T18937: broken pgsql extension from PHP<5.3
|
2016-10-17 18:21:40 +00:00
|
|
|
$this->numericVersion = pg_parameter_status( $conn, 'server_version' );
|
2010-07-25 22:09:34 +00:00
|
|
|
}
|
2009-01-10 00:53:26 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-12-27 00:10:41 +00:00
|
|
|
return $this->numericVersion;
|
2004-09-08 20:36:41 +00:00
|
|
|
}
|
2004-09-30 18:56:10 +00:00
|
|
|
|
2006-06-27 17:06:36 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Query whether a given relation exists (in the given schema, or the
|
2007-03-19 02:40:32 +00:00
|
|
|
* default mw one if not given)
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param array|string $types
|
|
|
|
|
* @param bool|string $schema
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2006-06-27 17:06:36 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
private function relationExists( $table, $types, $schema = false ) {
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( !is_array( $types ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$types = [ $types ];
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2016-10-24 05:03:37 +00:00
|
|
|
if ( $schema === false ) {
|
2012-03-09 17:24:57 +00:00
|
|
|
$schema = $this->getCoreSchema();
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2017-04-27 14:19:59 +00:00
|
|
|
$table = $this->realTableName( $table, 'raw' );
|
2009-02-26 09:10:18 +00:00
|
|
|
$etable = $this->addQuotes( $table );
|
|
|
|
|
$eschema = $this->addQuotes( $schema );
|
2014-03-15 11:32:44 +00:00
|
|
|
$sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
|
2007-03-19 02:40:32 +00:00
|
|
|
. "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
|
2010-09-05 18:35:34 +00:00
|
|
|
. "AND c.relkind IN ('" . implode( "','", $types ) . "')";
|
2014-03-15 11:32:44 +00:00
|
|
|
$res = $this->query( $sql );
|
2007-07-05 19:42:18 +00:00
|
|
|
$count = $res ? $res->numRows() : 0;
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2010-09-27 14:24:13 +00:00
|
|
|
return (bool)$count;
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-05 18:35:34 +00:00
|
|
|
/**
|
2016-10-29 05:14:26 +00:00
|
|
|
* For backward compatibility, this function checks both tables and views.
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $fname
|
|
|
|
|
* @param bool|string $schema
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2007-03-19 02:40:32 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function tableExists( $table, $fname = __METHOD__, $schema = false ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return $this->relationExists( $table, [ 'r', 'v' ], $schema );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function sequenceExists( $sequence, $schema = false ) {
|
2009-02-26 09:10:18 +00:00
|
|
|
return $this->relationExists( $sequence, 'S', $schema );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function triggerExists( $table, $trigger ) {
|
2009-12-07 08:51:52 +00:00
|
|
|
$q = <<<SQL
|
2007-03-19 02:40:32 +00:00
|
|
|
SELECT 1 FROM pg_class, pg_namespace, pg_trigger
|
|
|
|
|
WHERE relnamespace=pg_namespace.oid AND relkind='r'
|
2017-02-25 21:38:46 +00:00
|
|
|
AND tgrelid=pg_class.oid
|
|
|
|
|
AND nspname=%s AND relname=%s AND tgname=%s
|
2009-12-07 08:51:52 +00:00
|
|
|
SQL;
|
2010-09-05 18:35:34 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
sprintf(
|
|
|
|
|
$q,
|
2012-03-09 17:24:57 +00:00
|
|
|
$this->addQuotes( $this->getCoreSchema() ),
|
2010-09-05 18:35:34 +00:00
|
|
|
$this->addQuotes( $table ),
|
|
|
|
|
$this->addQuotes( $trigger )
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
if ( !$res ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2007-07-05 19:42:18 +00:00
|
|
|
$rows = $res->numRows();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-04-19 01:35:15 +00:00
|
|
|
return $rows;
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function ruleExists( $table, $rule ) {
|
2010-09-05 18:35:34 +00:00
|
|
|
$exists = $this->selectField( 'pg_rules', 'rulename',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2010-09-05 18:35:34 +00:00
|
|
|
'rulename' => $rule,
|
|
|
|
|
'tablename' => $table,
|
2012-03-09 17:24:57 +00:00
|
|
|
'schemaname' => $this->getCoreSchema()
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
2010-09-05 18:35:34 +00:00
|
|
|
);
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-03-19 02:40:32 +00:00
|
|
|
return $exists === $rule;
|
|
|
|
|
}
|
2006-06-29 17:24:04 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function constraintExists( $table, $constraint ) {
|
2014-03-15 11:32:44 +00:00
|
|
|
$sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
|
2013-11-20 06:58:22 +00:00
|
|
|
"WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
|
2012-03-09 17:24:57 +00:00
|
|
|
$this->addQuotes( $this->getCoreSchema() ),
|
2010-09-05 18:35:34 +00:00
|
|
|
$this->addQuotes( $table ),
|
|
|
|
|
$this->addQuotes( $constraint )
|
|
|
|
|
);
|
2014-03-15 11:32:44 +00:00
|
|
|
$res = $this->query( $sql );
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( !$res ) {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2007-07-05 19:42:18 +00:00
|
|
|
$rows = $res->numRows();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2007-04-19 01:35:15 +00:00
|
|
|
return $rows;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-28 18:05:08 +00:00
|
|
|
/**
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
* Query whether a given schema exists. Returns true if it does, false if it doesn't.
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $schema
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2006-06-28 18:05:08 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function schemaExists( $schema ) {
|
2016-10-26 23:07:12 +00:00
|
|
|
if ( !strlen( $schema ) ) {
|
|
|
|
|
return false; // short-circuit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$exists = $this->selectField(
|
|
|
|
|
'"pg_catalog"."pg_namespace"', 1, [ 'nspname' => $schema ], __METHOD__ );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
return (bool)$exists;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if a given role (i.e. user) exists, false otherwise.
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $roleName
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function roleExists( $roleName ) {
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
$exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'rolname' => $roleName ], __METHOD__ );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
|
|
|
return (bool)$exists;
|
2006-06-27 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 00:08:44 +00:00
|
|
|
/**
|
|
|
|
|
* @var string $table
|
|
|
|
|
* @var string $field
|
|
|
|
|
* @return PostgresField|null
|
|
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function fieldInfo( $table, $field ) {
|
2010-09-05 18:35:34 +00:00
|
|
|
return PostgresField::fromText( $this, $table, $field );
|
2006-06-27 17:06:36 +00:00
|
|
|
}
|
2010-09-05 18:00:33 +00:00
|
|
|
|
2008-07-24 12:37:07 +00:00
|
|
|
/**
|
|
|
|
|
* pg_field_type() wrapper
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
|
|
|
|
|
* @param int $index Field number, starting from 0
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return string
|
2008-07-24 12:37:07 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function fieldType( $res, $index ) {
|
2008-07-24 12:37:07 +00:00
|
|
|
if ( $res instanceof ResultWrapper ) {
|
|
|
|
|
$res = $res->result;
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-07-24 12:37:07 +00:00
|
|
|
return pg_field_type( $res, $index );
|
|
|
|
|
}
|
2006-06-27 17:06:36 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function encodeBlob( $b ) {
|
2014-10-31 20:16:54 +00:00
|
|
|
return new PostgresBlob( pg_escape_bytea( $b ) );
|
2006-07-05 03:59:54 +00:00
|
|
|
}
|
2007-09-23 19:54:56 +00:00
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function decodeBlob( $b ) {
|
2014-10-31 20:16:54 +00:00
|
|
|
if ( $b instanceof PostgresBlob ) {
|
2007-09-23 19:54:56 +00:00
|
|
|
$b = $b->fetch();
|
2014-10-31 20:16:54 +00:00
|
|
|
} elseif ( $b instanceof Blob ) {
|
|
|
|
|
return $b->fetch();
|
2007-09-23 19:54:56 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2006-07-05 03:59:54 +00:00
|
|
|
return pg_unescape_bytea( $b );
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function strencode( $s ) {
|
2015-06-17 13:28:51 +00:00
|
|
|
// Should not be called by us
|
2016-10-17 18:21:40 +00:00
|
|
|
return pg_escape_string( $this->getBindingHandle(), $s );
|
2006-07-05 03:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function addQuotes( $s ) {
|
2016-10-17 18:21:40 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
2006-07-05 03:59:54 +00:00
|
|
|
if ( is_null( $s ) ) {
|
|
|
|
|
return 'NULL';
|
2010-09-05 18:35:34 +00:00
|
|
|
} elseif ( is_bool( $s ) ) {
|
2008-08-21 13:10:19 +00:00
|
|
|
return intval( $s );
|
2010-09-05 18:35:34 +00:00
|
|
|
} elseif ( $s instanceof Blob ) {
|
2014-10-31 20:16:54 +00:00
|
|
|
if ( $s instanceof PostgresBlob ) {
|
|
|
|
|
$s = $s->fetch();
|
|
|
|
|
} else {
|
2016-10-17 18:21:40 +00:00
|
|
|
$s = pg_escape_bytea( $conn, $s->fetch() );
|
2014-10-31 20:16:54 +00:00
|
|
|
}
|
|
|
|
|
return "'$s'";
|
2006-07-05 03:59:54 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-10-17 18:21:40 +00:00
|
|
|
return "'" . pg_escape_string( $conn, $s ) . "'";
|
2006-07-05 03:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
2008-02-10 15:38:48 +00:00
|
|
|
/**
|
|
|
|
|
* Postgres specific version of replaceVars.
|
|
|
|
|
* Calls the parent version in Database.php
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $ins SQL string, read from a stream (usually tables.sql)
|
2008-02-10 15:38:48 +00:00
|
|
|
* @return string SQL string
|
|
|
|
|
*/
|
|
|
|
|
protected function replaceVars( $ins ) {
|
|
|
|
|
$ins = parent::replaceVars( $ins );
|
|
|
|
|
|
2013-12-27 00:10:41 +00:00
|
|
|
if ( $this->numericVersion >= 8.3 ) {
|
2008-02-10 15:38:48 +00:00
|
|
|
// Thanks for not providing backwards-compatibility, 8.3
|
|
|
|
|
$ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 00:10:41 +00:00
|
|
|
if ( $this->numericVersion <= 8.1 ) { // Our minimum version
|
2008-02-10 15:38:48 +00:00
|
|
|
$ins = str_replace( 'USING gin', 'USING gist', $ins );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ins;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function makeSelectOptions( $options ) {
|
2007-03-11 15:49:27 +00:00
|
|
|
$preLimitTail = $postLimitTail = '';
|
2016-07-04 10:56:40 +00:00
|
|
|
$startOpts = $useIndex = $ignoreIndex = '';
|
2006-12-25 22:54:01 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$noKeyOptions = [];
|
2006-12-25 22:54:01 +00:00
|
|
|
foreach ( $options as $key => $option ) {
|
|
|
|
|
if ( is_numeric( $key ) ) {
|
|
|
|
|
$noKeyOptions[$option] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-26 09:43:20 +00:00
|
|
|
$preLimitTail .= $this->makeGroupByWithHaving( $options );
|
2011-04-29 22:27:34 +00:00
|
|
|
|
2013-01-26 09:43:20 +00:00
|
|
|
$preLimitTail .= $this->makeOrderBy( $options );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2013-06-20 19:20:37 +00:00
|
|
|
if ( isset( $options['FOR UPDATE'] ) ) {
|
PostgreSQL: Support table prefixes
PostgreSQL is usually not used with table name prefixes,
and some of the PostgreSQL-specific code took the
shortcut of not dealing with prefixes. However, the PHPUnit
tests do force a prefix to be used, exposing this limitation.
Make PostgreSQL code add the prefix to table names being used
in FOR UPDATE OF clause.
This fixes 13 of the 17 PostgreSQL unit tests errors against
git HEAD:
EditPageTest::testAutoMerge with data set #1 ('Elmo', 'one
EditPageTest::testAutoMerge with data set #2 ('Elmo', 'Intro
EditPageTest::testAutoMerge with data set #3 ('Adam', 'one
EditPageTest::testAutoMerge with data set #4 ('Adam', 'one
EditPageTest::testAutoMerge with data set #5 ('Adam', 'Intro
EditPageTest::testAutoMerge with data set #6 ('Berta', 'one
EditPageTest::testAutoMerge with data set #7 ('Berta', 'one
EditPageTest::testAutoMerge with data set #8 ('Berta', 'Intro
WikiPageTest::testDoEdit
WikiPageTest::testDoEditContent
WikiPageTest::testDoRollback
WikiPageTest::testDoRollbackFailureSameContent
WikiPageTest::testExists
The change also applies to 1.22.6 and fixes several unit tests
errors there.
Change-Id: I054690f49e250cadd2dc2a6d9e20dea879d896b6
2014-05-29 06:28:04 +00:00
|
|
|
$postLimitTail .= ' FOR UPDATE OF ' .
|
2017-02-01 04:01:54 +00:00
|
|
|
implode( ', ', array_map( [ $this, 'tableName' ], $options['FOR UPDATE'] ) );
|
2014-02-05 10:20:17 +00:00
|
|
|
} elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
|
2010-09-05 18:35:34 +00:00
|
|
|
$postLimitTail .= ' FOR UPDATE';
|
|
|
|
|
}
|
2013-06-20 19:20:37 +00:00
|
|
|
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
|
|
|
|
|
$startOpts .= 'DISTINCT';
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2016-07-04 10:56:40 +00:00
|
|
|
return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
|
2006-12-25 22:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getDBname() {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->mDBname;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getServer() {
|
2008-03-30 09:48:15 +00:00
|
|
|
return $this->mServer;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function buildConcat( $stringList ) {
|
2007-09-23 22:23:01 +00:00
|
|
|
return implode( ' || ', $stringList );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 07:23:36 +00:00
|
|
|
public function buildGroupConcatField(
|
2016-02-17 09:09:32 +00:00
|
|
|
$delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
|
2013-11-15 07:23:36 +00:00
|
|
|
) {
|
|
|
|
|
$fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
|
2013-11-15 07:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-25 20:24:42 +00:00
|
|
|
public function buildStringCast( $field ) {
|
|
|
|
|
return $field . '::text';
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-12 23:22:09 +00:00
|
|
|
public function streamStatementEnd( &$sql, &$newLine ) {
|
2012-01-11 20:19:55 +00:00
|
|
|
# Allow dollar quoting for function declarations
|
|
|
|
|
if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
|
|
|
|
|
if ( $this->delimiter ) {
|
|
|
|
|
$this->delimiter = false;
|
2013-11-20 06:58:22 +00:00
|
|
|
} else {
|
2012-01-11 20:19:55 +00:00
|
|
|
$this->delimiter = ';';
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2012-01-11 20:19:55 +00:00
|
|
|
return parent::streamStatementEnd( $sql, $newLine );
|
|
|
|
|
}
|
2013-02-01 19:42:36 +00:00
|
|
|
|
2017-03-30 21:56:22 +00:00
|
|
|
public function doLockTables( array $read, array $write, $method ) {
|
|
|
|
|
$tablesWrite = [];
|
|
|
|
|
foreach ( $write as $table ) {
|
|
|
|
|
$tablesWrite[] = $this->tableName( $table );
|
|
|
|
|
}
|
|
|
|
|
$tablesRead = [];
|
|
|
|
|
foreach ( $read as $table ) {
|
|
|
|
|
$tablesRead[] = $this->tableName( $table );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Acquire locks for the duration of the current transaction...
|
|
|
|
|
if ( $tablesWrite ) {
|
|
|
|
|
$this->query(
|
|
|
|
|
'LOCK TABLE ONLY ' . implode( ',', $tablesWrite ) . ' IN EXCLUSIVE MODE',
|
|
|
|
|
$method
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( $tablesRead ) {
|
|
|
|
|
$this->query(
|
|
|
|
|
'LOCK TABLE ONLY ' . implode( ',', $tablesRead ) . ' IN SHARE MODE',
|
|
|
|
|
$method
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-01 19:42:36 +00:00
|
|
|
public function lockIsFree( $lockName, $method ) {
|
2016-10-29 05:14:26 +00:00
|
|
|
// http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
|
2013-02-01 19:42:36 +00:00
|
|
|
$key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
|
|
|
|
|
$result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
|
|
|
|
|
WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
|
|
|
|
|
$row = $this->fetchObject( $result );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-02-01 19:42:36 +00:00
|
|
|
return ( $row->lockstatus === 't' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function lock( $lockName, $method, $timeout = 5 ) {
|
2016-10-29 05:14:26 +00:00
|
|
|
// http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
|
2013-02-01 19:42:36 +00:00
|
|
|
$key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
|
2016-09-08 07:12:56 +00:00
|
|
|
$loop = new WaitConditionLoop(
|
|
|
|
|
function () use ( $lockName, $key, $timeout, $method ) {
|
|
|
|
|
$res = $this->query( "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
|
|
|
|
|
$row = $this->fetchObject( $res );
|
|
|
|
|
if ( $row->lockstatus === 't' ) {
|
|
|
|
|
parent::lock( $lockName, $method, $timeout ); // record
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-01-30 17:17:17 +00:00
|
|
|
|
2016-09-08 07:12:56 +00:00
|
|
|
return WaitConditionLoop::CONDITION_CONTINUE;
|
|
|
|
|
},
|
|
|
|
|
$timeout
|
|
|
|
|
);
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-09-08 07:12:56 +00:00
|
|
|
return ( $loop->invoke() === $loop::CONDITION_REACHED );
|
2013-02-01 19:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unlock( $lockName, $method ) {
|
2016-10-29 05:14:26 +00:00
|
|
|
// http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
|
2013-02-01 19:42:36 +00:00
|
|
|
$key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
|
|
|
|
|
$result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
|
|
|
|
|
$row = $this->fetchObject( $result );
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2016-01-30 17:17:17 +00:00
|
|
|
if ( $row->lockstatus === 't' ) {
|
|
|
|
|
parent::unlock( $lockName, $method ); // record
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug( __METHOD__ . " failed to release lock\n" );
|
2016-01-30 17:17:17 +00:00
|
|
|
|
|
|
|
|
return false;
|
2013-02-01 19:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $lockName
|
|
|
|
|
* @return string Integer
|
|
|
|
|
*/
|
|
|
|
|
private function bigintFromLockName( $lockName ) {
|
2017-02-07 04:49:57 +00:00
|
|
|
return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
|
2013-02-01 19:42:36 +00:00
|
|
|
}
|
2016-09-19 17:37:57 +00:00
|
|
|
}
|
2017-02-07 04:49:57 +00:00
|
|
|
|
|
|
|
|
class_alias( DatabasePostgres::class, 'DatabasePostgres' );
|