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
|
|
|
/**
|
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
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
namespace Wikimedia\Rdbms;
|
|
|
|
|
|
2020-03-31 01:11:44 +00:00
|
|
|
use RuntimeException;
|
2021-12-08 12:08:11 +00:00
|
|
|
use Wikimedia\Rdbms\Platform\PostgresPlatform;
|
2016-10-01 07:07:19 +00:00
|
|
|
use Wikimedia\WaitConditionLoop;
|
2010-08-01 21:13:44 +00:00
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2022-08-08 16:13:59 +00:00
|
|
|
* Postgres database abstraction layer.
|
|
|
|
|
*
|
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 {
|
2021-03-27 08:39:15 +00:00
|
|
|
/** @var int */
|
2019-07-11 02:35:46 +00:00
|
|
|
private $port;
|
2013-12-27 00:10:41 +00:00
|
|
|
/** @var string */
|
2018-03-18 03:13:43 +00:00
|
|
|
private $tempSchema;
|
2021-03-10 01:38:23 +00:00
|
|
|
/** @var null|string[] Map of (reserved table name => alternate table name) */
|
|
|
|
|
private $keywordTableMap;
|
2019-07-11 02:35:46 +00:00
|
|
|
/** @var float|string */
|
|
|
|
|
private $numericVersion;
|
|
|
|
|
|
|
|
|
|
/** @var resource|null */
|
|
|
|
|
private $lastResultHandle;
|
2004-06-07 02:59:58 +00:00
|
|
|
|
2022-05-17 19:25:35 +00:00
|
|
|
/** @var PostgresPlatform */
|
2022-04-25 08:47:27 +00:00
|
|
|
protected $platform;
|
2021-12-08 12:08:11 +00:00
|
|
|
|
2016-11-10 05:35:57 +00:00
|
|
|
/**
|
|
|
|
|
* @see Database::__construct()
|
|
|
|
|
* @param array $params Additional parameters include:
|
2021-03-10 01:38:23 +00:00
|
|
|
* - port: A port to append to the hostname
|
2016-11-10 05:35:57 +00:00
|
|
|
* - keywordTableMap : Map of reserved table names to alternative table names to use
|
2021-04-26 20:25:28 +00:00
|
|
|
* This is is deprecated since 1.37. Reserved identifiers should be quoted where necessary,
|
2016-11-10 05:35:57 +00:00
|
|
|
*/
|
2016-09-19 17:37:57 +00:00
|
|
|
public function __construct( array $params ) {
|
2019-07-11 02:35:46 +00:00
|
|
|
$this->port = intval( $params['port'] ?? null );
|
2021-04-26 20:25:28 +00:00
|
|
|
|
|
|
|
|
if ( isset( $params['keywordTableMap'] ) ) {
|
|
|
|
|
wfDeprecatedMsg( 'Passing keywordTableMap parameter to ' .
|
|
|
|
|
'DatabasePostgres::__construct() is deprecated', '1.37'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->keywordTableMap = $params['keywordTableMap'];
|
|
|
|
|
}
|
2016-11-10 05:35:57 +00:00
|
|
|
|
2016-09-19 23:34:32 +00:00
|
|
|
parent::__construct( $params );
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->platform = new PostgresPlatform(
|
|
|
|
|
$this,
|
|
|
|
|
$params['queryLogger'],
|
2022-08-16 08:23:34 +00:00
|
|
|
$this->currentDomain,
|
|
|
|
|
$this->errorLogger
|
2022-05-17 19:25:35 +00:00
|
|
|
);
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 01:38:23 +00:00
|
|
|
protected function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
|
2004-07-10 03:09:26 +00:00
|
|
|
if ( !function_exists( 'pg_connect' ) ) {
|
2019-07-11 02:35:46 +00:00
|
|
|
throw $this->newExceptionAfterConnectError(
|
2013-11-20 10:13:51 +00:00
|
|
|
"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" .
|
2019-07-11 02:35:46 +00:00
|
|
|
"webserver and database)"
|
2013-11-20 10:13:51 +00:00
|
|
|
);
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-07 19:06:40 +00:00
|
|
|
$this->close( __METHOD__ );
|
2019-07-06 19:36:54 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$connectVars = [
|
2019-07-14 22:43:26 +00:00
|
|
|
// A database must be specified in order to connect to Postgres. If $dbName is not
|
|
|
|
|
// specified, then use the standard "postgres" database that should exist by default.
|
2021-03-10 01:38:23 +00:00
|
|
|
'dbname' => strlen( $db ) ? $db : '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
|
|
|
];
|
2019-07-11 02:35:46 +00:00
|
|
|
if ( strlen( $server ) ) {
|
2008-10-06 00:45:18 +00:00
|
|
|
$connectVars['host'] = $server;
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
2019-07-11 02:35:46 +00:00
|
|
|
if ( $this->port > 0 ) {
|
|
|
|
|
$connectVars['port'] = $this->port;
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
2022-05-26 04:25:59 +00:00
|
|
|
if ( $this->ssl ) {
|
2018-08-09 20:18:34 +00:00
|
|
|
$connectVars['sslmode'] = 'require';
|
2012-08-15 20:44:41 +00:00
|
|
|
}
|
2019-07-11 02:35:46 +00:00
|
|
|
$connectString = $this->makeConnectionString( $connectVars );
|
2013-11-15 15:13:19 +00:00
|
|
|
|
2019-07-06 19:36:54 +00:00
|
|
|
$this->installErrorHandler();
|
2013-11-15 15:13:19 +00:00
|
|
|
try {
|
2019-07-11 02:35:46 +00:00
|
|
|
$this->conn = pg_connect( $connectString, PGSQL_CONNECT_FORCE_NEW ) ?: null;
|
2020-03-31 01:11:44 +00:00
|
|
|
} catch ( RuntimeException $e ) {
|
2013-11-15 15:13:19 +00:00
|
|
|
$this->restoreErrorHandler();
|
2019-07-11 02:35:46 +00:00
|
|
|
throw $this->newExceptionAfterConnectError( $e->getMessage() );
|
2013-11-15 15:13:19 +00:00
|
|
|
}
|
2019-07-11 02:35:46 +00:00
|
|
|
$error = $this->restoreErrorHandler();
|
2006-06-28 18:05:08 +00:00
|
|
|
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( !$this->conn ) {
|
2019-07-11 02:35:46 +00:00
|
|
|
throw $this->newExceptionAfterConnectError( $error ?: $this->lastError() );
|
2006-06-28 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-13 12:46:03 +00:00
|
|
|
try {
|
2019-07-11 02:35:46 +00:00
|
|
|
// Since no transaction is active at this point, any SET commands should apply
|
|
|
|
|
// for the entire session (e.g. will not be reverted on transaction rollback).
|
2019-06-13 12:46:03 +00:00
|
|
|
// See https://www.postgresql.org/docs/8.3/sql-set.html
|
2019-07-11 02:35:46 +00:00
|
|
|
$variables = [
|
2019-06-13 12:46:03 +00:00
|
|
|
'client_encoding' => 'UTF8',
|
|
|
|
|
'datestyle' => 'ISO, YMD',
|
|
|
|
|
'timezone' => 'GMT',
|
|
|
|
|
'standard_conforming_strings' => 'on',
|
2019-07-11 02:35:46 +00:00
|
|
|
'bytea_output' => 'escape',
|
|
|
|
|
'client_min_messages' => 'ERROR'
|
2019-06-13 12:46:03 +00:00
|
|
|
];
|
|
|
|
|
foreach ( $variables as $var => $val ) {
|
|
|
|
|
$this->query(
|
2021-12-08 12:08:11 +00:00
|
|
|
'SET ' . $this->platform->addIdentifierQuotes( $var ) . ' = ' . $this->addQuotes( $val ),
|
2019-06-13 12:46:03 +00:00
|
|
|
__METHOD__,
|
2021-12-14 01:00:28 +00:00
|
|
|
self::QUERY_NO_RETRY | self::QUERY_CHANGE_TRX
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$this->determineCoreSchema( $schema );
|
2021-03-10 01:38:23 +00:00
|
|
|
$this->currentDomain = new DatabaseDomain( $db, $schema, $tablePrefix );
|
2022-08-01 19:44:47 +00:00
|
|
|
$this->platform->setCurrentDomain( $this->currentDomain );
|
2020-03-31 01:11:44 +00:00
|
|
|
} catch ( RuntimeException $e ) {
|
2019-07-11 02:35:46 +00:00
|
|
|
throw $this->newExceptionAfterConnectError( $e->getMessage() );
|
2006-08-18 16:24:48 +00:00
|
|
|
}
|
2018-08-14 23:44:41 +00:00
|
|
|
}
|
|
|
|
|
|
2017-05-10 01:27:28 +00:00
|
|
|
public function databasesAreIndependent() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-14 23:44:41 +00:00
|
|
|
public function doSelectDomain( DatabaseDomain $domain ) {
|
|
|
|
|
if ( $this->getDBname() !== $domain->getDatabase() ) {
|
|
|
|
|
// 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
|
|
|
|
|
$this->open(
|
2021-03-10 01:38:23 +00:00
|
|
|
$this->connectionParams[self::CONN_HOST],
|
|
|
|
|
$this->connectionParams[self::CONN_USER],
|
|
|
|
|
$this->connectionParams[self::CONN_PASSWORD],
|
2018-08-14 23:44:41 +00:00
|
|
|
$domain->getDatabase(),
|
|
|
|
|
$domain->getSchema(),
|
|
|
|
|
$domain->getTablePrefix()
|
|
|
|
|
);
|
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
|
|
|
} else {
|
2018-08-14 23:44:41 +00:00
|
|
|
$this->currentDomain = $domain;
|
2022-08-01 19:44:47 +00:00
|
|
|
$this->platform->setCurrentDomain( $this->currentDomain );
|
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
|
|
|
}
|
2018-08-14 23:44:41 +00:00
|
|
|
|
|
|
|
|
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() {
|
2018-02-13 06:58:57 +00:00
|
|
|
return $this->conn ? pg_close( $this->conn ) : true;
|
2004-06-07 02:59:58 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2020-02-19 08:34:41 +00:00
|
|
|
public function doSingleStatementQuery( string $sql ): QueryStatus {
|
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' );
|
2020-02-19 08:34:41 +00:00
|
|
|
// Clear any previously left over result
|
|
|
|
|
while ( $priorRes = pg_get_result( $conn ) ) {
|
|
|
|
|
pg_free_result( $priorRes );
|
2015-10-14 09:42:00 +00:00
|
|
|
}
|
2020-02-19 08:34:41 +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" );
|
|
|
|
|
}
|
2020-02-19 08:34:41 +00:00
|
|
|
|
|
|
|
|
// Newer PHP versions use PgSql\Result instead of resource variables
|
|
|
|
|
// https://www.php.net/manual/en/function.pg-get-result.php
|
|
|
|
|
$pgRes = pg_get_result( $conn );
|
|
|
|
|
$this->lastResultHandle = $pgRes;
|
|
|
|
|
$res = pg_result_error( $pgRes ) ? false : $pgRes;
|
|
|
|
|
|
|
|
|
|
return new QueryStatus(
|
|
|
|
|
is_bool( $res ) ? $res : new PostgresResultWrapper( $this, $conn, $res ),
|
|
|
|
|
$this->affectedRows(),
|
|
|
|
|
$this->lastError(),
|
|
|
|
|
$this->lastErrno()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function doMultiStatementQuery( array $sqls ): array {
|
|
|
|
|
$qsByStatementId = [];
|
|
|
|
|
|
|
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
// Clear any previously left over result
|
|
|
|
|
while ( $pgResultSet = pg_get_result( $conn ) ) {
|
|
|
|
|
pg_free_result( $pgResultSet );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$combinedSql = mb_convert_encoding( implode( ";\n", $sqls ), 'UTF-8' );
|
|
|
|
|
pg_send_query( $conn, $combinedSql );
|
|
|
|
|
|
|
|
|
|
reset( $sqls );
|
|
|
|
|
while ( ( $pgResultSet = pg_get_result( $conn ) ) !== false ) {
|
|
|
|
|
$this->lastResultHandle = $pgResultSet;
|
|
|
|
|
|
|
|
|
|
$statementId = key( $sqls );
|
|
|
|
|
if ( $statementId !== null ) {
|
|
|
|
|
if ( pg_result_error( $pgResultSet ) ) {
|
|
|
|
|
$res = false;
|
|
|
|
|
} else {
|
|
|
|
|
$res = new PostgresResultWrapper( $this, $conn, $pgResultSet );
|
|
|
|
|
}
|
|
|
|
|
$qsByStatementId[$statementId] = new QueryStatus(
|
|
|
|
|
$res,
|
|
|
|
|
pg_affected_rows( $pgResultSet ),
|
|
|
|
|
(string)pg_result_error( $pgResultSet ),
|
|
|
|
|
pg_result_error_field( $pgResultSet, PGSQL_DIAG_SQLSTATE )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
next( $sqls );
|
|
|
|
|
}
|
|
|
|
|
// Fill in status for statements aborted due to prior statement failure
|
|
|
|
|
while ( ( $statementId = key( $sqls ) ) !== null ) {
|
|
|
|
|
$qsByStatementId[$statementId] = new QueryStatus( false, 0, 'Query aborted', 0 );
|
|
|
|
|
next( $sqls );
|
2012-03-30 03:06:43 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2020-02-19 08:34:41 +00:00
|
|
|
return $qsByStatementId;
|
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 ) {
|
2020-06-01 05:00:39 +00:00
|
|
|
$this->queryLogger->debug( sprintf( "PgSQL ERROR(%d): %s",
|
2018-02-16 19:16:10 +00:00
|
|
|
$d, pg_result_error_field( $this->lastResultHandle, $d ) ) );
|
2012-03-30 03:06:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function insertId() {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT lastval()",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2022-01-20 23:06:29 +00:00
|
|
|
$row = $res->fetchRow();
|
2020-03-20 13:16:46 +00:00
|
|
|
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnProbablyReal Return type is undefined for no lastval
|
2020-01-09 23:48:34 +00:00
|
|
|
return $row[0] === null ? null : (int)$row[0];
|
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 lastError() {
|
2018-02-13 06:58:57 +00:00
|
|
|
if ( $this->conn ) {
|
2018-02-16 19:16:10 +00:00
|
|
|
if ( $this->lastResultHandle ) {
|
|
|
|
|
return pg_result_error( $this->lastResultHandle );
|
2012-03-30 03:06:43 +00:00
|
|
|
} 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() {
|
2018-02-16 19:16:10 +00:00
|
|
|
if ( $this->lastResultHandle ) {
|
2022-03-09 19:28:00 +00:00
|
|
|
$lastErrno = pg_result_error_field( $this->lastResultHandle, PGSQL_DIAG_SQLSTATE );
|
|
|
|
|
if ( $lastErrno !== false ) {
|
2022-05-11 00:39:59 +00:00
|
|
|
return $lastErrno;
|
2022-03-09 19:28:00 +00:00
|
|
|
}
|
2012-03-30 03:06:43 +00:00
|
|
|
}
|
2022-05-11 00:39:59 +00:00
|
|
|
|
|
|
|
|
return '00000';
|
2006-07-21 19:34:45 +00:00
|
|
|
}
|
2004-07-24 07:24:04 +00:00
|
|
|
|
2018-01-28 14:10:39 +00:00
|
|
|
protected function fetchAffectedRowCount() {
|
2018-03-02 04:30:07 +00:00
|
|
|
if ( !$this->lastResultHandle ) {
|
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
|
|
|
|
2018-02-16 19:16:10 +00:00
|
|
|
return pg_affected_rows( $this->lastResultHandle );
|
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
|
2018-02-15 03:46:04 +00:00
|
|
|
* @param string $var
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string $conds
|
|
|
|
|
* @param string $fname
|
|
|
|
|
* @param array $options
|
2018-03-12 16:15:14 +00:00
|
|
|
* @param array $join_conds
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return int
|
2007-04-07 17:46:17 +00:00
|
|
|
*/
|
2018-02-15 03:46:04 +00:00
|
|
|
public function estimateRowCount( $table, $var = '*', $conds = '',
|
2018-03-12 16:15:14 +00:00
|
|
|
$fname = __METHOD__, $options = [], $join_conds = []
|
2013-11-20 10:13:51 +00:00
|
|
|
) {
|
2022-07-29 14:04:44 +00:00
|
|
|
$conds = $this->platform->normalizeConditions( $conds, $fname );
|
|
|
|
|
$column = $this->platform->extractSingleFieldFromList( $var );
|
2018-02-15 03:46:04 +00:00
|
|
|
if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
|
|
|
|
|
$conds[] = "$column IS NOT NULL";
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-07 17:46:17 +00:00
|
|
|
$options['EXPLAIN'] = true;
|
2018-02-15 03:46:04 +00:00
|
|
|
$res = $this->select( $table, $var, $conds, $fname, $options, $join_conds );
|
2007-04-07 17:46:17 +00:00
|
|
|
$rows = -1;
|
|
|
|
|
if ( $res ) {
|
2022-01-20 23:06:29 +00:00
|
|
|
$row = $res->fetchRow();
|
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__ ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT indexname FROM pg_indexes WHERE tablename='$table'",
|
|
|
|
|
$fname,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
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 ) {
|
2018-03-18 03:13:43 +00:00
|
|
|
$schemas = $this->getCoreSchemas();
|
|
|
|
|
} else {
|
|
|
|
|
$schemas = [ $schema ];
|
2013-04-20 20:28:52 +00:00
|
|
|
}
|
2018-03-18 03:13:43 +00:00
|
|
|
|
|
|
|
|
$eindex = $this->addQuotes( $index );
|
|
|
|
|
|
2020-03-20 13:16:46 +00:00
|
|
|
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
|
2018-03-18 03:13:43 +00:00
|
|
|
foreach ( $schemas as $schema ) {
|
|
|
|
|
$eschema = $this->addQuotes( $schema );
|
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
(SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
|
|
|
|
|
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=$eindex AND ns.nspname=$eschema) 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=$eindex AND n.nspname=$eschema
|
|
|
|
|
AND attrelid = ct.oid
|
|
|
|
|
AND i.indkey[s.g] = attnum
|
|
|
|
|
AND i.indclass[s.g] = opcls.oid
|
|
|
|
|
AND pg_am.oid = opcls.opcmethod
|
2012-04-13 18:55:14 +00:00
|
|
|
__INDEXATTR__;
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query( $sql, __METHOD__, $flags );
|
2018-03-18 03:13:43 +00:00
|
|
|
$a = [];
|
|
|
|
|
if ( $res ) {
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$a[] = [
|
|
|
|
|
$row->attname,
|
|
|
|
|
$row->opcname,
|
|
|
|
|
$row->amname,
|
|
|
|
|
$row->option ];
|
|
|
|
|
}
|
|
|
|
|
return $a;
|
2012-04-13 18:55:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-03-18 03:13:43 +00:00
|
|
|
return null;
|
2012-04-13 18:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-29 05:14:26 +00:00
|
|
|
public function indexUnique( $table, $index, $fname = __METHOD__ ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
|
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
|
|
|
")'";
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query( $sql, $fname, $flags );
|
2010-09-05 18:35:34 +00:00
|
|
|
if ( !$res ) {
|
2022-03-09 19:28:00 +00:00
|
|
|
return false;
|
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
|
|
|
}
|
|
|
|
|
|
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.
|
2018-08-14 07:56:35 +00:00
|
|
|
* @todo FIXME: Implement this a little better (separate 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
|
2010-09-05 18:35:34 +00:00
|
|
|
*/
|
2020-03-02 22:20:09 +00:00
|
|
|
protected function doInsertSelectNative(
|
|
|
|
|
$destTable,
|
|
|
|
|
$srcTable,
|
|
|
|
|
array $varMap,
|
|
|
|
|
$conds,
|
|
|
|
|
$fname,
|
|
|
|
|
array $insertOptions,
|
|
|
|
|
array $selectOptions,
|
|
|
|
|
$selectJoinConds
|
2016-10-29 05:14:26 +00:00
|
|
|
) {
|
2012-06-18 18:39:08 +00:00
|
|
|
if ( in_array( 'IGNORE', $insertOptions ) ) {
|
2022-07-29 14:04:44 +00:00
|
|
|
// Use "ON CONFLICT DO" if we have it for IGNORE
|
|
|
|
|
$destTable = $this->tableName( $destTable );
|
|
|
|
|
|
|
|
|
|
$selectSql = $this->selectSQLText(
|
|
|
|
|
$srcTable,
|
|
|
|
|
array_values( $varMap ),
|
|
|
|
|
$conds,
|
|
|
|
|
$fname,
|
|
|
|
|
$selectOptions,
|
|
|
|
|
$selectJoinConds
|
|
|
|
|
);
|
2018-03-18 00:29:31 +00:00
|
|
|
|
2022-07-29 14:04:44 +00:00
|
|
|
$sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ') ' .
|
|
|
|
|
$selectSql . ' ON CONFLICT DO NOTHING';
|
2018-03-18 00:29:31 +00:00
|
|
|
|
2022-07-29 14:04:44 +00:00
|
|
|
$this->query( $sql, $fname, self::QUERY_CHANGE_ROWS );
|
2018-10-26 20:17:34 +00:00
|
|
|
} else {
|
2020-03-02 22:20:09 +00:00
|
|
|
parent::doInsertSelectNative( $destTable, $srcTable, $varMap, $conds, $fname,
|
2018-10-26 20:17:34 +00:00
|
|
|
$insertOptions, $selectOptions, $selectJoinConds );
|
2012-06-18 18:39:08 +00:00
|
|
|
}
|
2009-10-27 05:33:31 +00:00
|
|
|
}
|
2010-09-05 18:00:33 +00:00
|
|
|
|
2016-10-25 18:56:41 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string Value of $name or remapped name if $name is a reserved keyword
|
2021-04-26 20:25:28 +00:00
|
|
|
* @deprecated since 1.37. Reserved identifiers should be quoted where necessary
|
2016-10-25 18:56:41 +00:00
|
|
|
*/
|
|
|
|
|
public function remappedTableName( $name ) {
|
2021-04-26 20:25:28 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
|
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
return $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 ) {
|
2017-08-31 00:27:51 +00:00
|
|
|
return new NextSequenceValue;
|
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 ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT currval('" . str_replace( "'", "''", $seqName ) . "')",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2022-01-20 23:06:29 +00:00
|
|
|
$row = $res->fetchRow();
|
2007-12-18 15:44:18 +00:00
|
|
|
$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 ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
|
|
|
|
|
$encTable = $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
|
2020-03-20 13:16:46 +00:00
|
|
|
WHERE relname='$encTable' AND a.attrelid=c.oid AND
|
2004-09-11 14:08:52 +00:00
|
|
|
a.atttypid=t.oid and a.attname='$field'";
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query( $sql, __METHOD__, $flags );
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2010-09-05 18:35:34 +00:00
|
|
|
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 wasDeadlock() {
|
2018-03-18 00:29:31 +00:00
|
|
|
// https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
|
2018-03-20 01:08:37 +00:00
|
|
|
return $this->lastErrno() === '40P01';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function wasLockTimeout() {
|
2018-03-18 00:29:31 +00:00
|
|
|
// https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
|
2018-03-20 01:08:37 +00:00
|
|
|
return $this->lastErrno() === '55P03';
|
2004-07-18 08:48:43 +00:00
|
|
|
}
|
2004-08-10 11:12:18 +00:00
|
|
|
|
2021-11-10 23:01:08 +00:00
|
|
|
protected function isConnectionError( $errno ) {
|
2018-03-18 00:29:31 +00:00
|
|
|
// https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
|
2018-03-20 11:16:41 +00:00
|
|
|
static $codes = [ '08000', '08003', '08006', '08001', '08004', '57P01', '57P03', '53300' ];
|
|
|
|
|
|
|
|
|
|
return in_array( $errno, $codes, true );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 00:08:39 +00:00
|
|
|
protected function isQueryTimeoutError( $errno ) {
|
|
|
|
|
// https://www.postgresql.org/docs/9.2/static/errcodes-appendix.html
|
|
|
|
|
return ( $errno === '57014' );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 00:55:52 +00:00
|
|
|
protected function isKnownStatementRollbackError( $errno ) {
|
2018-03-23 09:57:21 +00:00
|
|
|
return false; // transaction has to be rolled-back from error state
|
|
|
|
|
}
|
|
|
|
|
|
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__
|
|
|
|
|
) {
|
2021-12-08 12:08:11 +00:00
|
|
|
$newNameE = $this->platform->addIdentifierQuotes( $newName );
|
|
|
|
|
$oldNameE = $this->platform->addIdentifierQuotes( $oldName );
|
2018-03-18 17:23:58 +00:00
|
|
|
|
2018-04-09 15:57:27 +00:00
|
|
|
$temporary = $temporary ? 'TEMPORARY' : '';
|
|
|
|
|
|
2019-03-20 04:04:35 +00:00
|
|
|
$ret = $this->query(
|
|
|
|
|
"CREATE $temporary TABLE $newNameE " .
|
|
|
|
|
"(LIKE $oldNameE INCLUDING DEFAULTS INCLUDING INDEXES)",
|
|
|
|
|
$fname,
|
2020-03-20 13:16:46 +00:00
|
|
|
self::QUERY_PSEUDO_PERMANENT | self::QUERY_CHANGE_SCHEMA
|
2019-03-20 04:04:35 +00:00
|
|
|
);
|
2018-03-18 17:23:58 +00:00
|
|
|
if ( !$ret ) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
'SELECT attname FROM pg_class c'
|
2018-03-18 17:23:58 +00:00
|
|
|
. ' JOIN pg_namespace n ON (n.oid = c.relnamespace)'
|
|
|
|
|
. ' JOIN pg_attribute a ON (a.attrelid = c.oid)'
|
|
|
|
|
. ' JOIN pg_attrdef d ON (c.oid=d.adrelid and a.attnum=d.adnum)'
|
|
|
|
|
. ' WHERE relkind = \'r\''
|
|
|
|
|
. ' AND nspname = ' . $this->addQuotes( $this->getCoreSchema() )
|
|
|
|
|
. ' AND relname = ' . $this->addQuotes( $oldName )
|
2019-01-17 23:41:51 +00:00
|
|
|
. ' AND pg_get_expr(adbin, adrelid) LIKE \'nextval(%\'',
|
2020-03-20 13:16:46 +00:00
|
|
|
$fname,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
2018-03-18 17:23:58 +00:00
|
|
|
);
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2018-03-18 17:23:58 +00:00
|
|
|
if ( $row ) {
|
|
|
|
|
$field = $row->attname;
|
|
|
|
|
$newSeq = "{$newName}_{$field}_seq";
|
2021-12-08 12:08:11 +00:00
|
|
|
$fieldE = $this->platform->addIdentifierQuotes( $field );
|
|
|
|
|
$newSeqE = $this->platform->addIdentifierQuotes( $newSeq );
|
2018-03-18 17:23:58 +00:00
|
|
|
$newSeqQ = $this->addQuotes( $newSeq );
|
2019-03-20 04:04:35 +00:00
|
|
|
$this->query(
|
|
|
|
|
"CREATE $temporary SEQUENCE $newSeqE OWNED BY $newNameE.$fieldE",
|
2020-03-20 15:36:49 +00:00
|
|
|
$fname,
|
2020-03-20 13:16:46 +00:00
|
|
|
self::QUERY_CHANGE_SCHEMA
|
2019-03-20 04:04:35 +00:00
|
|
|
);
|
2018-03-18 17:23:58 +00:00
|
|
|
$this->query(
|
|
|
|
|
"ALTER TABLE $newNameE ALTER COLUMN $fieldE SET DEFAULT nextval({$newSeqQ}::regclass)",
|
2020-03-20 15:36:49 +00:00
|
|
|
$fname,
|
2020-03-20 13:16:46 +00:00
|
|
|
self::QUERY_CHANGE_SCHEMA
|
2018-03-18 17:23:58 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 17:32:06 +00:00
|
|
|
protected function doTruncate( array $tables, $fname ) {
|
|
|
|
|
$encTables = $this->tableNamesN( ...$tables );
|
|
|
|
|
$sql = "TRUNCATE TABLE " . implode( ',', $encTables ) . " RESTART IDENTITY";
|
2020-03-20 13:16:46 +00:00
|
|
|
$this->query( $sql, $fname, self::QUERY_CHANGE_SCHEMA );
|
2009-11-06 10:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
2018-09-20 05:38:05 +00:00
|
|
|
/**
|
2019-05-03 19:19:18 +00:00
|
|
|
* @param string $prefix Only show tables with this prefix, e.g. mw_
|
|
|
|
|
* @param string $fname Calling function name
|
|
|
|
|
* @return string[]
|
2018-09-20 05:38:05 +00:00
|
|
|
* @suppress SecurityCheck-SQLInjection array_map not recognized T204911
|
|
|
|
|
*/
|
2019-05-03 19:19:18 +00:00
|
|
|
public function listTables( $prefix = '', $fname = __METHOD__ ) {
|
2018-03-18 03:13:43 +00:00
|
|
|
$eschemas = implode( ',', array_map( [ $this, 'addQuotes' ], $this->getCoreSchemas() ) );
|
2016-09-19 17:37:57 +00:00
|
|
|
$result = $this->query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SELECT DISTINCT tablename FROM pg_tables WHERE schemaname IN ($eschemas)",
|
|
|
|
|
$fname,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
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 );
|
2019-05-03 19:19:18 +00:00
|
|
|
if ( $prefix == '' || strpos( $table, $prefix ) === 0 ) {
|
2011-10-24 19:28:31 +00:00
|
|
|
$endArray[] = $table;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $endArray;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2019-04-12 04:41:14 +00:00
|
|
|
* to https://www.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}
|
2019-11-23 22:28:57 +00:00
|
|
|
* @param string[] &$output
|
2022-06-18 08:32:35 +00:00
|
|
|
* @param int|false $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 ) {
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $limit === false ) {
|
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
|
|
|
}
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $text == '{}' ) {
|
2012-03-09 19:05:29 +00:00
|
|
|
return $output;
|
|
|
|
|
}
|
2012-03-09 17:24:57 +00:00
|
|
|
do {
|
2018-06-30 09:43:00 +00:00
|
|
|
if ( $text[$offset] != '{' ) {
|
2012-03-09 17:24:57 +00:00
|
|
|
preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
|
|
|
|
|
$text, $match, 0, $offset );
|
|
|
|
|
$offset += strlen( $match[0] );
|
2018-06-30 09:43:00 +00:00
|
|
|
$output[] = ( $match[1][0] != '"'
|
2013-11-20 06:58:22 +00:00
|
|
|
? $match[1]
|
|
|
|
|
: stripcslashes( substr( $match[1], 1, -1 ) ) );
|
2018-06-30 09:43:00 +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;
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT current_schema()",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2022-01-20 23:06:29 +00:00
|
|
|
$row = $res->fetchRow();
|
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
|
2021-12-26 11:24:49 +00:00
|
|
|
* @return array List of actual schemas for the current session
|
2012-03-09 17:24:57 +00:00
|
|
|
*/
|
2016-10-29 05:14:26 +00:00
|
|
|
public function getSchemas() {
|
2019-06-13 12:46:03 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT current_schemas(false)",
|
|
|
|
|
__METHOD__,
|
2020-03-20 13:16:46 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
2022-01-20 23:06:29 +00:00
|
|
|
$row = $res->fetchRow();
|
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() {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SHOW search_path",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2022-01-20 23:06:29 +00:00
|
|
|
$row = $res->fetchRow();
|
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
|
|
|
*
|
2019-07-11 02:35:46 +00:00
|
|
|
* @param string[] $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 ) {
|
2019-06-13 12:46:03 +00:00
|
|
|
$this->query(
|
|
|
|
|
"SET search_path = " . implode( ", ", $search_path ),
|
|
|
|
|
__METHOD__,
|
2021-12-14 01:00:28 +00:00
|
|
|
self::QUERY_CHANGE_TRX
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
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
|
2021-12-26 11:24:49 +00:00
|
|
|
* and is not already there.
|
2012-03-09 17:24:57 +00:00
|
|
|
*
|
|
|
|
|
* 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 ) {
|
2019-06-13 15:25:40 +00:00
|
|
|
if ( $this->trxLevel() ) {
|
2019-06-13 12:46:03 +00:00
|
|
|
// We do not want the schema selection to change on ROLLBACK or INSERT SELECT.
|
|
|
|
|
// See https://www.postgresql.org/docs/8.3/sql-set.html
|
|
|
|
|
throw new DBUnexpectedError(
|
|
|
|
|
$this,
|
2019-07-06 19:36:54 +00:00
|
|
|
__METHOD__ . ": a transaction is currently active"
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 18:24:44 +00:00
|
|
|
if ( $this->schemaExists( $desiredSchema ) ) {
|
|
|
|
|
if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->platform->setCoreSchema( $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 {
|
2019-07-11 02:35:46 +00:00
|
|
|
// Prepend the desired schema to the search path (T17816)
|
2012-03-09 17:24:57 +00:00
|
|
|
$search_path = $this->getSearchPath();
|
2021-12-08 12:08:11 +00:00
|
|
|
array_unshift( $search_path, $this->platform->addIdentifierQuotes( $desiredSchema ) );
|
2012-04-12 03:47:47 +00:00
|
|
|
$this->setSearchPath( $search_path );
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->platform->setCoreSchema( $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 {
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->platform->setCoreSchema( $this->getCurrentSchema() );
|
2016-09-19 17:37:57 +00:00
|
|
|
$this->queryLogger->debug(
|
|
|
|
|
"Schema \"" . $desiredSchema . "\" not found, using current \"" .
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->getCoreSchema() . "\"\n" );
|
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() {
|
2022-05-17 19:25:35 +00:00
|
|
|
return $this->platform->getCoreSchema();
|
2012-03-09 17:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-18 03:13:43 +00:00
|
|
|
/**
|
|
|
|
|
* Return schema names for temporary tables and core application tables
|
|
|
|
|
*
|
|
|
|
|
* @since 1.31
|
|
|
|
|
* @return string[] schema names
|
|
|
|
|
*/
|
|
|
|
|
public function getCoreSchemas() {
|
|
|
|
|
if ( $this->tempSchema ) {
|
|
|
|
|
return [ $this->tempSchema, $this->getCoreSchema() ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$res = $this->query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SELECT nspname FROM pg_catalog.pg_namespace n WHERE n.oid = pg_my_temp_schema()",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
2018-03-18 03:13:43 +00:00
|
|
|
);
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2018-03-18 03:13:43 +00:00
|
|
|
if ( $row ) {
|
|
|
|
|
$this->tempSchema = $row->nspname;
|
|
|
|
|
return [ $this->tempSchema, $this->getCoreSchema() ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [ $this->getCoreSchema() ];
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
2018-03-18 03:13:43 +00:00
|
|
|
$schemas = $this->getCoreSchemas();
|
|
|
|
|
} else {
|
|
|
|
|
$schemas = [ $schema ];
|
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 );
|
2018-03-18 03:13:43 +00:00
|
|
|
foreach ( $schemas as $schema ) {
|
|
|
|
|
$eschema = $this->addQuotes( $schema );
|
|
|
|
|
$sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
|
|
|
|
|
. "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
|
|
|
|
|
. "AND c.relkind IN ('" . implode( "','", $types ) . "')";
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
$sql,
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2018-03-18 03:13:43 +00:00
|
|
|
if ( $res && $res->numRows() ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2018-03-18 03:13:43 +00:00
|
|
|
return false;
|
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;
|
2018-03-18 03:13:43 +00:00
|
|
|
foreach ( $this->getCoreSchemas() as $schema ) {
|
|
|
|
|
$res = $this->query(
|
|
|
|
|
sprintf(
|
|
|
|
|
$q,
|
|
|
|
|
$this->addQuotes( $schema ),
|
|
|
|
|
$this->addQuotes( $table ),
|
|
|
|
|
$this->addQuotes( $trigger )
|
2020-03-20 13:16:46 +00:00
|
|
|
),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
2018-03-18 03:13:43 +00:00
|
|
|
);
|
|
|
|
|
if ( $res && $res->numRows() ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2018-03-18 03:13:43 +00:00
|
|
|
return false;
|
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,
|
2018-03-18 03:13:43 +00:00
|
|
|
'schemaname' => $this->getCoreSchemas()
|
2020-06-07 19:06:40 +00:00
|
|
|
],
|
|
|
|
|
__METHOD__
|
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 ) {
|
2018-03-18 03:13:43 +00:00
|
|
|
foreach ( $this->getCoreSchemas() as $schema ) {
|
|
|
|
|
$sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
|
|
|
|
|
"WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
|
|
|
|
|
$this->addQuotes( $schema ),
|
|
|
|
|
$this->addQuotes( $table ),
|
|
|
|
|
$this->addQuotes( $constraint )
|
|
|
|
|
);
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
$sql,
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2018-03-18 03:13:43 +00:00
|
|
|
if ( $res && $res->numRows() ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-09-05 18:35:34 +00:00
|
|
|
}
|
2018-03-18 03:13:43 +00:00
|
|
|
return false;
|
2007-04-19 01:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-13 12:46:03 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT 1 FROM pg_catalog.pg_namespace " .
|
|
|
|
|
"WHERE nspname = " . $this->addQuotes( $schema ) . " LIMIT 1",
|
|
|
|
|
__METHOD__,
|
2020-03-20 13:16:46 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2022-01-20 23:06:29 +00:00
|
|
|
return ( $res->numRows() > 0 );
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SELECT 1 FROM pg_catalog.pg_roles " .
|
|
|
|
|
"WHERE rolname = " . $this->addQuotes( $roleName ) . " LIMIT 1",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2022-01-20 23:06:29 +00:00
|
|
|
return ( $res->numRows() > 0 );
|
2006-06-27 17:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-01 00:08:44 +00:00
|
|
|
/**
|
2017-09-09 20:47:04 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $field
|
2016-07-01 00:08:44 +00:00
|
|
|
* @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
|
|
|
|
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
|
2017-10-01 00:46:51 +00:00
|
|
|
return pg_escape_string( $this->getBindingHandle(), (string)$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();
|
|
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $s === null ) {
|
2006-07-05 03:59:54 +00:00
|
|
|
return 'NULL';
|
2010-09-05 18:35:34 +00:00
|
|
|
} elseif ( is_bool( $s ) ) {
|
2019-11-15 05:35:33 +00:00
|
|
|
return (string)intval( $s );
|
|
|
|
|
} elseif ( is_int( $s ) ) {
|
|
|
|
|
return (string)$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'";
|
2017-08-31 00:27:51 +00:00
|
|
|
} elseif ( $s instanceof NextSequenceValue ) {
|
|
|
|
|
return 'DEFAULT';
|
2006-07-05 03:59:54 +00:00
|
|
|
}
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2017-10-01 00:46:51 +00:00
|
|
|
return "'" . pg_escape_string( $conn, (string)$s ) . "'";
|
2006-07-05 03:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2021-06-09 04:14:26 +00:00
|
|
|
public function doLockIsFree( string $lockName, string $method ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
2022-08-01 20:29:15 +00:00
|
|
|
$this->platform->lockIsFreeSQLText( $lockName ),
|
2020-03-20 13:16:46 +00:00
|
|
|
$method,
|
2021-12-14 01:19:35 +00:00
|
|
|
self::QUERY_CHANGE_LOCKS
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2021-06-09 04:14:26 +00:00
|
|
|
return ( $row->unlocked === 't' );
|
2013-02-01 19:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 04:14:26 +00:00
|
|
|
public function doLock( string $lockName, string $method, int $timeout ) {
|
2022-08-01 20:29:15 +00:00
|
|
|
$sql = $this->platform->lockSQLText( $lockName, $timeout );
|
2021-06-09 04:14:26 +00:00
|
|
|
|
|
|
|
|
$acquired = null;
|
2016-09-08 07:12:56 +00:00
|
|
|
$loop = new WaitConditionLoop(
|
2022-08-01 20:29:15 +00:00
|
|
|
function () use ( $lockName, $sql, $timeout, $method, &$acquired ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
2022-08-01 20:29:15 +00:00
|
|
|
$sql,
|
2020-03-20 13:16:46 +00:00
|
|
|
$method,
|
2021-12-14 01:19:35 +00:00
|
|
|
self::QUERY_CHANGE_LOCKS
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2021-06-09 04:14:26 +00:00
|
|
|
|
|
|
|
|
if ( $row->acquired !== null ) {
|
|
|
|
|
$acquired = (float)$row->acquired;
|
|
|
|
|
|
|
|
|
|
return WaitConditionLoop::CONDITION_REACHED;
|
2016-09-08 07:12:56 +00:00
|
|
|
}
|
2016-01-30 17:17:17 +00:00
|
|
|
|
2016-09-08 07:12:56 +00:00
|
|
|
return WaitConditionLoop::CONDITION_CONTINUE;
|
|
|
|
|
},
|
|
|
|
|
$timeout
|
|
|
|
|
);
|
2021-06-09 04:14:26 +00:00
|
|
|
$loop->invoke();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2021-06-09 04:14:26 +00:00
|
|
|
return $acquired;
|
2013-02-01 19:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 04:14:26 +00:00
|
|
|
public function doUnlock( string $lockName, string $method ) {
|
2020-03-20 13:16:46 +00:00
|
|
|
$result = $this->query(
|
2022-08-01 20:29:15 +00:00
|
|
|
$this->platform->unlockSQLText( $lockName ),
|
2020-03-20 13:16:46 +00:00
|
|
|
$method,
|
2021-12-14 01:19:35 +00:00
|
|
|
self::QUERY_CHANGE_LOCKS
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $result->fetchObject();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2021-06-09 04:14:26 +00:00
|
|
|
return ( $row->released === 't' );
|
2013-02-01 19:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-18 00:08:39 +00:00
|
|
|
protected function doFlushSession( $fname ) {
|
2022-05-31 22:55:13 +00:00
|
|
|
$flags = self::QUERY_CHANGE_LOCKS | self::QUERY_NO_RETRY;
|
2022-03-18 00:08:39 +00:00
|
|
|
|
|
|
|
|
// https://www.postgresql.org/docs/9.1/functions-admin.html
|
|
|
|
|
$sql = "pg_advisory_unlock_all()";
|
2020-02-19 08:34:41 +00:00
|
|
|
$qs = $this->executeQuery( $sql, __METHOD__, $flags, $sql );
|
|
|
|
|
if ( $qs->res === false ) {
|
|
|
|
|
$this->reportQueryError( $qs->message, $qs->code, $sql, $fname, true );
|
2022-03-18 00:08:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-15 08:36:39 +00:00
|
|
|
public function serverIsReadOnly() {
|
2020-03-20 13:16:46 +00:00
|
|
|
$res = $this->query(
|
|
|
|
|
"SHOW default_transaction_read_only",
|
|
|
|
|
__METHOD__,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE
|
|
|
|
|
);
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2017-06-15 08:36:39 +00:00
|
|
|
|
|
|
|
|
return $row ? ( strtolower( $row->default_transaction_read_only ) === 'on' ) : false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 09:31:57 +00:00
|
|
|
public static function getAttributes() {
|
2019-03-26 06:54:39 +00:00
|
|
|
return [ self::ATTR_SCHEMAS_AS_TABLE_GROUPS => true ];
|
|
|
|
|
}
|
2016-09-19 17:37:57 +00:00
|
|
|
}
|
2017-02-07 04:49:57 +00:00
|
|
|
|
2018-05-29 16:21:31 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated since 1.29
|
|
|
|
|
*/
|
2017-02-07 04:49:57 +00:00
|
|
|
class_alias( DatabasePostgres::class, 'DatabasePostgres' );
|