2004-06-07 02:59:58 +00:00
< ? php
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
/**
2006-12-25 22:54:01 +00:00
* This is the Postgres database abstraction layer .
2006-01-07 13:09:30 +00:00
*
2010-08-01 21:13:44 +00:00
* @ file
* @ ingroup Database
2004-09-02 23:28:24 +00:00
*/
2010-08-01 21:13:44 +00:00
2010-11-21 19:56:51 +00:00
class PostgresField implements Field {
2011-06-10 07:36:22 +00:00
private $name , $tablename , $type , $nullable , $max_length , $deferred , $deferrable , $conname ;
2007-03-19 02:40:32 +00:00
2011-02-19 00:44:38 +00:00
/**
* @ param $db DatabaseBase
* @ param $table
* @ param $field
* @ return null | PostgresField
*/
static function fromText ( $db , $table , $field ) {
global $wgDBmwschema ;
2007-03-19 02:40:32 +00:00
2009-12-07 08:51:52 +00:00
$q = <<< SQL
2010-09-05 18:00:33 +00:00
SELECT
2010-03-22 18:14:25 +00:00
attnotnull , attlen , COALESCE ( conname , '' ) AS conname ,
COALESCE ( condeferred , 'f' ) AS deferred ,
COALESCE ( condeferrable , 'f' ) AS deferrable ,
CASE WHEN typname = 'int2' THEN 'smallint'
WHEN typname = 'int4' THEN 'integer'
WHEN typname = 'int8' THEN 'bigint'
WHEN typname = 'bpchar' THEN 'char'
ELSE typname END AS typname
FROM pg_class c
JOIN pg_namespace n ON ( n . oid = c . relnamespace )
JOIN pg_attribute a ON ( a . attrelid = c . oid )
JOIN pg_type t ON ( t . oid = a . atttypid )
LEFT JOIN pg_constraint o ON ( o . conrelid = c . oid AND a . attnum = ANY ( o . conkey ) AND o . contype = 'f' )
WHERE relkind = 'r'
2007-03-25 23:53:36 +00:00
AND nspname =% s
AND relname =% s
AND attname =% s ;
2009-12-07 08:51:52 +00:00
SQL ;
2010-07-02 10:01:09 +00:00
2011-09-06 20:51:10 +00:00
$table = $db -> tableName ( $table , 'raw' );
2010-09-05 18:35:34 +00:00
$res = $db -> query (
sprintf ( $q ,
$db -> addQuotes ( $wgDBmwschema ),
$db -> addQuotes ( $table ),
$db -> addQuotes ( $field )
)
);
$row = $db -> fetchObject ( $res );
if ( ! $row ) {
2007-03-19 02:40:32 +00:00
return null ;
2010-09-05 18:35:34 +00:00
}
2007-03-19 02:40:32 +00:00
$n = new PostgresField ;
$n -> type = $row -> typname ;
2010-09-05 18:35:34 +00:00
$n -> nullable = ( $row -> attnotnull == 'f' );
2007-03-19 02:40:32 +00:00
$n -> name = $field ;
$n -> tablename = $table ;
$n -> max_length = $row -> attlen ;
2010-09-05 18:35:34 +00:00
$n -> deferrable = ( $row -> deferrable == 't' );
$n -> deferred = ( $row -> deferred == 't' );
2010-03-22 18:14:25 +00:00
$n -> conname = $row -> conname ;
2007-03-19 02:40:32 +00:00
return $n ;
}
function name () {
return $this -> name ;
}
function tableName () {
return $this -> tablename ;
}
function type () {
return $this -> type ;
}
2010-11-21 19:56:51 +00:00
function isNullable () {
2007-03-19 02:40:32 +00:00
return $this -> nullable ;
}
function maxLength () {
return $this -> max_length ;
}
2010-03-22 18:14:25 +00:00
function is_deferrable () {
return $this -> deferrable ;
}
function is_deferred () {
return $this -> deferred ;
}
function conname () {
return $this -> conname ;
}
2007-03-19 02:40:32 +00:00
}
2007-04-20 08:55:14 +00:00
/**
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
* @ ingroup Database
2007-04-20 08:55:14 +00:00
*/
2009-06-12 17:59:04 +00:00
class DatabasePostgres extends DatabaseBase {
2009-12-11 21:07:27 +00:00
var $mInsertId = null ;
var $mLastResult = null ;
var $numeric_version = null ;
var $mAffectedRows = null ;
2004-06-07 02:59:58 +00:00
2010-01-08 00:31:24 +00:00
function getType () {
return 'postgres' ;
}
2007-03-21 18:19:35 +00:00
function cascadingDeletes () {
return true ;
}
function cleanupTriggers () {
return true ;
}
function strictIPs () {
return true ;
}
2006-12-01 04:37:48 +00:00
function realTimestamps () {
return true ;
}
2007-01-02 21:34:42 +00:00
function implicitGroupby () {
return false ;
}
2007-09-02 18:03:10 +00:00
function implicitOrderby () {
return false ;
}
2007-01-23 14:47:12 +00:00
function searchableIPs () {
return true ;
}
2007-07-30 14:10:42 +00:00
function functionalIndexes () {
return true ;
}
2007-01-23 14:47:12 +00:00
2007-09-23 22:23:01 +00:00
function hasConstraint ( $name ) {
2008-02-04 16:45:09 +00:00
global $wgDBmwschema ;
2010-10-14 20:53:04 +00:00
$SQL = " SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = ' " .
pg_escape_string ( $this -> mConn , $name ) . " ' AND n.nspname = ' " . pg_escape_string ( $this -> mConn , $wgDBmwschema ) . " ' " ;
$res = $this -> doQuery ( $SQL );
return $this -> numRows ( $res );
2007-09-23 22:23:01 +00:00
}
2004-09-02 23:28:24 +00:00
/**
* Usually aborts on failure
2012-02-09 21:33:27 +00:00
* @ return DatabaseBase | null
2004-09-02 23:28:24 +00:00
*/
2006-06-27 16:11:47 +00:00
function open ( $server , $user , $password , $dbName ) {
2006-08-13 14:35:51 +00:00
# Test for Postgres support, to avoid suppressed fatal error
2004-07-10 03:09:26 +00:00
if ( ! function_exists ( 'pg_connect' ) ) {
2006-08-17 02:48:39 +00:00
throw new DBConnectionError ( $this , " Postgres functions missing, have you compiled PHP with the --with-pgsql option? \n (Note: if you recently installed PHP, you may need to restart your webserver and database) \n " );
2004-07-10 03:09:26 +00:00
}
2011-06-10 07:36:22 +00:00
global $wgDBport ;
2004-09-17 13:47:28 +00:00
2010-09-05 18:35:34 +00:00
if ( ! strlen ( $user ) ) { # e.g. the class is being loaded
2010-02-05 05:40:50 +00:00
return ;
2008-11-22 06:39:55 +00:00
}
2011-02-03 05:17:18 +00:00
2004-06-07 02:59:58 +00:00
$this -> close ();
$this -> mServer = $server ;
2011-02-23 13:53:47 +00:00
$port = $wgDBport ;
2004-06-07 02:59:58 +00:00
$this -> mUser = $user ;
$this -> mPassword = $password ;
$this -> mDBname = $dbName ;
2006-01-07 13:31:29 +00:00
2008-10-06 00:45:18 +00:00
$connectVars = array (
'dbname' => $dbName ,
'user' => $user ,
2010-09-05 18:35:34 +00:00
'password' => $password
);
if ( $server != false && $server != '' ) {
2008-10-06 00:45:18 +00:00
$connectVars [ 'host' ] = $server ;
2006-06-28 18:05:08 +00:00
}
2010-09-05 18:35:34 +00:00
if ( $port != false && $port != '' ) {
2008-10-06 00:45:18 +00:00
$connectVars [ 'port' ] = $port ;
2006-06-28 18:05:08 +00:00
}
2010-02-05 05:40:50 +00:00
$connectString = $this -> makeConnectionString ( $connectVars , PGSQL_CONNECT_FORCE_NEW );
2006-06-28 18:05:08 +00:00
2008-10-06 00:45:18 +00:00
$this -> installErrorHandler ();
$this -> mConn = pg_connect ( $connectString );
$phpError = $this -> restoreErrorHandler ();
2006-06-28 18:05:08 +00:00
2010-06-09 11:44:05 +00:00
if ( ! $this -> mConn ) {
2006-06-28 18:05:08 +00:00
wfDebug ( " DB connection error \n " );
wfDebug ( " Server: $server , Database: $dbName , User: $user , Password: " . substr ( $password , 0 , 3 ) . " ... \n " );
2010-09-05 18:35:34 +00:00
wfDebug ( $this -> lastError () . " \n " );
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
throw new DBConnectionError ( $this , str_replace ( " \n " , ' ' , $phpError ) );
2006-06-28 18:05:08 +00:00
}
$this -> mOpened = true ;
2006-08-18 16:24:48 +00:00
global $wgCommandLineMode ;
2010-09-05 18:35:34 +00:00
# If called from the command-line (e.g. importDump), only show errors
if ( $wgCommandLineMode ) {
2009-02-23 21:23:14 +00:00
$this -> doQuery ( " SET client_min_messages = 'ERROR' " );
2006-08-18 16:24:48 +00:00
}
2011-02-23 13:51:00 +00:00
$this -> query ( " SET client_encoding='UTF8' " , __METHOD__ );
$this -> query ( " SET datestyle = 'ISO, YMD' " , __METHOD__ );
$this -> query ( " SET timezone = 'GMT' " , __METHOD__ );
2011-10-18 21:09:52 +00:00
$this -> query ( " SET standard_conforming_strings = on " , __METHOD__ );
2009-02-23 21:23:14 +00:00
2011-01-28 17:25:00 +00:00
global $wgDBmwschema ;
2011-02-23 16:01:22 +00:00
if ( $this -> schemaExists ( $wgDBmwschema ) ) {
2010-12-04 15:14:08 +00:00
$safeschema = $this -> addIdentifierQuotes ( $wgDBmwschema );
2011-02-23 16:01:22 +00:00
$this -> doQuery ( " SET search_path = $safeschema " );
} else {
$this -> doQuery ( " SET search_path = public " );
2007-09-19 02:31:28 +00:00
}
2004-06-07 02:59:58 +00:00
return $this -> mConn ;
}
2006-01-07 13:31:29 +00:00
2011-02-23 16:01:22 +00:00
/**
* Postgres doesn ' t support selectDB in the same way MySQL does . So if the
* DB name doesn ' t match the open connection , open a new one
* @ return
*/
function selectDB ( $db ) {
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
if ( $this -> mDBname !== $db ) {
return ( bool ) $this -> open ( $this -> mServer , $this -> mUser , $this -> mPassword , $db );
} else {
return true ;
}
2011-02-23 16:01:22 +00:00
}
2008-10-06 00:45:18 +00:00
function makeConnectionString ( $vars ) {
$s = '' ;
foreach ( $vars as $name => $value ) {
$s .= " $name =' " . str_replace ( " ' " , " \\ ' " , $value ) . " ' " ;
}
return $s ;
}
2004-09-02 23:28:24 +00:00
/**
* Closes a database connection , if it is open
* Returns success , true if already closed
2012-02-09 21:33:27 +00:00
* @ return bool
2004-09-02 23:28:24 +00:00
*/
function close () {
2011-06-10 07:36:22 +00:00
$this -> mOpened = false ;
if ( $this -> mConn ) {
return pg_close ( $this -> mConn );
} else {
return true ;
2004-06-07 02:59:58 +00:00
}
}
2006-01-07 13:31:29 +00:00
2011-06-20 12:09:22 +00:00
protected function doQuery ( $sql ) {
2010-09-05 18:35:34 +00:00
if ( function_exists ( 'mb_convert_encoding' ) ) {
$sql = mb_convert_encoding ( $sql , 'UTF-8' );
2007-11-05 15:21:59 +00:00
}
2011-06-10 07:36:22 +00:00
$this -> mLastResult = pg_query ( $this -> mConn , $sql );
2009-12-11 21:07:27 +00:00
$this -> mAffectedRows = null ; // use pg_affected_rows(mLastResult)
2008-09-06 07:05:14 +00:00
return $this -> mLastResult ;
2004-06-07 02:59:58 +00:00
}
2006-01-07 13:31:29 +00:00
2010-11-26 12:06:50 +00:00
function queryIgnore ( $sql , $fname = 'DatabasePostgres::queryIgnore' ) {
2004-07-10 03:09:26 +00:00
return $this -> query ( $sql , $fname , true );
}
2006-01-07 13:31:29 +00:00
2004-06-07 02:59:58 +00:00
function freeResult ( $res ) {
2007-07-05 19:42:18 +00:00
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
2011-07-04 15:00:30 +00:00
wfSuppressWarnings ();
$ok = pg_free_result ( $res );
wfRestoreWarnings ();
if ( ! $ok ) {
2010-09-05 18:35:34 +00:00
throw new DBUnexpectedError ( $this , " Unable to free Postgres result \n " );
2004-06-07 02:59:58 +00:00
}
}
2006-01-07 13:31:29 +00:00
2004-06-07 02:59:58 +00:00
function fetchObject ( $res ) {
2007-07-05 19:42:18 +00:00
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
2011-07-04 15:00:30 +00:00
wfSuppressWarnings ();
$row = pg_fetch_object ( $res );
wfRestoreWarnings ();
2011-05-17 22:03:20 +00:00
# @todo FIXME: HACK HACK HACK HACK debug
2006-01-07 13:31:29 +00:00
2011-05-17 22:03:20 +00:00
# @todo hashar: not sure if the following test really trigger if the object
2007-05-15 02:59:20 +00:00
# fetching failed.
2010-09-05 18:35:34 +00:00
if ( pg_last_error ( $this -> mConn ) ) {
throw new DBUnexpectedError ( $this , 'SQL error: ' . htmlspecialchars ( pg_last_error ( $this -> mConn ) ) );
2004-06-07 02:59:58 +00:00
}
return $row ;
}
2004-06-10 13:02:27 +00:00
function fetchRow ( $res ) {
2007-07-05 19:42:18 +00:00
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
2011-07-04 15:00:30 +00:00
wfSuppressWarnings ();
$row = pg_fetch_array ( $res );
wfRestoreWarnings ();
2010-09-05 18:35:34 +00:00
if ( pg_last_error ( $this -> mConn ) ) {
throw new DBUnexpectedError ( $this , 'SQL error: ' . htmlspecialchars ( pg_last_error ( $this -> mConn ) ) );
2006-03-07 01:10:39 +00:00
}
2004-06-10 13:02:27 +00:00
return $row ;
}
2004-06-07 02:59:58 +00:00
function numRows ( $res ) {
2007-07-05 19:42:18 +00:00
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
2011-07-04 15:00:30 +00:00
wfSuppressWarnings ();
$n = pg_num_rows ( $res );
wfRestoreWarnings ();
2010-09-05 18:35:34 +00:00
if ( pg_last_error ( $this -> mConn ) ) {
throw new DBUnexpectedError ( $this , 'SQL error: ' . htmlspecialchars ( pg_last_error ( $this -> mConn ) ) );
2004-06-07 02:59:58 +00:00
}
return $n ;
}
2010-09-05 18:35:34 +00:00
2007-07-05 19:42:18 +00:00
function numFields ( $res ) {
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
return pg_num_fields ( $res );
}
2010-09-05 18:35:34 +00:00
2007-07-05 19:42:18 +00:00
function fieldName ( $res , $n ) {
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
return pg_field_name ( $res , $n );
}
2006-01-07 13:31:29 +00:00
2004-09-02 23:28:24 +00:00
/**
* This must be called after nextSequenceVal
2012-02-09 21:33:27 +00:00
* @ return null
2004-09-02 23:28:24 +00:00
*/
2006-01-07 13:09:30 +00:00
function insertId () {
2004-07-10 03:09:26 +00:00
return $this -> mInsertId ;
2004-06-07 02:59:58 +00:00
}
2004-07-10 03:09:26 +00:00
2007-07-05 19:42:18 +00:00
function dataSeek ( $res , $row ) {
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
return pg_result_seek ( $res , $row );
}
2006-06-27 00:53:46 +00:00
function lastError () {
if ( $this -> mConn ) {
return pg_last_error ();
2010-09-05 18:35:34 +00:00
} else {
return 'No database connection' ;
2006-06-27 00:53:46 +00:00
}
}
2006-07-21 19:34:45 +00:00
function lastErrno () {
return pg_last_error () ? 1 : 0 ;
}
2004-07-24 07:24:04 +00:00
2006-01-07 13:09:30 +00:00
function affectedRows () {
2008-09-06 07:05:14 +00:00
if ( ! is_null ( $this -> mAffectedRows ) ) {
// Forced result for simulated queries
return $this -> mAffectedRows ;
}
2010-09-05 18:35:34 +00:00
if ( empty ( $this -> mLastResult ) ) {
2007-05-15 02:59:20 +00:00
return 0 ;
2010-09-05 18:35:34 +00:00
}
2007-05-16 20:29:05 +00:00
return pg_affected_rows ( $this -> mLastResult );
2004-06-11 14:32:05 +00:00
}
2006-01-07 13:31:29 +00:00
2007-04-07 17:46:17 +00:00
/**
* Estimate rows in dataset
* Returns estimated count , based on EXPLAIN output
* This is not necessarily an accurate estimate , so use sparingly
* Returns - 1 if count cannot be found
* Takes same arguments as Database :: select ()
2012-02-09 21:33:27 +00:00
* @ return int
2007-04-07 17:46:17 +00:00
*/
2010-09-05 18:35:34 +00:00
function estimateRowCount ( $table , $vars = '*' , $conds = '' , $fname = 'DatabasePostgres::estimateRowCount' , $options = array () ) {
2007-04-07 17:46:17 +00:00
$options [ 'EXPLAIN' ] = true ;
$res = $this -> select ( $table , $vars , $conds , $fname , $options );
$rows = - 1 ;
if ( $res ) {
$row = $this -> fetchRow ( $res );
$count = array ();
if ( preg_match ( '/rows=(\d+)/' , $row [ 0 ], $count ) ) {
$rows = $count [ 1 ];
}
}
return $rows ;
}
2004-09-02 23:28:24 +00:00
/**
* Returns information about an index
* If errors are explicitly ignored , returns NULL on failure
2012-02-09 21:33:27 +00:00
* @ return bool | null
2004-09-02 23:28:24 +00:00
*/
2009-02-26 09:10:18 +00:00
function indexInfo ( $table , $index , $fname = 'DatabasePostgres::indexInfo' ) {
2004-06-09 16:13:46 +00:00
$sql = " SELECT indexname FROM pg_indexes WHERE tablename=' $table ' " ;
2004-07-10 03:09:26 +00:00
$res = $this -> query ( $sql , $fname );
2004-06-07 02:59:58 +00:00
if ( ! $res ) {
2009-12-11 21:07:27 +00:00
return null ;
2004-06-07 02:59:58 +00:00
}
2010-10-13 23:11:40 +00:00
foreach ( $res as $row ) {
2009-01-19 13:56:08 +00:00
if ( $row -> indexname == $this -> indexName ( $index ) ) {
2004-07-10 03:09:26 +00:00
return $row ;
2004-06-07 02:59:58 +00:00
}
}
2004-07-10 03:09:26 +00:00
return false ;
2004-06-07 02:59:58 +00:00
}
2010-09-05 18:35:34 +00:00
function indexUnique ( $table , $index , $fname = 'DatabasePostgres::indexUnique' ) {
2004-09-09 07:12:11 +00:00
$sql = " SELECT indexname FROM pg_indexes WHERE tablename=' { $table } ' " .
2010-09-05 18:00:33 +00:00
" AND indexdef LIKE 'CREATE UNIQUE%( " .
2009-01-19 13:56:08 +00:00
$this -> strencode ( $this -> indexName ( $index ) ) .
2009-01-15 14:20:28 +00:00
" )' " ;
2004-09-09 07:12:11 +00:00
$res = $this -> query ( $sql , $fname );
2010-09-05 18:35:34 +00:00
if ( ! $res ) {
2009-12-11 21:07:27 +00:00
return null ;
2010-09-05 18:35:34 +00:00
}
2010-10-13 23:11:40 +00:00
foreach ( $res as $row ) {
2004-09-09 07:12:11 +00:00
return true ;
2010-09-05 18:35:34 +00:00
}
2004-09-09 07:12:11 +00:00
return false ;
}
2007-05-15 12:14:20 +00:00
/**
* INSERT wrapper , inserts an array into a table
*
2008-04-14 07:45:50 +00:00
* $args may be a single associative array , or an array of these with numeric keys ,
2007-05-15 12:14:20 +00:00
* for multi - row insert ( Postgres version 8.2 and above only ) .
*
2008-11-29 18:50:39 +00:00
* @ param $table String : Name of the table to insert to .
* @ param $args Array : Items to insert into the table .
* @ param $fname String : Name of the function , for profiling
* @ param $options String or Array . Valid options : IGNORE
2007-05-15 12:14:20 +00:00
*
* @ return bool Success of insert operation . IGNORE always returns true .
*/
function insert ( $table , $args , $fname = 'DatabasePostgres::insert' , $options = array () ) {
2008-05-02 15:28:10 +00:00
if ( ! count ( $args ) ) {
return true ;
}
2007-05-15 12:14:20 +00:00
$table = $this -> tableName ( $table );
2010-07-25 22:09:34 +00:00
if ( ! isset ( $this -> numeric_version ) ) {
$this -> getServerVersion ();
2007-05-15 12:14:20 +00:00
}
2004-07-18 08:48:43 +00:00
2010-09-05 18:35:34 +00:00
if ( ! is_array ( $options ) ) {
2007-05-15 12:14:20 +00:00
$options = array ( $options );
2010-09-05 18:35:34 +00:00
}
2004-09-01 12:27:57 +00:00
2007-05-15 12:14:20 +00:00
if ( isset ( $args [ 0 ] ) && is_array ( $args [ 0 ] ) ) {
2007-05-15 02:59:20 +00:00
$multi = true ;
2007-05-15 12:14:20 +00:00
$keys = array_keys ( $args [ 0 ] );
2010-09-05 18:35:34 +00:00
} else {
2007-05-15 02:59:20 +00:00
$multi = false ;
2007-05-15 12:14:20 +00:00
$keys = array_keys ( $args );
2007-05-15 02:59:20 +00:00
}
2008-07-05 17:00:01 +00:00
// If IGNORE is set, we use savepoints to emulate mysql's behavior
2008-07-04 15:57:44 +00:00
$ignore = in_array ( 'IGNORE' , $options ) ? 'mw' : '' ;
2008-07-05 17:00:01 +00:00
// If we are not in a transaction, we need to be for savepoint trickery
$didbegin = 0 ;
2008-07-04 15:57:44 +00:00
if ( $ignore ) {
2010-09-05 18:35:34 +00:00
if ( ! $this -> mTrxLevel ) {
2008-07-04 15:57:44 +00:00
$this -> begin ();
$didbegin = 1 ;
}
2007-05-15 12:14:20 +00:00
$olde = error_reporting ( 0 );
2008-07-05 17:00:01 +00:00
// For future use, we may want to track the number of actual inserts
// Right now, insert (all writes) simply return true/false
$numrowsinserted = 0 ;
2008-07-04 15:57:44 +00:00
}
2008-07-05 17:00:01 +00:00
2007-05-15 02:59:20 +00:00
$sql = " INSERT INTO $table ( " . implode ( ',' , $keys ) . ') VALUES ' ;
if ( $multi ) {
2010-07-25 22:09:34 +00:00
if ( $this -> numeric_version >= 8.2 && ! $ignore ) {
2007-05-15 02:59:20 +00:00
$first = true ;
2007-05-15 12:14:20 +00:00
foreach ( $args as $row ) {
2007-05-15 02:59:20 +00:00
if ( $first ) {
$first = false ;
} else {
$sql .= ',' ;
}
$sql .= '(' . $this -> makeList ( $row ) . ')' ;
}
$res = ( bool ) $this -> query ( $sql , $fname , $ignore );
2010-09-05 18:35:34 +00:00
} else {
2007-05-15 02:59:20 +00:00
$res = true ;
$origsql = $sql ;
2007-05-15 12:14:20 +00:00
foreach ( $args as $row ) {
2007-05-15 02:59:20 +00:00
$tempsql = $origsql ;
$tempsql .= '(' . $this -> makeList ( $row ) . ')' ;
2008-07-05 17:00:01 +00:00
if ( $ignore ) {
2010-09-05 18:35:34 +00:00
pg_query ( $this -> mConn , " SAVEPOINT $ignore " );
2008-07-05 17:00:01 +00:00
}
2007-05-15 02:59:20 +00:00
$tempres = ( bool ) $this -> query ( $tempsql , $fname , $ignore );
2008-07-05 17:00:01 +00:00
if ( $ignore ) {
$bar = pg_last_error ();
2010-09-05 18:35:34 +00:00
if ( $bar != false ) {
2008-07-05 17:00:01 +00:00
pg_query ( $this -> mConn , " ROLLBACK TO $ignore " );
2010-09-05 18:35:34 +00:00
} else {
2008-07-05 17:00:01 +00:00
pg_query ( $this -> mConn , " RELEASE $ignore " );
$numrowsinserted ++ ;
}
}
// If any of them fail, we fail overall for this function call
// Note that this will be ignored if IGNORE is set
2010-09-05 18:35:34 +00:00
if ( ! $tempres ) {
2007-05-15 02:59:20 +00:00
$res = false ;
2010-09-05 18:35:34 +00:00
}
2007-05-15 02:59:20 +00:00
}
}
2010-09-05 18:35:34 +00:00
} else {
2008-07-05 17:00:01 +00:00
// Not multi, just a lone insert
if ( $ignore ) {
pg_query ( $this -> mConn , " SAVEPOINT $ignore " );
}
2007-05-15 12:14:20 +00:00
$sql .= '(' . $this -> makeList ( $args ) . ')' ;
2007-05-15 02:59:20 +00:00
$res = ( bool ) $this -> query ( $sql , $fname , $ignore );
2008-07-05 17:00:01 +00:00
if ( $ignore ) {
$bar = pg_last_error ();
2010-09-05 18:35:34 +00:00
if ( $bar != false ) {
2008-07-05 17:00:01 +00:00
pg_query ( $this -> mConn , " ROLLBACK TO $ignore " );
2010-09-05 18:35:34 +00:00
} else {
2008-07-05 17:00:01 +00:00
pg_query ( $this -> mConn , " RELEASE $ignore " );
$numrowsinserted ++ ;
}
}
2004-06-07 02:59:58 +00:00
}
2007-05-15 12:14:20 +00:00
if ( $ignore ) {
2007-05-15 02:59:20 +00:00
$olde = error_reporting ( $olde );
2010-09-05 18:35:34 +00:00
if ( $didbegin ) {
2008-07-04 15:57:44 +00:00
$this -> commit ();
}
2008-07-05 17:00:01 +00:00
2008-09-06 07:05:14 +00:00
// Set the affected row count for the whole operation
$this -> mAffectedRows = $numrowsinserted ;
2008-07-05 17:00:01 +00:00
// IGNORE always returns true
2007-05-15 12:14:20 +00:00
return true ;
}
2007-05-15 02:59:20 +00:00
return $res ;
2004-06-07 02:59:58 +00:00
}
2006-01-07 13:31:29 +00:00
2009-10-27 05:33:31 +00:00
/**
* INSERT SELECT wrapper
* $varMap must be an associative array of the form array ( 'dest1' => 'source1' , ... )
* Source items may be literals rather then field names , but strings should be quoted with Database :: addQuotes ()
* $conds may be " * " to copy the whole table
* srcTable may be an array of tables .
2011-05-17 22:03:20 +00:00
* @ todo FIXME : Implement this a little better ( seperate select / insert ) ?
2012-02-09 21:33:27 +00:00
* @ return bool
2010-09-05 18:35:34 +00:00
*/
2009-10-27 05:33:31 +00:00
function insertSelect ( $destTable , $srcTable , $varMap , $conds , $fname = 'DatabasePostgres::insertSelect' ,
$insertOptions = array (), $selectOptions = array () )
{
$destTable = $this -> tableName ( $destTable );
// If IGNORE is set, we use savepoints to emulate mysql's behavior
$ignore = in_array ( 'IGNORE' , $insertOptions ) ? 'mw' : '' ;
if ( is_array ( $insertOptions ) ) {
2011-10-29 01:17:26 +00:00
$insertOptions = implode ( ' ' , $insertOptions ); // FIXME: This is unused
2009-10-27 05:33:31 +00:00
}
if ( ! is_array ( $selectOptions ) ) {
$selectOptions = array ( $selectOptions );
}
list ( $startOpts , $useIndex , $tailOpts ) = $this -> makeSelectOptions ( $selectOptions );
if ( is_array ( $srcTable ) ) {
$srcTable = implode ( ',' , array_map ( array ( & $this , 'tableName' ), $srcTable ) );
} else {
$srcTable = $this -> tableName ( $srcTable );
}
// If we are not in a transaction, we need to be for savepoint trickery
$didbegin = 0 ;
if ( $ignore ) {
if ( ! $this -> mTrxLevel ) {
$this -> begin ();
$didbegin = 1 ;
}
$olde = error_reporting ( 0 );
$numrowsinserted = 0 ;
pg_query ( $this -> mConn , " SAVEPOINT $ignore " );
}
$sql = " INSERT INTO $destTable ( " . implode ( ',' , array_keys ( $varMap ) ) . ')' .
" SELECT $startOpts " . implode ( ',' , $varMap ) .
" FROM $srcTable $useIndex " ;
2010-09-05 18:35:34 +00:00
if ( $conds != '*' ) {
2009-10-27 05:33:31 +00:00
$sql .= ' WHERE ' . $this -> makeList ( $conds , LIST_AND );
}
$sql .= " $tailOpts " ;
$res = ( bool ) $this -> query ( $sql , $fname , $ignore );
if ( $ignore ) {
$bar = pg_last_error ();
if ( $bar != false ) {
pg_query ( $this -> mConn , " ROLLBACK TO $ignore " );
} else {
pg_query ( $this -> mConn , " RELEASE $ignore " );
$numrowsinserted ++ ;
}
$olde = error_reporting ( $olde );
if ( $didbegin ) {
$this -> commit ();
}
// Set the affected row count for the whole operation
$this -> mAffectedRows = $numrowsinserted ;
// IGNORE always returns true
return true ;
}
return $res ;
}
2010-09-05 18:00:33 +00:00
2011-09-06 20:51:10 +00:00
function tableName ( $name , $format = 'quoted' ) {
2012-02-03 23:37:30 +00:00
# Replace reserved words with better ones
switch ( $name ) {
2004-07-10 03:09:26 +00:00
case 'user' :
2012-02-03 23:37:30 +00:00
return 'mwuser' ;
2011-06-10 07:36:22 +00:00
case 'text' :
2012-02-03 23:37:30 +00:00
return 'pagecontent' ;
2011-06-10 07:36:22 +00:00
default :
2012-02-03 23:37:30 +00:00
return parent :: tableName ( $name , $format );
2004-07-10 03:09:26 +00:00
}
2004-06-07 02:59:58 +00:00
}
2004-09-02 23:28:24 +00:00
/**
* Return the next in a sequence , save the value for retrieval via insertId ()
2012-02-09 21:33:27 +00:00
* @ return null
2004-09-02 23:28:24 +00:00
*/
2004-07-10 03:09:26 +00:00
function nextSequenceValue ( $seqName ) {
2010-12-04 09:27:02 +00:00
$safeseq = str_replace ( " ' " , " '' " , $seqName );
2006-07-05 03:59:54 +00:00
$res = $this -> query ( " SELECT nextval(' $safeseq ') " );
$row = $this -> fetchRow ( $res );
$this -> mInsertId = $row [ 0 ];
return $this -> mInsertId ;
2004-07-10 03:09:26 +00:00
}
2004-06-07 02:59:58 +00:00
2007-12-18 15:44:18 +00:00
/**
2009-11-27 15:19:05 +00:00
* Return the current value of a sequence . Assumes it has been nextval ' ed in this session .
2012-02-09 21:33:27 +00:00
* @ return
2007-12-18 15:44:18 +00:00
*/
function currentSequenceValue ( $seqName ) {
2010-12-04 09:27:02 +00:00
$safeseq = str_replace ( " ' " , " '' " , $seqName );
2007-12-18 15:44:18 +00:00
$res = $this -> query ( " SELECT currval(' $safeseq ') " );
$row = $this -> fetchRow ( $res );
$currval = $row [ 0 ];
return $currval ;
}
2004-07-10 03:09:26 +00:00
# Returns the size of a text field, or -1 for "unlimited"
function textFieldSize ( $table , $field ) {
$table = $this -> tableName ( $table );
2004-09-11 14:08:52 +00:00
$sql = " SELECT t.typname as ftype,a.atttypmod as size
2006-01-07 13:09:30 +00:00
FROM pg_class c , pg_attribute a , pg_type t
WHERE relname = '$table' AND a . attrelid = c . oid AND
2004-09-11 14:08:52 +00:00
a . atttypid = t . oid and a . attname = '$field' " ;
2010-09-05 18:35:34 +00:00
$res = $this -> query ( $sql );
$row = $this -> fetchObject ( $res );
if ( $row -> ftype == 'varchar' ) {
$size = $row -> size - 4 ;
2004-09-11 14:08:52 +00:00
} else {
2010-09-05 18:35:34 +00:00
$size = $row -> size ;
2004-09-11 14:08:52 +00:00
}
2004-07-10 03:09:26 +00:00
return $size ;
}
2006-01-07 13:31:29 +00:00
2010-09-05 18:35:34 +00:00
function limitResult ( $sql , $limit , $offset = false ) {
return " $sql LIMIT $limit " . ( is_numeric ( $offset ) ? " OFFSET { $offset } " : '' );
2004-07-15 15:09:32 +00:00
}
2006-01-07 13:31:29 +00:00
2004-07-18 08:48:43 +00:00
function wasDeadlock () {
2006-12-25 22:54:01 +00:00
return $this -> lastErrno () == '40P01' ;
2004-07-18 08:48:43 +00:00
}
2004-08-10 11:12:18 +00:00
2009-11-06 10:17:44 +00:00
function duplicateTableStructure ( $oldName , $newName , $temporary = false , $fname = 'DatabasePostgres::duplicateTableStructure' ) {
2011-04-12 18:54:51 +00:00
$newName = $this -> addIdentifierQuotes ( $newName );
$oldName = $this -> addIdentifierQuotes ( $oldName );
2009-11-06 10:17:44 +00:00
return $this -> query ( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName (LIKE $oldName INCLUDING DEFAULTS) " , $fname );
}
2011-10-24 19:28:31 +00:00
function listTables ( $prefix = null , $fname = 'DatabasePostgres::listTables' ) {
global $wgDBmwschema ;
$eschema = $this -> addQuotes ( $wgDBmwschema );
$result = $this -> query ( " SELECT tablename FROM pg_tables WHERE schemaname = $eschema " , $fname );
$endArray = array ();
foreach ( $result as $table ) {
$vars = get_object_vars ( $table );
$table = array_pop ( $vars );
if ( ! $prefix || strpos ( $table , $prefix ) === 0 ) {
$endArray [] = $table ;
}
}
return $endArray ;
}
2010-09-05 18:35:34 +00:00
function timestamp ( $ts = 0 ) {
return wfTimestamp ( TS_POSTGRES , $ts );
2004-09-08 20:36:41 +00:00
}
2006-03-07 01:10:39 +00:00
/**
* Return aggregated value function call
*/
2010-09-05 18:35:34 +00:00
function aggregateValue ( $valuedata , $valuename = 'value' ) {
2006-03-07 01:10:39 +00:00
return $valuedata ;
}
2004-09-09 12:04:39 +00:00
2004-09-08 20:36:41 +00:00
/**
* @ return string wikitext of a link to the server software ' s web site
*/
2010-08-22 20:55:07 +00:00
public static function getSoftwareLink () {
2010-09-05 18:35:34 +00:00
return '[http://www.postgresql.org/ PostgreSQL]' ;
2004-09-08 20:36:41 +00:00
}
2006-01-07 13:31:29 +00:00
2004-09-08 20:36:41 +00:00
/**
* @ return string Version information from the database
*/
function getServerVersion () {
2010-07-26 06:57:58 +00:00
if ( ! isset ( $this -> numeric_version ) ) {
2010-07-25 22:09:34 +00:00
$versionInfo = pg_version ( $this -> mConn );
if ( version_compare ( $versionInfo [ 'client' ], '7.4.0' , 'lt' ) ) {
// Old client, abort install
$this -> numeric_version = '7.3 or earlier' ;
} elseif ( isset ( $versionInfo [ 'server' ] ) ) {
// Normal client
$this -> numeric_version = $versionInfo [ 'server' ];
} else {
// Bug 16937: broken pgsql extension from PHP<5.3
$this -> numeric_version = pg_parameter_status ( $this -> mConn , 'server_version' );
}
2009-01-10 00:53:26 +00:00
}
2008-11-22 06:49:16 +00:00
return $this -> numeric_version ;
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 )
2012-02-09 21:33:27 +00:00
* @ return bool
2006-06-27 17:06:36 +00:00
*/
2007-03-19 02:40:32 +00:00
function relationExists ( $table , $types , $schema = false ) {
2006-06-29 17:24:04 +00:00
global $wgDBmwschema ;
2010-09-05 18:35:34 +00:00
if ( ! is_array ( $types ) ) {
2009-02-26 09:10:18 +00:00
$types = array ( $types );
2010-09-05 18:35:34 +00:00
}
if ( ! $schema ) {
2006-06-29 17:24:04 +00:00
$schema = $wgDBmwschema ;
2010-09-05 18:35:34 +00:00
}
2011-09-06 20:51:10 +00:00
$table = $this -> tableName ( $table , 'raw' );
2009-02-26 09:10:18 +00:00
$etable = $this -> addQuotes ( $table );
$eschema = $this -> addQuotes ( $schema );
2006-06-27 17:06:36 +00:00
$SQL = " SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
2007-03-19 02:40:32 +00:00
. " WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
2010-09-05 18:35:34 +00:00
. " AND c.relkind IN (' " . implode ( " ',' " , $types ) . " ') " ;
2006-06-29 17:24:04 +00:00
$res = $this -> query ( $SQL );
2007-07-05 19:42:18 +00:00
$count = $res ? $res -> numRows () : 0 ;
2010-09-27 14:24:13 +00:00
return ( bool ) $count ;
2006-06-28 18:05:08 +00:00
}
2010-09-05 18:35:34 +00:00
/**
2008-04-14 07:45:50 +00:00
* For backward compatibility , this function checks both tables and
2007-03-19 02:40:32 +00:00
* views .
2012-02-09 21:33:27 +00:00
* @ return bool
2007-03-19 02:40:32 +00:00
*/
2011-11-10 20:39:23 +00:00
function tableExists ( $table , $fname = __METHOD__ , $schema = false ) {
2009-02-26 09:10:18 +00:00
return $this -> relationExists ( $table , array ( 'r' , 'v' ), $schema );
2007-03-19 02:40:32 +00:00
}
2008-04-14 07:45:50 +00:00
2009-02-26 09:10:18 +00:00
function sequenceExists ( $sequence , $schema = false ) {
return $this -> relationExists ( $sequence , 'S' , $schema );
2007-03-19 02:40:32 +00:00
}
2009-02-26 09:10:18 +00:00
function triggerExists ( $table , $trigger ) {
2007-04-19 01:35:15 +00:00
global $wgDBmwschema ;
2007-03-19 02:40:32 +00:00
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'
2011-10-11 20:49:28 +00:00
AND tgrelid = pg_class . oid
AND nspname =% s AND relname =% s AND tgname =% s
2009-12-07 08:51:52 +00:00
SQL ;
2010-09-05 18:35:34 +00:00
$res = $this -> query (
sprintf (
$q ,
$this -> addQuotes ( $wgDBmwschema ),
$this -> addQuotes ( $table ),
$this -> addQuotes ( $trigger )
)
);
if ( ! $res ) {
2009-12-11 21:07:27 +00:00
return null ;
2010-09-05 18:35:34 +00:00
}
2007-07-05 19:42:18 +00:00
$rows = $res -> numRows ();
2007-04-19 01:35:15 +00:00
return $rows ;
2007-03-19 02:40:32 +00:00
}
2009-02-26 09:10:18 +00:00
function ruleExists ( $table , $rule ) {
2007-04-19 01:35:15 +00:00
global $wgDBmwschema ;
2010-09-05 18:35:34 +00:00
$exists = $this -> selectField ( 'pg_rules' , 'rulename' ,
array (
'rulename' => $rule ,
'tablename' => $table ,
'schemaname' => $wgDBmwschema
)
);
2007-03-19 02:40:32 +00:00
return $exists === $rule ;
}
2006-06-29 17:24:04 +00:00
2009-02-26 09:10:18 +00:00
function constraintExists ( $table , $constraint ) {
2007-04-19 01:35:15 +00:00
global $wgDBmwschema ;
2010-09-05 18:35:34 +00:00
$SQL = sprintf ( " SELECT 1 FROM information_schema.table_constraints " .
2007-04-19 01:35:15 +00:00
" WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s " ,
2010-09-05 18:35:34 +00:00
$this -> addQuotes ( $wgDBmwschema ),
$this -> addQuotes ( $table ),
$this -> addQuotes ( $constraint )
);
$res = $this -> query ( $SQL );
if ( ! $res ) {
2009-12-11 21:07:27 +00:00
return null ;
2010-09-05 18:35:34 +00:00
}
2007-07-05 19:42:18 +00:00
$rows = $res -> numRows ();
2007-04-19 01:35:15 +00:00
return $rows ;
}
2006-06-28 18:05:08 +00:00
/**
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
* Query whether a given schema exists . Returns true if it does , false if it doesn ' t .
2012-02-09 21:33:27 +00:00
* @ return bool
2006-06-28 18:05:08 +00:00
*/
2006-06-29 17:24:04 +00:00
function schemaExists ( $schema ) {
PostgreSQL install fixes:
* Made PG throw a DBQueryError when it gets a query error, instead of DBUnexpectedError. Apparently this mistake goes back to r14625, when exceptions were first introduced. Did it by removing reportQueryError(), the DatabaseBase version works fine.
* Fixed several places where there was an attempt to check for a query error by checking if the result of query() was false. This never worked. Used try/catch instead.
* Made the DBConnectionError messages go on one line so that they don't mess up the formatting in the installer.
* In DatabasePostgres::selectDB(), only disconnect and reconnect if the DB name is actually changing.
* Made DatabasePostgres::schemaExists() less weird and scary.
* Added DatabasePostgres::roleExists() for use by the installer.
* Removed the PostgreSQL-specific hack to make _InstallUser have a default other than "root". Made _InstallUser into a proper DBMS-specific internal variable instead, since every DBMS we support so far needs a different default.
* Removed the $dbName parameters from openConnection/getConnection, and got rid of $this->useAdmin. Implemented a more sophisticated caching scheme instead. Partial revert of r89389 and r81440.
* When connecting as the install user before DB creation, and when testing the web user's credentials, try a few different database names and use whichever one works.
* Instead of connecting as the web user to create tables, I used SET ROLE. It seems cleaner and more like what the other DBMSes do during installation. "SET ROLE wikiuser" requires the same privileges as "CREATE SCHEMA ... AUTHORIZATION wikiuser", so it's unlikely to break anything.
* In the area of web account creation, fixed various minor logic errors and introduced more informative error messages at the submit stage, pre-install. Show a helpful error message if the web user exists already and the install user can't do the relevant SET ROLE.
* Split schema creation out to a separate install step.
* When creating an account as a non-superuser, add the administrative account to the new account's group. This is necessary to avoid a fatal error during installation (bug 28845).
* Removed code which alters an existing web user to have appropriate search paths and permissions. This may break other apps and is not necessary. As in other DBMSes, If the web user exists, it is the responsibility of the sysadmin to ensure that it has appropriate permissions.
* Rewrote setupPLpgSQL() to use the query builder functions.
2011-06-10 11:32:57 +00:00
$exists = $this -> selectField ( '"pg_catalog"."pg_namespace"' , 1 ,
array ( 'nspname' => $schema ), __METHOD__ );
return ( bool ) $exists ;
}
/**
* Returns true if a given role ( i . e . user ) exists , false otherwise .
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
*/
function roleExists ( $roleName ) {
$exists = $this -> selectField ( '"pg_catalog"."pg_roles"' , 1 ,
array ( 'rolname' => $roleName ), __METHOD__ );
return ( bool ) $exists ;
2006-06-27 17:06:36 +00:00
}
2006-06-28 18:05:08 +00:00
function fieldInfo ( $table , $field ) {
2010-09-05 18:35:34 +00:00
return PostgresField :: fromText ( $this , $table , $field );
2006-06-27 17:06:36 +00:00
}
2010-09-05 18:00:33 +00:00
2008-07-24 12:37:07 +00:00
/**
* pg_field_type () wrapper
2012-02-09 21:33:27 +00:00
* @ return string
2008-07-24 12:37:07 +00:00
*/
function fieldType ( $res , $index ) {
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
return pg_field_type ( $res , $index );
}
2006-06-27 17:06:36 +00:00
2006-06-29 17:24:04 +00:00
/* Not even sure why this is used in the main codebase... */
2009-02-26 09:10:18 +00:00
function limitResultForUpdate ( $sql , $num ) {
2006-06-29 01:55:52 +00:00
return $sql ;
}
2011-05-28 14:52:55 +00:00
/**
* @ param $b
* @ return Blob
*/
2007-06-08 00:57:22 +00:00
function encodeBlob ( $b ) {
2010-09-05 18:35:34 +00:00
return new Blob ( pg_escape_bytea ( $this -> mConn , $b ) );
2006-07-05 03:59:54 +00:00
}
2007-09-23 19:54:56 +00:00
2007-06-08 00:57:22 +00:00
function decodeBlob ( $b ) {
2010-09-05 18:35:34 +00:00
if ( $b instanceof Blob ) {
2007-09-23 19:54:56 +00:00
$b = $b -> fetch ();
}
2006-07-05 03:59:54 +00:00
return pg_unescape_bytea ( $b );
}
2010-09-05 18:35:34 +00:00
function strencode ( $s ) { # Should not be called by us
2010-09-03 15:53:08 +00:00
return pg_escape_string ( $this -> mConn , $s );
2006-07-05 03:59:54 +00:00
}
2011-05-28 14:52:55 +00:00
/**
* @ param $s null | bool | Blob
* @ return int | string
*/
2006-07-05 03:59:54 +00:00
function addQuotes ( $s ) {
if ( is_null ( $s ) ) {
return 'NULL' ;
2010-09-05 18:35:34 +00:00
} elseif ( is_bool ( $s ) ) {
2008-08-21 13:10:19 +00:00
return intval ( $s );
2010-09-05 18:35:34 +00:00
} elseif ( $s instanceof Blob ) {
2011-10-18 21:09:52 +00:00
return " ' " . $s -> fetch ( $s ) . " ' " ;
2006-07-05 03:59:54 +00:00
}
2010-09-03 15:53:08 +00:00
return " ' " . pg_escape_string ( $this -> mConn , $s ) . " ' " ;
2006-07-05 03:59:54 +00:00
}
2008-02-10 15:38:48 +00:00
/**
* Postgres specific version of replaceVars .
* Calls the parent version in Database . php
*
* @ private
*
2008-11-29 18:50:39 +00:00
* @ param $ins String : SQL string , read from a stream ( usually tables . sql )
2008-02-10 15:38:48 +00:00
*
* @ return string SQL string
*/
protected function replaceVars ( $ins ) {
$ins = parent :: replaceVars ( $ins );
2010-09-05 18:35:34 +00:00
if ( $this -> numeric_version >= 8.3 ) {
2008-02-10 15:38:48 +00:00
// Thanks for not providing backwards-compatibility, 8.3
$ins = preg_replace ( " /to_tsvector \ s* \ ( \ s*'default' \ s*,/ " , 'to_tsvector(' , $ins );
}
2010-09-05 18:35:34 +00:00
if ( $this -> numeric_version <= 8.1 ) { // Our minimum version
2008-02-10 15:38:48 +00:00
$ins = str_replace ( 'USING gin' , 'USING gist' , $ins );
}
return $ins ;
}
2006-12-25 22:54:01 +00:00
/**
2007-05-02 17:35:50 +00:00
* Various select options
2006-12-25 22:54:01 +00:00
*
* @ private
*
2008-11-29 18:50:39 +00:00
* @ param $options Array : an associative array of options to be turned into
2006-12-25 22:54:01 +00:00
* an SQL query , valid keys are listed in the function .
* @ return array
*/
function makeSelectOptions ( $options ) {
2007-03-11 15:49:27 +00:00
$preLimitTail = $postLimitTail = '' ;
2007-05-02 17:35:50 +00:00
$startOpts = $useIndex = '' ;
2006-12-25 22:54:01 +00:00
$noKeyOptions = array ();
foreach ( $options as $key => $option ) {
if ( is_numeric ( $key ) ) {
$noKeyOptions [ $option ] = true ;
}
}
2010-09-05 18:35:34 +00:00
if ( isset ( $options [ 'GROUP BY' ] ) ) {
2011-04-29 22:27:34 +00:00
$gb = is_array ( $options [ 'GROUP BY' ] )
? implode ( ',' , $options [ 'GROUP BY' ] )
: $options [ 'GROUP BY' ];
$preLimitTail .= " GROUP BY { $gb } " ;
2010-09-05 18:35:34 +00:00
}
2011-04-29 22:27:34 +00:00
2010-09-05 18:35:34 +00:00
if ( isset ( $options [ 'HAVING' ] ) ) {
$preLimitTail .= " HAVING { $options [ 'HAVING' ] } " ;
}
2011-04-29 22:27:34 +00:00
2010-09-05 18:35:34 +00:00
if ( isset ( $options [ 'ORDER BY' ] ) ) {
2011-04-29 22:27:34 +00:00
$ob = is_array ( $options [ 'ORDER BY' ] )
? implode ( ',' , $options [ 'ORDER BY' ] )
: $options [ 'ORDER BY' ];
$preLimitTail .= " ORDER BY { $ob } " ;
2010-09-05 18:35:34 +00:00
}
2008-04-14 07:45:50 +00:00
2010-09-05 18:35:34 +00:00
//if ( isset( $options['LIMIT'] ) ) {
// $tailOpts .= $this->limitResult( '', $options['LIMIT'],
// isset( $options['OFFSET'] ) ? $options['OFFSET']
// : false );
2007-03-11 03:59:37 +00:00
//}
2006-12-25 22:54:01 +00:00
2010-09-05 18:35:34 +00:00
if ( isset ( $noKeyOptions [ 'FOR UPDATE' ] ) ) {
$postLimitTail .= ' FOR UPDATE' ;
}
if ( isset ( $noKeyOptions [ 'LOCK IN SHARE MODE' ] ) ) {
$postLimitTail .= ' LOCK IN SHARE MODE' ;
}
if ( isset ( $noKeyOptions [ 'DISTINCT' ] ) || isset ( $noKeyOptions [ 'DISTINCTROW' ] ) ) {
$startOpts .= 'DISTINCT' ;
}
2008-04-14 07:45:50 +00:00
2007-03-11 15:49:27 +00:00
return array ( $startOpts , $useIndex , $preLimitTail , $postLimitTail );
2006-12-25 22:54:01 +00:00
}
2008-07-09 18:14:01 +00:00
function setFakeMaster ( $enabled = true ) {}
2008-03-30 09:48:15 +00:00
function getDBname () {
return $this -> mDBname ;
}
function getServer () {
return $this -> mServer ;
}
2007-09-23 22:23:01 +00:00
function buildConcat ( $stringList ) {
return implode ( ' || ' , $stringList );
}
2008-08-18 15:22:00 +00:00
public function getSearchEngine () {
2010-09-05 18:35:34 +00:00
return 'SearchPostgres' ;
2008-08-18 15:22:00 +00:00
}
2012-01-11 20:19:55 +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 ;
}
else {
$this -> delimiter = ';' ;
}
}
return parent :: streamStatementEnd ( $sql , $newLine );
}
2006-12-25 17:25:18 +00:00
} // end DatabasePostgres class