return false;
}
- protected function open( $server, $user, $password, $dbName ) {
+ protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
# Test for Postgres support, to avoid suppressed fatal error
if ( !function_exists( 'pg_connect' ) ) {
throw new DBConnectionError(
$this->server = $server;
$this->user = $user;
$this->password = $password;
- $this->dbName = $dbName;
$connectVars = [
// pg_connect() user $user as the default database. Since a database is *required*,
$this->query( "SET standard_conforming_strings = on", __METHOD__ );
$this->query( "SET bytea_output = 'escape'", __METHOD__ ); // PHP bug 53127
- $this->determineCoreSchema( $this->schema );
- // The schema to be used is now in the search path; no need for explicit qualification
- $this->schema = '';
+ $this->determineCoreSchema( $schema );
+ $this->currentDomain = new DatabaseDomain( $dbName, $schema, $tablePrefix );
- return $this->conn;
+ return (bool)$this->conn;
+ }
+
+ protected function relationSchemaQualifier() {
+ if ( $this->coreSchema === $this->currentDomain->getSchema() ) {
+ // The schema to be used is now in the search path; no need for explicit qualification
+ return '';
+ }
+
+ return parent::relationSchemaQualifier();
}
public function databasesAreIndependent() {
return true;
}
- /**
- * 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
- * @param string $db
- * @return bool
- * @throws DBUnexpectedError
- */
- public function selectDB( $db ) {
- if ( $this->dbName !== $db ) {
- return (bool)$this->open( $this->server, $this->user, $this->password, $db );
+ 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(
+ $this->server,
+ $this->user,
+ $this->password,
+ $domain->getDatabase(),
+ $domain->getSchema(),
+ $domain->getTablePrefix()
+ );
} else {
- return true;
+ $this->currentDomain = $domain;
}
+
+ return true;
}
/**
* @param array $insertOptions
* @param array $selectOptions
* @param array $selectJoinConds
- * @return bool
*/
- public function nativeInsertSelect(
+ protected function nativeInsertSelect(
$destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
$insertOptions = [], $selectOptions = [], $selectJoinConds = []
) {
$sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ') ' .
$selectSql . ' ON CONFLICT DO NOTHING';
- return $this->query( $sql, $fname );
+ $this->query( $sql, $fname );
} else {
// IGNORE and we don't have ON CONFLICT DO NOTHING, so just use the non-native version
- return $this->nonNativeInsertSelect(
+ $this->nonNativeInsertSelect(
$destTable, $srcTable, $varMap, $conds, $fname,
$insertOptions, $selectOptions, $selectJoinConds
);
}
+ } else {
+ parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
+ $insertOptions, $selectOptions, $selectJoinConds );
}
-
- return parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
- $insertOptions, $selectOptions, $selectJoinConds );
}
public function tableName( $name, $format = 'quoted' ) {
* @return string[]
*/
private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
- if ( false === $limit ) {
+ if ( $limit === false ) {
$limit = strlen( $text ) - 1;
$output = [];
}
- if ( '{}' == $text ) {
+ if ( $text == '{}' ) {
return $output;
}
do {
- if ( '{' != $text[$offset] ) {
+ if ( $text[$offset] != '{' ) {
preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
$text, $match, 0, $offset );
$offset += strlen( $match[0] );
- $output[] = ( '"' != $match[1][0]
+ $output[] = ( $match[1][0] != '"'
? $match[1]
: stripcslashes( substr( $match[1], 1, -1 ) ) );
- if ( '},' == $match[3] ) {
+ if ( $match[3] == '},' ) {
return $output;
}
} else {
return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
}
- public function getDBname() {
- return $this->dbName;
- }
-
public function getServer() {
return $this->server;
}