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
|
|
|
*/
|
2024-02-08 23:12:50 +00:00
|
|
|
|
2017-02-07 04:49:57 +00:00
|
|
|
namespace Wikimedia\Rdbms;
|
|
|
|
|
|
2025-06-05 18:51:15 +00:00
|
|
|
use PgSql\Connection;
|
|
|
|
|
use PgSql\Result;
|
2020-03-31 01:11:44 +00:00
|
|
|
use RuntimeException;
|
2021-12-08 12:08:11 +00:00
|
|
|
use Wikimedia\Rdbms\Platform\PostgresPlatform;
|
2022-10-13 14:03:27 +00:00
|
|
|
use Wikimedia\Rdbms\Replication\ReplicationReporter;
|
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;
|
2019-07-11 02:35:46 +00:00
|
|
|
/** @var float|string */
|
|
|
|
|
private $numericVersion;
|
|
|
|
|
|
2025-06-05 18:51:15 +00:00
|
|
|
/** @var Result|null */
|
2019-07-11 02:35:46 +00:00
|
|
|
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
|
|
|
*/
|
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 );
|
2016-09-19 23:34:32 +00:00
|
|
|
parent::__construct( $params );
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
|
2022-05-17 19:25:35 +00:00
|
|
|
$this->platform = new PostgresPlatform(
|
|
|
|
|
$this,
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$this->logger,
|
2022-08-16 08:23:34 +00:00
|
|
|
$this->currentDomain,
|
|
|
|
|
$this->errorLogger
|
2022-05-17 19:25:35 +00:00
|
|
|
);
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$this->replicationReporter = new ReplicationReporter(
|
|
|
|
|
$params['topologyRole'],
|
|
|
|
|
$this->logger,
|
|
|
|
|
$params['srvCache']
|
|
|
|
|
);
|
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.
|
2022-11-15 02:28:24 +00:00
|
|
|
'dbname' => ( $db !== null && $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
|
|
|
];
|
2022-11-15 02:28:24 +00:00
|
|
|
if ( $server !== null && $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 ) {
|
2023-05-24 20:26:52 +00:00
|
|
|
$sql = 'SET ' . $this->platform->addIdentifierQuotes( $var ) . ' = ' . $this->addQuotes( $val );
|
2023-12-01 07:35:31 +00:00
|
|
|
$query = new Query( $sql, self::QUERY_NO_RETRY | self::QUERY_CHANGE_TRX, 'SET' );
|
2023-05-24 20:26:52 +00:00
|
|
|
$this->query( $query, __METHOD__ );
|
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 ) {
|
2022-09-23 00:49:00 +00:00
|
|
|
$database = $domain->getDatabase();
|
|
|
|
|
if ( $database === null ) {
|
|
|
|
|
// A null database means "don't care" so leave it as is and update the table prefix
|
|
|
|
|
$this->currentDomain = new DatabaseDomain(
|
|
|
|
|
$this->currentDomain->getDatabase(),
|
|
|
|
|
$domain->getSchema() ?? $this->currentDomain->getSchema(),
|
|
|
|
|
$domain->getTablePrefix()
|
|
|
|
|
);
|
|
|
|
|
$this->platform->setCurrentDomain( $this->currentDomain );
|
|
|
|
|
} elseif ( $this->getDBname() !== $database ) {
|
2018-08-14 23:44:41 +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
|
|
|
|
|
$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],
|
2022-09-23 00:49:00 +00:00
|
|
|
$database,
|
2018-08-14 23:44:41 +00:00
|
|
|
$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;
|
2024-05-23 18:44:26 +00:00
|
|
|
$this->platform->setCurrentDomain( $domain );
|
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
|
|
|
}
|
|
|
|
|
|
2025-06-05 18:51:15 +00:00
|
|
|
protected function getBindingHandle(): Connection {
|
|
|
|
|
return parent::getBindingHandle();
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
2022-06-27 00:51:06 +00:00
|
|
|
$s .= "$name='" . str_replace( [ "\\", "'" ], [ "\\\\", "\\'" ], $value ) . "' ";
|
2008-10-06 00:45:18 +00:00
|
|
|
}
|
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
|
2024-08-31 21:34:30 +00:00
|
|
|
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
|
2020-02-19 08:34:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
$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 ),
|
2023-04-26 18:53:56 +00:00
|
|
|
$pgRes ? pg_affected_rows( $pgRes ) : 0,
|
2020-02-19 08:34:41 +00:00
|
|
|
$this->lastError(),
|
|
|
|
|
$this->lastErrno()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$this->logger->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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 22:10:31 +00:00
|
|
|
protected function lastInsertId() {
|
|
|
|
|
// Avoid using query() to prevent unwanted side-effects like changing affected
|
|
|
|
|
// row counts or connection retries. Note that lastval() is connection-specific.
|
|
|
|
|
// Note that this causes "lastval is not yet defined in this session" errors if
|
|
|
|
|
// nextval() was never directly or implicitly triggered (error out any transaction).
|
|
|
|
|
$qs = $this->doSingleStatementQuery( "SELECT lastval() AS id" );
|
|
|
|
|
|
|
|
|
|
return $qs->res ? (int)$qs->res->fetchRow()['id'] : 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 {
|
2022-10-13 22:46:04 +00:00
|
|
|
return pg_last_error() ?: $this->lastConnectError;
|
2012-03-30 03:06:43 +00:00
|
|
|
}
|
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-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 = []
|
2024-04-18 22:30:23 +00:00
|
|
|
): int {
|
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__ ) {
|
2024-05-17 05:01:10 +00:00
|
|
|
$components = $this->platform->qualifiedTableComponents( $table );
|
2024-09-18 00:49:46 +00:00
|
|
|
if ( count( $components ) === 1 ) {
|
|
|
|
|
$schema = $this->getCoreSchema();
|
|
|
|
|
$tableComponent = $components[0];
|
|
|
|
|
} elseif ( count( $components ) === 2 ) {
|
|
|
|
|
[ $schema, $tableComponent ] = $components;
|
|
|
|
|
} else {
|
|
|
|
|
[ , $schema, $tableComponent ] = $components;
|
|
|
|
|
}
|
|
|
|
|
$encSchema = $this->addQuotes( $schema );
|
|
|
|
|
$encTable = $this->addQuotes( $tableComponent );
|
2024-05-17 05:01:10 +00:00
|
|
|
$encIndex = $this->addQuotes( $this->platform->indexName( $index ) );
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2024-05-17 05:01:10 +00:00
|
|
|
"SELECT indexname,indexdef FROM pg_indexes " .
|
2024-09-18 00:49:46 +00:00
|
|
|
"WHERE schemaname=$encSchema AND tablename=$encTable AND indexname=$encIndex",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query );
|
2024-05-17 05:01:10 +00:00
|
|
|
$row = $res->fetchObject();
|
|
|
|
|
|
|
|
|
|
if ( $row ) {
|
|
|
|
|
return [ 'unique' => ( strpos( $row->indexdef, 'CREATE UNIQUE ' ) === 0 ) ];
|
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__;
|
2023-12-01 07:35:31 +00:00
|
|
|
$query = new Query( $sql, $flags, 'SELECT' );
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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
|
|
|
}
|
|
|
|
|
|
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
|
2023-05-24 20:26:52 +00:00
|
|
|
$destTableEnc = $this->tableName( $destTable );
|
2022-07-29 14:04:44 +00:00
|
|
|
|
|
|
|
|
$selectSql = $this->selectSQLText(
|
|
|
|
|
$srcTable,
|
|
|
|
|
array_values( $varMap ),
|
|
|
|
|
$conds,
|
|
|
|
|
$fname,
|
|
|
|
|
$selectOptions,
|
|
|
|
|
$selectJoinConds
|
|
|
|
|
);
|
2018-03-18 00:29:31 +00:00
|
|
|
|
2023-05-24 20:26:52 +00:00
|
|
|
$sql = "INSERT INTO $destTableEnc (" . implode( ',', array_keys( $varMap ) ) . ') ' .
|
2022-07-29 14:04:44 +00:00
|
|
|
$selectSql . ' ON CONFLICT DO NOTHING';
|
2023-12-11 02:56:41 +00:00
|
|
|
$query = new Query( $sql, self::QUERY_CHANGE_ROWS, 'INSERT', $destTable );
|
2023-05-24 20:26:52 +00:00
|
|
|
$this->query( $query, $fname );
|
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
|
|
|
|
2022-11-16 20:19:20 +00:00
|
|
|
public function getValueTypesForWithClause( $table ) {
|
|
|
|
|
$typesByColumn = [];
|
|
|
|
|
|
|
|
|
|
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
|
|
|
|
|
$encTable = $this->addQuotes( $table );
|
|
|
|
|
foreach ( $this->getCoreSchemas() as $schema ) {
|
|
|
|
|
$encSchema = $this->addQuotes( $schema );
|
|
|
|
|
$sql = "SELECT column_name,udt_name " .
|
|
|
|
|
"FROM information_schema.columns " .
|
|
|
|
|
"WHERE table_name = $encTable AND table_schema = $encSchema";
|
2023-12-01 07:35:31 +00:00
|
|
|
$query = new Query( $sql, $flags, 'SELECT' );
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
2022-11-16 20:19:20 +00:00
|
|
|
if ( $res->numRows() ) {
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$typesByColumn[$row->column_name] = $row->udt_name;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $typesByColumn;
|
|
|
|
|
}
|
|
|
|
|
|
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' : '';
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2019-03-20 04:04:35 +00:00
|
|
|
"CREATE $temporary TABLE $newNameE " .
|
2023-05-24 20:26:52 +00:00
|
|
|
"(LIKE $oldNameE INCLUDING DEFAULTS INCLUDING INDEXES)",
|
|
|
|
|
self::QUERY_PSEUDO_PERMANENT | self::QUERY_CHANGE_SCHEMA,
|
2023-12-05 22:13:25 +00:00
|
|
|
$temporary ? 'CREATE TEMPORARY' : 'CREATE',
|
2023-12-01 07:35:31 +00:00
|
|
|
// Use a dot to avoid double-prefixing in Database::getTempTableWrites()
|
2023-12-11 02:56:41 +00:00
|
|
|
'.' . $newName
|
2019-03-20 04:04:35 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$ret = $this->query( $query, $fname );
|
2018-03-18 17:23:58 +00:00
|
|
|
if ( !$ret ) {
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 20:26:52 +00:00
|
|
|
$sql = '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 )
|
2023-05-24 20:26:52 +00:00
|
|
|
. ' AND pg_get_expr(adbin, adrelid) LIKE \'nextval(%\'';
|
|
|
|
|
$query = new Query(
|
|
|
|
|
$sql,
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2018-03-18 17:23:58 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
|
|
|
|
|
$res = $this->query( $query, $fname );
|
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 );
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2019-03-20 04:04:35 +00:00
|
|
|
"CREATE $temporary SEQUENCE $newSeqE OWNED BY $newNameE.$fieldE",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_CHANGE_SCHEMA,
|
|
|
|
|
'CREATE',
|
2023-12-01 07:35:31 +00:00
|
|
|
// Do not treat this is as a table modification on top of the CREATE above.
|
2023-12-11 02:56:41 +00:00
|
|
|
null
|
2019-03-20 04:04:35 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$this->query( $query, $fname );
|
|
|
|
|
$query = new Query(
|
2018-03-18 17:23:58 +00:00
|
|
|
"ALTER TABLE $newNameE ALTER COLUMN $fieldE SET DEFAULT nextval({$newSeqQ}::regclass)",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_CHANGE_SCHEMA,
|
|
|
|
|
'ALTER',
|
2023-12-01 07:35:31 +00:00
|
|
|
// Do not treat this is as a table modification on top of the CREATE above.
|
2023-12-11 02:56:41 +00:00
|
|
|
null
|
2018-03-18 17:23:58 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$this->query( $query, $fname );
|
2018-03-18 17:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-03 01:52:33 +00:00
|
|
|
public function truncateTable( $table, $fname = __METHOD__ ) {
|
|
|
|
|
$sql = "TRUNCATE TABLE " . $this->tableName( $table ) . " RESTART IDENTITY";
|
|
|
|
|
$query = new Query( $sql, self::QUERY_CHANGE_SCHEMA, 'TRUNCATE', $table );
|
|
|
|
|
$this->query( $query, $fname );
|
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() ) );
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SELECT DISTINCT tablename FROM pg_tables WHERE schemaname IN ($eschemas)",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$result = $this->query( $query, $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 );
|
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() {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SELECT current_schema()",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
|
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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() {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2019-06-13 12:46:03 +00:00
|
|
|
"SELECT current_schemas(false)",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
|
|
|
|
'SELECT'
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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() {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SHOW search_path",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
|
|
|
|
'SHOW'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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 ) {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2019-06-13 12:46:03 +00:00
|
|
|
"SET search_path = " . implode( ", ", $search_path ),
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_CHANGE_TRX,
|
|
|
|
|
'SET'
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$this->query( $query, __METHOD__ );
|
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
|
|
|
*
|
2023-01-29 14:30:57 +00:00
|
|
|
* @param string|null $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 );
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$this->logger->debug(
|
2016-09-19 17:37:57 +00:00
|
|
|
"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 );
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$this->logger->debug(
|
2016-09-19 17:37:57 +00:00
|
|
|
"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() );
|
rdbms: Consolidate logger channels into one
Notable changes:
* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
injected. The rest implicitly got a NullLogger due to being absent.
These are now effectively unsilenced.
* Database::__construct() required almost all parameters, even the
loggers. I've wanted to move some of DatabaseFactory into the ctor
here for a while. In order to make this change not a breaking
change, the new 'logger' parameter is optional with NullLogger as
default. This allowed some of the test cases, which were simply
passing NullLogger, to be fixed by passing nothing instead of
passing the new option name.
The Database class is behind a dozen layers of indirection for
real use, so this will still be injected just fine (DBF, LB, LBF,
MWLBF, etc.).
* In LegacyLogger, the handling for $wgDBerrorLog was previously
limited to DBConnection and DBQuery. This now includes errors
from other (generally, newer) parts of Rdbms as well, which were
previously missing.
This only affects sites (typically CI and dev setup) where
$wgDBerrorLog is used, as opposed to the more common
$wgDebugLogGroups by-channel configuration.
* TransactionProfiler gets its logger injected in a rather odd way,
via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
as opposed to service wiring. This is kept as-is for now.
* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
there are cases that intentionally produce failures of which
the result is then observed. In CI we assert that dberror.log is
empty so instead of adding the missing logger fields to that
LBFactory instance, the only one set (replLogger) is removed.
The alternative is to set 'logger' now, which would naturally
cause CI failures due to unexpected entries coming through to
non-mocked error log.
Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
|
|
|
$this->logger->debug(
|
2016-09-19 17:37:57 +00:00
|
|
|
"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() ];
|
|
|
|
|
}
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SELECT nspname FROM pg_catalog.pg_namespace n WHERE n.oid = pg_my_temp_schema()",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2018-03-18 03:13:43 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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 ) ) {
|
2024-03-25 22:10:21 +00:00
|
|
|
// Works on PG 7.4+
|
|
|
|
|
$this->numericVersion = pg_version( $this->getBindingHandle() )['server'];
|
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
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2006-06-27 17:06:36 +00:00
|
|
|
*/
|
2024-05-16 20:22:09 +00:00
|
|
|
private function relationExists( $table, $types ) {
|
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
|
|
|
}
|
2024-05-16 20:22:09 +00:00
|
|
|
$schemas = $this->getCoreSchemas();
|
2024-04-12 00:39:15 +00:00
|
|
|
$components = $this->platform->qualifiedTableComponents( $table );
|
|
|
|
|
$etable = $this->addQuotes( end( $components ) );
|
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 ) . "')";
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
$sql,
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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
|
|
|
}
|
|
|
|
|
|
2024-05-16 20:22:09 +00:00
|
|
|
public function tableExists( $table, $fname = __METHOD__ ) {
|
|
|
|
|
return $this->relationExists( $table, [ 'r', 'v' ] );
|
2007-03-19 02:40:32 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2024-05-16 20:22:09 +00:00
|
|
|
public function sequenceExists( $sequence ) {
|
|
|
|
|
return $this->relationExists( $sequence, 'S' );
|
2007-03-19 02:40:32 +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 )
|
|
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
$sql,
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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.
|
2023-01-29 14:30:57 +00:00
|
|
|
* @param string|null $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 ) {
|
2023-01-29 14:30:57 +00:00
|
|
|
if ( !strlen( $schema ?? '' ) ) {
|
2016-10-26 23:07:12 +00:00
|
|
|
return false; // short-circuit
|
|
|
|
|
}
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2019-06-13 12:46:03 +00:00
|
|
|
"SELECT 1 FROM pg_catalog.pg_namespace " .
|
|
|
|
|
"WHERE nspname = " . $this->addQuotes( $schema ) . " LIMIT 1",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2019-06-13 12:46:03 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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 ) {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SELECT 1 FROM pg_catalog.pg_roles " .
|
|
|
|
|
"WHERE rolname = " . $this->addQuotes( $roleName ) . " LIMIT 1",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
2023-12-01 07:35:31 +00:00
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
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 ) {
|
2022-11-15 02:28:24 +00:00
|
|
|
$conn = $this->getBindingHandle();
|
|
|
|
|
|
|
|
|
|
return new PostgresBlob( pg_escape_bytea( $conn, $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 ) {
|
2024-05-13 17:42:50 +00:00
|
|
|
if ( $s instanceof RawSQLValue ) {
|
|
|
|
|
return $s->toSql();
|
|
|
|
|
}
|
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'";
|
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
|
2023-05-20 12:16:32 +00:00
|
|
|
if ( str_starts_with( $newLine, '$mw$' ) ) {
|
2012-01-11 20:19:55 +00:00
|
|
|
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 ) {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2022-08-01 20:29:15 +00:00
|
|
|
$this->platform->lockIsFreeSQLText( $lockName ),
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_CHANGE_LOCKS,
|
|
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, $method );
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2023-11-28 23:40:48 +00:00
|
|
|
return (bool)$row->unlocked;
|
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 ) {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
|
|
|
|
$this->platform->lockSQLText( $lockName, $timeout ),
|
|
|
|
|
self::QUERY_CHANGE_LOCKS,
|
|
|
|
|
'SELECT'
|
|
|
|
|
);
|
2021-06-09 04:14:26 +00:00
|
|
|
|
|
|
|
|
$acquired = null;
|
2016-09-08 07:12:56 +00:00
|
|
|
$loop = new WaitConditionLoop(
|
2023-05-24 20:26:52 +00:00
|
|
|
function () use ( $query, $method, &$acquired ) {
|
|
|
|
|
$res = $this->query( $query, $method );
|
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 ) {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2022-08-01 20:29:15 +00:00
|
|
|
$this->platform->unlockSQLText( $lockName ),
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_CHANGE_LOCKS,
|
|
|
|
|
'SELECT'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$result = $this->query( $query, $method );
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $result->fetchObject();
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2023-11-28 23:40:48 +00:00
|
|
|
return (bool)$row->released;
|
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
|
2023-07-24 13:10:34 +00:00
|
|
|
$sql = "SELECT pg_advisory_unlock_all()";
|
2023-04-21 09:41:25 +00:00
|
|
|
$query = new Query( $sql, $flags, 'UNLOCK' );
|
2023-05-08 10:18:16 +00:00
|
|
|
$qs = $this->executeQuery( $query, __METHOD__, $flags );
|
2020-02-19 08:34:41 +00:00
|
|
|
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() {
|
2023-05-24 20:26:52 +00:00
|
|
|
$query = new Query(
|
2020-03-20 13:16:46 +00:00
|
|
|
"SHOW default_transaction_read_only",
|
2023-05-24 20:26:52 +00:00
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
|
|
|
|
'SHOW'
|
2020-03-20 13:16:46 +00:00
|
|
|
);
|
2023-05-24 20:26:52 +00:00
|
|
|
$res = $this->query( $query, __METHOD__ );
|
2022-01-19 00:42:16 +00:00
|
|
|
$row = $res->fetchObject();
|
2017-06-15 08:36:39 +00:00
|
|
|
|
2023-03-24 01:14:02 +00:00
|
|
|
return $row && strtolower( $row->default_transaction_read_only ) === 'on';
|
2017-06-15 08:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-28 22:10:31 +00:00
|
|
|
protected function getInsertIdColumnForUpsert( $table ) {
|
|
|
|
|
$column = null;
|
|
|
|
|
|
|
|
|
|
$flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE;
|
2024-04-12 00:39:15 +00:00
|
|
|
$components = $this->platform->qualifiedTableComponents( $table );
|
|
|
|
|
$encTable = $this->addQuotes( end( $components ) );
|
2022-07-28 22:10:31 +00:00
|
|
|
foreach ( $this->getCoreSchemas() as $schema ) {
|
|
|
|
|
$encSchema = $this->addQuotes( $schema );
|
|
|
|
|
$query = new Query(
|
|
|
|
|
"SELECT column_name,data_type,column_default " .
|
|
|
|
|
"FROM information_schema.columns " .
|
|
|
|
|
"WHERE table_name = $encTable AND table_schema = $encSchema",
|
|
|
|
|
self::QUERY_IGNORE_DBO_TRX | self::QUERY_CHANGE_NONE,
|
|
|
|
|
'SELECT'
|
|
|
|
|
);
|
|
|
|
|
$res = $this->query( $query, __METHOD__ );
|
|
|
|
|
if ( $res->numRows() ) {
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if (
|
|
|
|
|
$row->column_default !== null &&
|
|
|
|
|
str_starts_with( $row->column_default, "nextval(" ) &&
|
|
|
|
|
in_array( $row->data_type, [ 'integer', 'bigint' ], true )
|
|
|
|
|
) {
|
|
|
|
|
$column = $row->column_name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $column;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|