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
2007-03-19 02:40:32 +00:00
class PostgresField {
2010-03-22 18:14:25 +00:00
private $name , $tablename , $type , $nullable , $max_length , $deferred , $deferrable , $conname ;
2007-03-19 02:40:32 +00:00
static function fromText ( $db , $table , $field ) {
2007-03-25 23:53:36 +00:00
global $wgDBmwschema ;
2007-03-19 02:40:32 +00:00
2009-12-07 08:51:52 +00:00
$q = <<< SQL
2010-03-22 18:14:25 +00:00
SELECT
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
$table = $db -> tableName ( $table );
2007-03-19 02:40:32 +00:00
$res = $db -> query ( sprintf ( $q ,
$db -> addQuotes ( $wgDBmwschema ),
$db -> addQuotes ( $table ),
$db -> addQuotes ( $field )));
$row = $db -> fetchObject ( $res );
if ( ! $row )
return null ;
$n = new PostgresField ;
$n -> type = $row -> typname ;
$n -> nullable = ( $row -> attnotnull == 'f' );
$n -> name = $field ;
$n -> tablename = $table ;
$n -> max_length = $row -> attlen ;
2010-03-22 18:14:25 +00:00
$n -> deferrable = ( $row -> deferrable == 't' );
$n -> deferred = ( $row -> deferred == 't' );
$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 ;
}
function nullable () {
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
2006-06-27 16:11:47 +00:00
function DatabasePostgres ( $server = false , $user = false , $password = false , $dbName = false ,
2006-06-27 00:53:46 +00:00
$failFunction = false , $flags = 0 )
2004-06-07 02:59:58 +00:00
{
2006-06-28 18:05:08 +00:00
$this -> mFailFunction = $failFunction ;
$this -> mFlags = $flags ;
$this -> open ( $server , $user , $password , $dbName );
2004-06-07 02:59:58 +00:00
}
2004-07-10 03:09:26 +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 ;
$SQL = " SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = ' " . pg_escape_string ( $name ) . " ' AND n.nspname = ' " . pg_escape_string ( $wgDBmwschema ) . " ' " ;
2007-09-23 22:23:01 +00:00
return $this -> numRows ( $res = $this -> doQuery ( $SQL ));
}
2007-03-19 02:40:32 +00:00
static function newFromParams ( $server , $user , $password , $dbName , $failFunction = false , $flags = 0 )
2004-06-07 02:59:58 +00:00
{
2006-06-27 16:11:47 +00:00
return new DatabasePostgres ( $server , $user , $password , $dbName , $failFunction , $flags );
2004-06-07 02:59:58 +00:00
}
2004-07-10 03:09:26 +00:00
2004-09-02 23:28:24 +00:00
/**
* Usually aborts on failure
* If the failFunction is set to a non - zero integer , returns success
*/
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
}
2006-06-29 17:24:04 +00:00
global $wgDBport ;
2004-09-17 13:47:28 +00:00
2010-02-05 05:40:50 +00:00
if ( ! strlen ( $user )) { ## e.g. the class is being loaded
return ;
2008-11-22 06:39:55 +00:00
}
2004-06-07 02:59:58 +00:00
$this -> close ();
$this -> mServer = $server ;
2007-12-16 16:34:30 +00:00
$this -> mPort = $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 ,
'password' => $password );
2006-06-28 18:05:08 +00:00
if ( $server != false && $server != " " ) {
2008-10-06 00:45:18 +00:00
$connectVars [ 'host' ] = $server ;
2006-06-28 18:05:08 +00:00
}
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 " );
wfDebug ( $this -> lastError () . " \n " );
2008-10-06 00:45:18 +00:00
if ( ! $this -> mFailFunction ) {
throw new DBConnectionError ( $this , $phpError );
} else {
return false ;
}
2006-06-28 18:05:08 +00:00
}
$this -> mOpened = true ;
2006-08-18 16:24:48 +00:00
global $wgCommandLineMode ;
## 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
}
2009-02-23 21:23:14 +00:00
$this -> doQuery ( " SET client_encoding='UTF8' " );
2007-09-19 02:31:28 +00:00
global $wgDBmwschema , $wgDBts2schema ;
if ( isset ( $wgDBmwschema ) && isset ( $wgDBts2schema )
&& $wgDBmwschema !== 'mediawiki'
&& preg_match ( '/^\w+$/' , $wgDBmwschema )
&& preg_match ( '/^\w+$/' , $wgDBts2schema )
) {
2007-12-06 17:55:50 +00:00
$safeschema = $this -> quote_ident ( $wgDBmwschema );
$safeschema2 = $this -> quote_ident ( $wgDBts2schema );
2009-02-23 21:23:14 +00:00
$this -> doQuery ( " SET search_path = $safeschema , $wgDBts2schema , 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
2008-10-06 00:45:18 +00:00
function makeConnectionString ( $vars ) {
$s = '' ;
foreach ( $vars as $name => $value ) {
$s .= " $name =' " . str_replace ( " ' " , " \\ ' " , $value ) . " ' " ;
}
return $s ;
}
2007-12-16 16:34:30 +00:00
2010-07-25 21:02:42 +00:00
function initial_setup ( $superuser , $password , $dbName ) {
2008-02-10 15:38:48 +00:00
// If this is the initial connection, setup the schema stuff and possibly create the user
2010-07-26 20:55:44 +00:00
global $wgDBname , $wgDBuser , $wgDBpassword , $wgDBmwschema , $wgDBts2schema ;
2008-02-10 15:38:48 +00:00
print " <li>Checking the version of Postgres... " ;
2008-11-22 06:49:16 +00:00
$version = $this -> getServerVersion ();
2008-02-10 15:38:48 +00:00
$PGMINVER = '8.1' ;
2008-10-06 00:45:18 +00:00
if ( $version < $PGMINVER ) {
2009-02-06 03:46:35 +00:00
print " <b>FAILED</b>. Required version is $PGMINVER . You have " . htmlspecialchars ( $version ) . " </li> \n " ;
2008-02-10 15:38:48 +00:00
dieout ( " </ul> " );
}
2009-02-06 03:46:35 +00:00
print " version " . htmlspecialchars ( $this -> numeric_version ) . " is OK.</li> \n " ;
2008-02-10 15:38:48 +00:00
$safeuser = $this -> quote_ident ( $wgDBuser );
// Are we connecting as a superuser for the first time?
2010-07-25 21:02:42 +00:00
if ( $superuser ) {
2008-02-10 15:38:48 +00:00
// Are we really a superuser? Check out our rights
$SQL = " SELECT
CASE WHEN usesuper IS TRUE THEN
CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
END AS rights
2010-07-25 21:02:42 +00:00
FROM pg_catalog . pg_user WHERE usename = " . $this->addQuotes ( $superuser );
2008-02-10 15:38:48 +00:00
$rows = $this -> numRows ( $res = $this -> doQuery ( $SQL ));
if ( ! $rows ) {
2010-07-25 21:02:42 +00:00
print " <li>ERROR: Could not read permissions for user \" " . htmlspecialchars ( $superuser ) . " \" </li> \n " ;
2008-02-10 15:38:48 +00:00
dieout ( '</ul>' );
}
$perms = pg_fetch_result ( $res , 0 , 0 );
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
$SQL = " SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this -> addQuotes ( $wgDBuser );
$rows = $this -> numRows ( $this -> doQuery ( $SQL ));
if ( $rows ) {
2009-02-06 03:46:35 +00:00
print " <li>User \" " . htmlspecialchars ( $wgDBuser ) . " \" already exists, skipping account creation.</li> " ;
2008-02-10 15:38:48 +00:00
}
else {
if ( $perms != 1 and $perms != 3 ) {
2010-07-25 21:02:42 +00:00
print " <li>ERROR: the user \" " . htmlspecialchars ( $superuser ) . " \" cannot create other users. " ;
2008-02-10 15:38:48 +00:00
print 'Please use a different Postgres user.</li>' ;
dieout ( '</ul>' );
}
2009-02-06 03:46:35 +00:00
print " <li>Creating user <b> " . htmlspecialchars ( $wgDBuser ) . " </b>... " ;
2008-02-10 15:38:48 +00:00
$safepass = $this -> addQuotes ( $wgDBpassword );
$SQL = " CREATE USER $safeuser NOCREATEDB PASSWORD $safepass " ;
$this -> doQuery ( $SQL );
print " OK</li> \n " ;
}
// User now exists, check out the database
if ( $dbName != $wgDBname ) {
$SQL = " SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this -> addQuotes ( $wgDBname );
$rows = $this -> numRows ( $this -> doQuery ( $SQL ));
if ( $rows ) {
2009-02-06 03:46:35 +00:00
print " <li>Database \" " . htmlspecialchars ( $wgDBname ) . " \" already exists, skipping database creation.</li> " ;
2008-02-10 15:38:48 +00:00
}
else {
2009-03-24 13:37:50 +00:00
if ( $perms < 1 ) {
2010-07-25 21:02:42 +00:00
print " <li>ERROR: the user \" " . htmlspecialchars ( $superuser ) . " \" cannot create databases. " ;
2008-02-10 15:38:48 +00:00
print 'Please use a different Postgres user.</li>' ;
dieout ( '</ul>' );
}
2009-02-06 03:46:35 +00:00
print " <li>Creating database <b> " . htmlspecialchars ( $wgDBname ) . " </b>... " ;
2008-02-10 15:38:48 +00:00
$safename = $this -> quote_ident ( $wgDBname );
$SQL = " CREATE DATABASE $safename OWNER $safeuser " ;
$this -> doQuery ( $SQL );
print " OK</li> \n " ;
// Hopefully tsearch2 and plpgsql are in template1...
}
// Reconnect to check out tsearch2 rights for this user
2009-02-06 03:46:35 +00:00
print " <li>Connecting to \" " . htmlspecialchars ( $wgDBname ) . " \" as superuser \" " .
2010-07-25 21:02:42 +00:00
htmlspecialchars ( $superuser ) . " \" to check rights... " ;
2008-04-14 07:45:50 +00:00
2009-02-06 03:46:35 +00:00
$connectVars = array ();
2008-02-10 15:38:48 +00:00
if ( $this -> mServer != false && $this -> mServer != " " ) {
2009-02-06 03:46:35 +00:00
$connectVars [ 'host' ] = $this -> mServer ;
2008-02-10 15:38:48 +00:00
}
if ( $this -> mPort != false && $this -> mPort != " " ) {
2009-02-06 03:46:35 +00:00
$connectVars [ 'port' ] = $this -> mPort ;
2008-02-10 15:38:48 +00:00
}
2009-02-06 03:46:35 +00:00
$connectVars [ 'dbname' ] = $wgDBname ;
2010-07-25 21:02:42 +00:00
$connectVars [ 'user' ] = $superuser ;
2009-02-06 03:46:35 +00:00
$connectVars [ 'password' ] = $password ;
2008-02-10 15:38:48 +00:00
2009-02-06 03:46:35 +00:00
@ $this -> mConn = pg_connect ( $this -> makeConnectionString ( $connectVars ) );
2010-06-29 09:20:09 +00:00
if ( ! $this -> mConn ) {
2008-02-10 15:38:48 +00:00
print " <b>FAILED TO CONNECT!</b></li> " ;
dieout ( " </ul> " );
}
print " OK</li> \n " ;
}
if ( $this -> numeric_version < 8.3 ) {
// Tsearch2 checks
2009-02-06 03:46:35 +00:00
print " <li>Checking that tsearch2 is installed in the database \" " .
htmlspecialchars ( $wgDBname ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
if ( ! $this -> tableExists ( " pg_ts_cfg " , $wgDBts2schema )) {
2009-02-06 03:46:35 +00:00
print " <b>FAILED</b>. tsearch2 must be installed in the database \" " .
htmlspecialchars ( $wgDBname ) . " \" . " ;
2008-02-10 15:38:48 +00:00
print " Please see <a href='http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a> " ;
print " for instructions or ask on #postgresql on irc.freenode.net</li> \n " ;
dieout ( " </ul> " );
2008-04-14 07:45:50 +00:00
}
2008-02-10 15:38:48 +00:00
print " OK</li> \n " ;
2009-02-06 03:46:35 +00:00
print " <li>Ensuring that user \" " . htmlspecialchars ( $wgDBuser ) .
" \" has select rights on the tsearch2 tables... " ;
2008-02-10 15:38:48 +00:00
foreach ( array ( 'cfg' , 'cfgmap' , 'dict' , 'parser' ) as $table ) {
$SQL = " GRANT SELECT ON pg_ts_ $table TO $safeuser " ;
$this -> doQuery ( $SQL );
}
print " OK</li> \n " ;
}
// Setup the schema for this user if needed
$result = $this -> schemaExists ( $wgDBmwschema );
$safeschema = $this -> quote_ident ( $wgDBmwschema );
if ( ! $result ) {
2009-02-06 03:46:35 +00:00
print " <li>Creating schema <b> " . htmlspecialchars ( $wgDBmwschema ) . " </b> ... " ;
2008-02-10 15:38:48 +00:00
$result = $this -> doQuery ( " CREATE SCHEMA $safeschema AUTHORIZATION $safeuser " );
if ( ! $result ) {
print " <b>FAILED</b>.</li> \n " ;
dieout ( " </ul> " );
}
print " OK</li> \n " ;
}
else {
print " <li>Schema already exists, explicitly granting rights... \n " ;
$safeschema2 = $this -> addQuotes ( $wgDBmwschema );
$SQL = " SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser ;' \n " .
" FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n \n " .
" WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n " .
" AND p.relkind IN ('r','S','v') \n " ;
$SQL .= " UNION \n " ;
$SQL .= " SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('|| \n " .
" pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser ;' \n " .
" FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n \n " .
" WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2 " ;
$res = $this -> doQuery ( $SQL );
if ( ! $res ) {
print " <b>FAILED</b>. Could not set rights for the user.</li> \n " ;
dieout ( " </ul> " );
}
$this -> doQuery ( " SET search_path = $safeschema " );
$rows = $this -> numRows ( $res );
while ( $rows ) {
$rows -- ;
$this -> doQuery ( pg_fetch_result ( $res , $rows , 0 ));
}
print " OK</li> " ;
}
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
// Install plpgsql if needed
$this -> setup_plpgsql ();
2010-07-25 21:02:42 +00:00
$superuser = '' ;
2008-02-10 15:38:48 +00:00
return true ; // Reconnect as regular user
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
} // end superuser
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
if ( ! defined ( 'POSTGRES_SEARCHPATH' )) {
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
if ( $this -> numeric_version < 8.3 ) {
// Do we have the basic tsearch2 table?
2009-02-06 03:46:35 +00:00
print " <li>Checking for tsearch2 in the schema \" " . htmlspecialchars ( $wgDBts2schema ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
if ( ! $this -> tableExists ( " pg_ts_dict " , $wgDBts2schema )) {
print " <b>FAILED</b>. Make sure tsearch2 is installed. See <a href= " ;
print " 'http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a> " ;
print " for instructions.</li> \n " ;
dieout ( " </ul> " );
}
print " OK</li> \n " ;
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
// Does this user have the rights to the tsearch2 tables?
$ctype = pg_fetch_result ( $this -> doQuery ( " SHOW lc_ctype " ), 0 , 0 );
print " <li>Checking tsearch2 permissions... " ;
// Let's check all four, just to be safe
error_reporting ( 0 );
$ts2tables = array ( 'cfg' , 'cfgmap' , 'dict' , 'parser' );
$safetsschema = $this -> quote_ident ( $wgDBts2schema );
foreach ( $ts2tables AS $tname ) {
$SQL = " SELECT count(*) FROM $safetsschema .pg_ts_ $tname " ;
$res = $this -> doQuery ( $SQL );
if ( ! $res ) {
2009-02-06 03:46:35 +00:00
print " <b>FAILED</b> to access " . htmlspecialchars ( " pg_ts_ $tname " ) .
" . Make sure that the user \" " . htmlspecialchars ( $wgDBuser ) .
" \" has SELECT access to all four tsearch2 tables</li> \n " ;
2008-02-10 15:38:48 +00:00
dieout ( " </ul> " );
}
}
2009-02-06 03:46:35 +00:00
$SQL = " SELECT ts_name FROM $safetsschema .pg_ts_cfg WHERE locale = " . $this -> addQuotes ( $ctype ) ;
2008-02-10 15:38:48 +00:00
$SQL .= " ORDER BY CASE WHEN ts_name <> 'default' THEN 1 ELSE 0 END " ;
$res = $this -> doQuery ( $SQL );
error_reporting ( E_ALL );
if ( ! $res ) {
print " <b>FAILED</b>. Could not determine the tsearch2 locale information</li> \n " ;
dieout ( " </ul> " );
}
print " OK</li> " ;
// Will the current locale work? Can we force it to?
2009-02-06 03:46:35 +00:00
print " <li>Verifying tsearch2 locale with " . htmlspecialchars ( $ctype ) . " ... " ;
2008-02-10 15:38:48 +00:00
$rows = $this -> numRows ( $res );
$resetlocale = 0 ;
if ( ! $rows ) {
print " <b>not found</b></li> \n " ;
2009-02-06 03:46:35 +00:00
print " <li>Attempting to set default tsearch2 locale to \" " . htmlspecialchars ( $ctype ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
$resetlocale = 1 ;
}
else {
$tsname = pg_fetch_result ( $res , 0 , 0 );
if ( $tsname != 'default' ) {
2009-02-06 03:46:35 +00:00
print " <b>not set to default ( " . htmlspecialchars ( $tsname ) . " )</b> " ;
print " <li>Attempting to change tsearch2 default locale to \" " .
htmlspecialchars ( $ctype ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
$resetlocale = 1 ;
}
}
if ( $resetlocale ) {
2009-02-06 03:46:35 +00:00
$SQL = " UPDATE $safetsschema .pg_ts_cfg SET locale = " . $this -> addQuotes ( $ctype ) . " WHERE ts_name = 'default' " ;
2008-02-10 15:38:48 +00:00
$res = $this -> doQuery ( $SQL );
if ( ! $res ) {
print " <b>FAILED</b>. " ;
2009-02-06 03:46:35 +00:00
print " Please make sure that the locale in pg_ts_cfg for \" default \" is set to \" " .
htmlspecialchars ( $ctype ) . " \" </li> \n " ;
2008-02-10 15:38:48 +00:00
dieout ( " </ul> " );
}
print " OK</li> " ;
}
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
// Final test: try out a simple tsearch2 query
$SQL = " SELECT $safetsschema .to_tsvector('default','MediaWiki tsearch2 testing') " ;
$res = $this -> doQuery ( $SQL );
if ( ! $res ) {
2009-02-06 03:46:35 +00:00
print " <b>FAILED</b>. Specifically, \" " . htmlspecialchars ( $SQL ) . " \" did not work.</li> " ;
2008-02-10 15:38:48 +00:00
dieout ( " </ul> " );
}
print " OK</li> " ;
}
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
// Install plpgsql if needed
$this -> setup_plpgsql ();
// Does the schema already exist? Who owns it?
$result = $this -> schemaExists ( $wgDBmwschema );
if ( ! $result ) {
2009-02-06 03:46:35 +00:00
print " <li>Creating schema <b> " . htmlspecialchars ( $wgDBmwschema ) . " </b> ... " ;
2008-02-10 15:38:48 +00:00
error_reporting ( 0 );
$safeschema = $this -> quote_ident ( $wgDBmwschema );
$result = $this -> doQuery ( " CREATE SCHEMA $safeschema " );
error_reporting ( E_ALL );
if ( ! $result ) {
2009-02-06 03:46:35 +00:00
print " <b>FAILED</b>. The user \" " . htmlspecialchars ( $wgDBuser ) .
" \" must be able to access the schema. " .
2008-02-10 15:38:48 +00:00
" You can try making them the owner of the database, or try creating the schema with a " .
2009-02-06 03:46:35 +00:00
" different user, and then grant access to the \" " .
htmlspecialchars ( $wgDBuser ) . " \" user.</li> \n " ;
2008-02-10 15:38:48 +00:00
dieout ( " </ul> " );
}
print " OK</li> \n " ;
}
else if ( $result != $wgDBuser ) {
2009-02-06 03:46:35 +00:00
print " <li>Schema \" " . htmlspecialchars ( $wgDBmwschema ) . " \" exists but is not owned by \" " .
htmlspecialchars ( $wgDBuser ) . " \" . Not ideal.</li> \n " ;
2008-02-10 15:38:48 +00:00
}
else {
2009-02-06 03:46:35 +00:00
print " <li>Schema \" " . htmlspecialchars ( $wgDBmwschema ) . " \" exists and is owned by \" " .
htmlspecialchars ( $wgDBuser ) . " \" . Excellent.</li> \n " ;
2008-02-10 15:38:48 +00:00
}
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
// Always return GMT time to accomodate the existing integer-based timestamp assumption
2009-02-06 03:46:35 +00:00
print " <li>Setting the timezone to GMT for user \" " . htmlspecialchars ( $wgDBuser ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
$SQL = " ALTER USER $safeuser SET timezone = 'GMT' " ;
$result = pg_query ( $this -> mConn , $SQL );
if ( ! $result ) {
print " <b>FAILED</b>.</li> \n " ;
dieout ( " </ul> " );
}
print " OK</li> \n " ;
// Set for the rest of this session
$SQL = " SET timezone = 'GMT' " ;
$result = pg_query ( $this -> mConn , $SQL );
if ( ! $result ) {
print " <li>Failed to set timezone</li> \n " ;
dieout ( " </ul> " );
}
2009-02-06 03:46:35 +00:00
print " <li>Setting the datestyle to ISO, YMD for user \" " . htmlspecialchars ( $wgDBuser ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
$SQL = " ALTER USER $safeuser SET datestyle = 'ISO, YMD' " ;
$result = pg_query ( $this -> mConn , $SQL );
if ( ! $result ) {
print " <b>FAILED</b>.</li> \n " ;
dieout ( " </ul> " );
}
print " OK</li> \n " ;
// Set for the rest of this session
$SQL = " SET datestyle = 'ISO, YMD' " ;
$result = pg_query ( $this -> mConn , $SQL );
if ( ! $result ) {
print " <li>Failed to set datestyle</li> \n " ;
dieout ( " </ul> " );
}
2008-04-14 07:45:50 +00:00
2008-02-10 15:38:48 +00:00
// Fix up the search paths if needed
2009-02-06 03:46:35 +00:00
print " <li>Setting the search path for user \" " . htmlspecialchars ( $wgDBuser ) . " \" ... " ;
2008-02-10 15:38:48 +00:00
$path = $this -> quote_ident ( $wgDBmwschema );
if ( $wgDBts2schema !== $wgDBmwschema )
$path .= " , " . $this -> quote_ident ( $wgDBts2schema );
if ( $wgDBmwschema !== 'public' and $wgDBts2schema !== 'public' )
$path .= " , public " ;
$SQL = " ALTER USER $safeuser SET search_path = $path " ;
$result = pg_query ( $this -> mConn , $SQL );
if ( ! $result ) {
print " <b>FAILED</b>.</li> \n " ;
dieout ( " </ul> " );
}
print " OK</li> \n " ;
// Set for the rest of this session
$SQL = " SET search_path = $path " ;
$result = pg_query ( $this -> mConn , $SQL );
if ( ! $result ) {
print " <li>Failed to set search_path</li> \n " ;
dieout ( " </ul> " );
}
define ( " POSTGRES_SEARCHPATH " , $path );
}
}
2007-12-16 16:34:30 +00:00
2008-02-04 16:40:35 +00:00
function setup_plpgsql () {
2010-08-04 11:58:35 +00:00
print " <li>Checking for PL/pgSQL ... " ;
2008-02-04 16:40:35 +00:00
$SQL = " SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql' " ;
$rows = $this -> numRows ( $this -> doQuery ( $SQL ));
if ( $rows < 1 ) {
// plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
2010-08-04 11:58:35 +00:00
print " not installed. Attempting to install PL/pgSQL ... " ;
2008-02-04 16:40:35 +00:00
$SQL = " SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) " .
" WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog' " ;
$rows = $this -> numRows ( $this -> doQuery ( $SQL ));
2010-07-23 20:58:09 +00:00
global $wgDBname ;
2008-02-04 16:40:35 +00:00
if ( $rows >= 1 ) {
2010-07-23 20:58:09 +00:00
$olde = error_reporting ( 0 );
2008-02-04 16:40:35 +00:00
error_reporting ( $olde - E_WARNING );
$result = $this -> doQuery ( " CREATE LANGUAGE plpgsql " );
error_reporting ( $olde );
if ( ! $result ) {
2010-08-04 11:58:35 +00:00
print " <b>FAILED</b>. You need to install the language PL/pgSQL in the database <tt> " .
2009-02-06 03:46:35 +00:00
htmlspecialchars ( $wgDBname ) . " </tt></li> " ;
2008-02-04 16:40:35 +00:00
dieout ( " </ul> " );
}
}
else {
2010-08-04 11:58:35 +00:00
print " <b>FAILED</b>. You need to install the language PL/pgSQL in the database <tt> " .
2009-02-06 03:46:35 +00:00
htmlspecialchars ( $wgDBname ) . " </tt></li> " ;
2008-02-04 16:40:35 +00:00
dieout ( " </ul> " );
}
}
print " OK</li> \n " ;
}
2004-09-02 23:28:24 +00:00
/**
* Closes a database connection , if it is open
* Returns success , true if already closed
*/
function close () {
2004-06-07 02:59:58 +00:00
$this -> mOpened = false ;
if ( $this -> mConn ) {
return pg_close ( $this -> mConn );
} else {
return true ;
}
}
2006-01-07 13:31:29 +00:00
2004-07-24 07:24:04 +00:00
function doQuery ( $sql ) {
2007-11-05 15:21:59 +00:00
if ( function_exists ( 'mb_convert_encoding' )) {
2008-09-06 07:05:14 +00:00
$sql = mb_convert_encoding ( $sql , 'UTF-8' );
2007-11-05 15:21:59 +00:00
}
2008-09-06 07:05:14 +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
2004-08-22 17:24:50 +00:00
function queryIgnore ( $sql , $fname = '' ) {
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 ;
}
2004-06-07 02:59:58 +00:00
if ( !@ pg_free_result ( $res ) ) {
2006-08-13 14:35:51 +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 ;
}
2004-06-07 02:59:58 +00:00
@ $row = pg_fetch_object ( $res );
# FIXME: HACK HACK HACK HACK debug
2006-01-07 13:31:29 +00:00
2004-06-07 02:59:58 +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.
2004-06-10 06:37:12 +00:00
if ( pg_last_error ( $this -> mConn ) ) {
2006-06-06 23:07:26 +00:00
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 ;
}
2004-06-10 13:02:27 +00:00
@ $row = pg_fetch_array ( $res );
2006-03-06 04:20:20 +00:00
if ( pg_last_error ( $this -> mConn ) ) {
2006-06-06 23:07:26 +00:00
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 ;
}
2006-01-07 13:09:30 +00:00
@ $n = pg_num_rows ( $res );
2004-06-10 06:37:12 +00:00
if ( pg_last_error ( $this -> mConn ) ) {
2006-06-06 23:07:26 +00:00
throw new DBUnexpectedError ( $this , 'SQL error: ' . htmlspecialchars ( pg_last_error ( $this -> mConn ) ) );
2004-06-07 02:59:58 +00:00
}
return $n ;
}
2007-07-05 19:42:18 +00:00
function numFields ( $res ) {
if ( $res instanceof ResultWrapper ) {
$res = $res -> result ;
}
return pg_num_fields ( $res );
}
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
*/
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 ();
}
else {
return " No database connection " ;
}
}
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 ;
}
if ( empty ( $this -> mLastResult ) )
2007-05-15 02:59:20 +00:00
return 0 ;
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 ()
*/
2008-04-14 07:45:50 +00:00
2009-02-26 09:10:18 +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
*/
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
}
while ( $row = $this -> fetchObject ( $res ) ) {
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
}
2009-02-26 09:10:18 +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 } ' " .
2009-01-15 14:20:28 +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 );
if ( ! $res )
2009-12-11 21:07:27 +00:00
return null ;
2006-01-07 13:09:30 +00:00
while ( $row = $this -> fetchObject ( $res ))
2004-09-09 07:12:11 +00:00
return true ;
return false ;
2006-01-07 13:31:29 +00:00
2004-09-09 07:12:11 +00:00
}
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
2007-05-15 12:14:20 +00:00
if ( ! is_array ( $options ) )
$options = array ( $options );
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 ] );
2007-05-15 02:59:20 +00:00
}
else {
$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 ) {
if ( ! $this -> mTrxLevel ) {
$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 );
}
else {
$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 ) {
pg_query ( $this -> mConn , " SAVEPOINT $ignore " );
}
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 ();
if ( $bar != false ) {
pg_query ( $this -> mConn , " ROLLBACK TO $ignore " );
}
else {
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
2007-05-15 02:59:20 +00:00
if ( ! $tempres )
$res = false ;
}
}
2004-09-01 12:27:57 +00:00
}
2007-05-15 02:59:20 +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 ();
if ( $bar != false ) {
pg_query ( $this -> mConn , " ROLLBACK TO $ignore " );
}
else {
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 );
2008-07-04 15:57:44 +00:00
if ( $didbegin ) {
$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
2008-07-05 17:00:01 +00:00
2007-05-15 02:59:20 +00:00
return $res ;
2004-09-01 12:27:57 +00:00
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 .
* @ todo FIXME : implement this a little better ( seperate select / insert ) ?
*/
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 ) ) {
$insertOptions = implode ( ' ' , $insertOptions );
}
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 " ;
if ( $conds != '*' ) {
$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 ;
}
2004-07-10 03:09:26 +00:00
function tableName ( $name ) {
2006-07-23 02:04:40 +00:00
# Replace reserved words with better ones
2004-07-10 03:09:26 +00:00
switch ( $name ) {
case 'user' :
2006-07-23 02:04:40 +00:00
return 'mwuser' ;
case 'text' :
return 'pagecontent' ;
2004-07-10 03:09:26 +00:00
default :
return $name ;
}
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 ()
*/
2004-07-10 03:09:26 +00:00
function nextSequenceValue ( $seqName ) {
2006-07-05 03:59:54 +00:00
$safeseq = preg_replace ( " /'/ " , " '' " , $seqName );
$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 .
2007-12-18 15:44:18 +00:00
*/
function currentSequenceValue ( $seqName ) {
$safeseq = preg_replace ( " /'/ " , " '' " , $seqName );
$res = $this -> query ( " SELECT currval(' $safeseq ') " );
$row = $this -> fetchRow ( $res );
$currval = $row [ 0 ];
return $currval ;
}
2004-07-10 03:09:26 +00:00
# REPLACE query wrapper
2006-08-13 14:35:51 +00:00
# Postgres simulates this with a DELETE followed by INSERT
2004-07-10 03:09:26 +00:00
# $row is the row to insert, an associative array
2006-01-07 13:09:30 +00:00
# $uniqueIndexes is an array of indexes. Each element may be either a
2004-07-10 03:09:26 +00:00
# field name or an array of field names
#
2006-01-07 13:09:30 +00:00
# It may be more efficient to leave off unique indexes which are unlikely to collide.
# However if you do this, you run the risk of encountering errors which wouldn't have
2004-07-10 03:09:26 +00:00
# occurred in MySQL
2009-02-26 09:10:18 +00:00
function replace ( $table , $uniqueIndexes , $rows , $fname = 'DatabasePostgres::replace' ) {
2004-07-10 03:09:26 +00:00
$table = $this -> tableName ( $table );
2006-01-07 13:31:29 +00:00
2004-09-06 09:56:29 +00:00
if ( count ( $rows ) == 0 ) {
return ;
}
2004-07-10 03:09:26 +00:00
2004-07-18 08:48:43 +00:00
# Single row case
if ( ! is_array ( reset ( $rows ) ) ) {
$rows = array ( $rows );
}
foreach ( $rows as $row ) {
# Delete rows which collide
if ( $uniqueIndexes ) {
2004-09-06 08:30:42 +00:00
$sql = " DELETE FROM $table WHERE " ;
2004-07-18 08:48:43 +00:00
$first = true ;
foreach ( $uniqueIndexes as $index ) {
if ( $first ) {
$first = false ;
2004-09-07 08:37:50 +00:00
$sql .= " ( " ;
2004-07-18 08:48:43 +00:00
} else {
2004-08-22 17:24:50 +00:00
$sql .= ') OR (' ;
2004-07-15 15:09:32 +00:00
}
2004-07-18 08:48:43 +00:00
if ( is_array ( $index ) ) {
$first2 = true ;
foreach ( $index as $col ) {
2006-01-07 13:09:30 +00:00
if ( $first2 ) {
2004-07-18 08:48:43 +00:00
$first2 = false ;
} else {
2004-08-22 17:24:50 +00:00
$sql .= ' AND ' ;
2004-07-18 08:48:43 +00:00
}
2004-08-22 17:24:50 +00:00
$sql .= $col . '=' . $this -> addQuotes ( $row [ $col ] );
2004-07-18 08:48:43 +00:00
}
2004-09-07 08:37:50 +00:00
} else {
2004-08-22 17:24:50 +00:00
$sql .= $index . '=' . $this -> addQuotes ( $row [ $index ] );
2004-09-07 08:37:50 +00:00
}
2004-07-10 03:09:26 +00:00
}
2004-08-22 17:24:50 +00:00
$sql .= ')' ;
2004-07-18 08:48:43 +00:00
$this -> query ( $sql , $fname );
2004-07-10 03:09:26 +00:00
}
2004-07-18 08:48:43 +00:00
# Now insert the row
2004-09-07 08:37:50 +00:00
$sql = " INSERT INTO $table ( " . $this -> makeList ( array_keys ( $row ), LIST_NAMES ) . ') VALUES (' .
2004-07-18 08:48:43 +00:00
$this -> makeList ( $row , LIST_COMMA ) . ')' ;
$this -> query ( $sql , $fname );
2004-07-10 03:09:26 +00:00
}
}
# DELETE where the condition is a join
2009-02-26 09:10:18 +00:00
function deleteJoin ( $delTable , $joinTable , $delVar , $joinVar , $conds , $fname = 'DatabasePostgres::deleteJoin' ) {
2004-07-10 03:09:26 +00:00
if ( ! $conds ) {
2006-06-06 23:07:26 +00:00
throw new DBUnexpectedError ( $this , 'Database::deleteJoin() called with empty $conds' );
2004-06-07 02:59:58 +00:00
}
2004-07-10 03:09:26 +00:00
$delTable = $this -> tableName ( $delTable );
$joinTable = $this -> tableName ( $joinTable );
$sql = " DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable " ;
if ( $conds != '*' ) {
2004-08-22 17:24:50 +00:00
$sql .= 'WHERE ' . $this -> makeList ( $conds , LIST_AND );
2004-06-07 02:59:58 +00:00
}
2004-08-22 17:24:50 +00:00
$sql .= ')' ;
2004-07-10 03:09:26 +00:00
$this -> query ( $sql , $fname );
2004-06-07 02:59:58 +00:00
}
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' " ;
$res = $this -> query ( $sql );
$row = $this -> fetchObject ( $res );
if ( $row -> ftype == " varchar " ) {
2006-01-07 13:31:29 +00:00
$size = $row -> size - 4 ;
2004-09-11 14:08:52 +00:00
} else {
$size = $row -> size ;
}
2004-07-10 03:09:26 +00:00
return $size ;
}
2006-01-07 13:31:29 +00:00
2008-05-02 15:28:10 +00:00
function limitResult ( $sql , $limit , $offset = false ) {
2006-03-07 01:10:39 +00:00
return " $sql LIMIT $limit " . ( is_numeric ( $offset ) ? " OFFSET { $offset } " : " " );
2004-07-15 15:09:32 +00:00
}
2006-01-07 13:31:29 +00:00
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' ) {
return $this -> query ( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName (LIKE $oldName INCLUDING DEFAULTS) " , $fname );
}
2004-09-08 20:36:41 +00:00
function timestamp ( $ts = 0 ) {
2006-07-18 01:40:38 +00:00
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
*/
function aggregateValue ( $valuedata , $valuename = 'value' ) {
return $valuedata ;
}
2004-09-09 12:04:39 +00:00
2004-09-08 20:36:41 +00:00
function reportQueryError ( $error , $errno , $sql , $fname , $tempIgnore = false ) {
2008-02-10 15:38:48 +00:00
// Ignore errors during error handling to avoid infinite recursion
2006-11-30 01:00:08 +00:00
$ignore = $this -> ignoreErrors ( true );
2008-02-10 15:38:48 +00:00
$this -> mErrorCount ++ ;
2006-11-30 01:00:08 +00:00
if ( $ignore || $tempIgnore ) {
wfDebug ( " SQL ERROR (ignored): $error\n " );
$this -> ignoreErrors ( $ignore );
}
else {
$message = " A database error has occurred \n " .
" Query: $sql\n " .
" Function: $fname\n " .
" Error: $errno $error\n " ;
throw new DBUnexpectedError ( $this , $message );
}
2004-08-19 13:00:34 +00:00
}
2004-09-08 20:36:41 +00:00
/**
* @ return string wikitext of a link to the server software ' s web site
*/
2008-09-22 03:15:17 +00:00
function getSoftwareLink () {
2004-09-08 20:36:41 +00:00
return " [http://www.postgresql.org/ PostgreSQL] " ;
}
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 )
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 ;
2009-02-26 09:10:18 +00:00
if ( ! is_array ( $types ) )
$types = array ( $types );
if ( ! $schema )
2006-06-29 17:24:04 +00:00
$schema = $wgDBmwschema ;
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 "
. " 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 ;
2007-06-07 14:02:46 +00:00
return $count ? true : false ;
2006-06-28 18:05:08 +00:00
}
2007-03-19 02:40:32 +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 .
*/
2009-02-26 09:10:18 +00:00
function tableExists ( $table , $schema = false ) {
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'
AND tgrelid = pg_class . oid
AND nspname =% s AND relname =% s AND tgname =% s
2009-12-07 08:51:52 +00:00
SQL ;
2007-03-19 02:40:32 +00:00
$res = $this -> query ( sprintf ( $q ,
$this -> addQuotes ( $wgDBmwschema ),
$this -> addQuotes ( $table ),
$this -> addQuotes ( $trigger )));
2007-04-19 01:35:15 +00:00
if ( ! $res )
2009-12-11 21:07:27 +00:00
return null ;
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 ;
2007-03-19 02:40:32 +00:00
$exists = $this -> selectField ( " pg_rules " , " rulename " ,
array ( " rulename " => $rule ,
" tablename " => $table ,
2009-02-26 09:10:18 +00:00
" 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 ;
$SQL = sprintf ( " SELECT 1 FROM information_schema.table_constraints " .
" WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s " ,
2008-04-14 07:45:50 +00:00
$this -> addQuotes ( $wgDBmwschema ),
$this -> addQuotes ( $table ),
2007-04-19 01:35:15 +00:00
$this -> addQuotes ( $constraint ));
$res = $this -> query ( $SQL );
if ( ! $res )
2009-12-11 21:07:27 +00:00
return null ;
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
/**
* Query whether a given schema exists . Returns the name of the owner
*/
2006-06-29 17:24:04 +00:00
function schemaExists ( $schema ) {
$eschema = preg_replace ( " /'/ " , " '' " , $schema );
2006-06-28 18:05:08 +00:00
$SQL = " SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
2006-06-29 17:24:04 +00:00
. " WHERE n.nspowner=r.oid AND n.nspname = ' $eschema ' " ;
$res = $this -> query ( $SQL );
2007-07-05 19:42:18 +00:00
if ( $res && $res -> numRows () ) {
2007-07-16 21:28:01 +00:00
$row = $res -> fetchObject ();
2007-07-05 19:42:18 +00:00
$owner = $row -> rolname ;
} else {
$owner = false ;
}
2006-06-28 18:05:08 +00:00
return $owner ;
2006-06-27 17:06:36 +00:00
}
2006-06-28 18:05:08 +00:00
function fieldInfo ( $table , $field ) {
2007-03-19 02:40:32 +00:00
return PostgresField :: fromText ( $this , $table , $field );
2006-06-27 17:06:36 +00:00
}
2008-07-24 12:37:07 +00:00
/**
* pg_field_type () wrapper
*/
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 ;
}
2006-07-16 12:28:38 +00:00
function setup_database () {
2007-01-09 06:01:01 +00:00
global $wgVersion , $wgDBmwschema , $wgDBts2schema , $wgDBport , $wgDBuser ;
2008-02-10 15:38:48 +00:00
// Make sure that we can write to the correct schema
// If not, Postgres will happily and silently go to the next search_path item
$ctest = " mediawiki_test_table " ;
2007-12-06 17:55:50 +00:00
$safeschema = $this -> quote_ident ( $wgDBmwschema );
2007-03-26 00:12:26 +00:00
if ( $this -> tableExists ( $ctest , $wgDBmwschema )) {
2007-12-06 17:55:50 +00:00
$this -> doQuery ( " DROP TABLE $safeschema . $ctest " );
2007-03-26 00:12:26 +00:00
}
2007-12-06 17:55:50 +00:00
$SQL = " CREATE TABLE $safeschema . $ctest (a int) " ;
2007-05-15 02:59:20 +00:00
$olde = error_reporting ( 0 );
2007-01-09 06:01:01 +00:00
$res = $this -> doQuery ( $SQL );
2007-05-15 02:59:20 +00:00
error_reporting ( $olde );
2007-01-09 06:01:01 +00:00
if ( ! $res ) {
2009-02-06 03:46:35 +00:00
print " <b>FAILED</b>. Make sure that the user \" " . htmlspecialchars ( $wgDBuser ) .
" \" can write to the schema \" " . htmlspecialchars ( $wgDBmwschema ) . " \" </li> \n " ;
2010-04-29 21:49:58 +00:00
dieout ( " " ); # Will close the main list <ul> and finish the page.
2007-01-09 06:01:01 +00:00
}
2008-02-10 14:48:50 +00:00
$this -> doQuery ( " DROP TABLE $safeschema . $ctest " );
2006-07-16 12:28:38 +00:00
2009-07-29 23:41:16 +00:00
$res = $this -> sourceFile ( " ../maintenance/postgres/tables.sql " );
2010-04-29 21:49:58 +00:00
if ( $res === true ) {
print " done.</li> \n " ;
} else {
print " <b>FAILED</b></li> \n " ;
dieout ( htmlspecialchars ( $res ) );
}
2007-01-16 04:04:55 +00:00
2006-07-16 12:28:38 +00:00
## Update version information
$mwv = $this -> addQuotes ( $wgVersion );
$pgv = $this -> addQuotes ( $this -> getServerVersion ());
$pgu = $this -> addQuotes ( $this -> mUser );
$mws = $this -> addQuotes ( $wgDBmwschema );
$tss = $this -> addQuotes ( $wgDBts2schema );
$pgp = $this -> addQuotes ( $wgDBport );
$dbn = $this -> addQuotes ( $this -> mDBname );
2009-02-06 03:46:35 +00:00
$ctype = $this -> addQuotes ( pg_fetch_result ( $this -> doQuery ( " SHOW lc_ctype " ), 0 , 0 ) );
2006-07-16 12:28:38 +00:00
2010-04-29 21:49:58 +00:00
echo " <li>Populating interwiki table... " ;
2006-06-29 01:55:52 +00:00
## Avoid the non-standard "REPLACE INTO" syntax
$f = fopen ( " ../maintenance/interwiki.sql " , 'r' );
2010-06-29 09:11:37 +00:00
if ( ! $f ) {
2010-04-29 21:49:58 +00:00
print " <b>FAILED</b></li> " ;
dieout ( " Could not find the interwiki.sql file " );
2006-06-29 01:55:52 +00:00
}
## We simply assume it is already empty as we have just created it
$SQL = " INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES " ;
while ( ! feof ( $f ) ) {
$line = fgets ( $f , 1024 );
2006-11-29 11:43:58 +00:00
$matches = array ();
if ( ! preg_match ( '/^\s*(\(.+?),(\d)\)/' , $line , $matches )) {
2006-06-29 01:55:52 +00:00
continue ;
}
$this -> query ( " $SQL $matches[1] , $matches[2] ) " );
}
2010-04-29 21:49:58 +00:00
print " successfully populated.</li> \n " ;
2007-03-26 00:12:26 +00:00
$this -> doQuery ( " COMMIT " );
2006-06-29 01:55:52 +00:00
}
2007-06-08 00:57:22 +00:00
function encodeBlob ( $b ) {
2007-09-23 19:54:56 +00:00
return new Blob ( pg_escape_bytea ( $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 ) {
2007-09-23 19:54:56 +00:00
if ( $b instanceof Blob ) {
$b = $b -> fetch ();
}
2006-07-05 03:59:54 +00:00
return pg_unescape_bytea ( $b );
}
function strencode ( $s ) { ## Should not be called by us
return pg_escape_string ( $s );
}
function addQuotes ( $s ) {
if ( is_null ( $s ) ) {
return 'NULL' ;
2008-08-21 13:10:19 +00:00
} else if ( is_bool ( $s ) ) {
return intval ( $s );
2007-09-23 19:54:56 +00:00
} else if ( $s instanceof Blob ) {
return " ' " . $s -> fetch ( $s ) . " ' " ;
2006-07-05 03:59:54 +00:00
}
return " ' " . pg_escape_string ( $s ) . " ' " ;
}
2006-07-24 22:13:24 +00:00
function quote_ident ( $s ) {
return '"' . preg_replace ( '/"/' , '""' , $s ) . '"' ;
}
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 );
if ( $this -> numeric_version >= 8.3 ) {
// Thanks for not providing backwards-compatibility, 8.3
$ins = preg_replace ( " /to_tsvector \ s* \ ( \ s*'default' \ s*,/ " , 'to_tsvector(' , $ins );
}
if ( $this -> numeric_version <= 8.1 ) { // Our minimum version
$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 ;
}
}
2007-03-19 02:40:32 +00:00
if ( isset ( $options [ 'GROUP BY' ] ) ) $preLimitTail .= " GROUP BY " . $options [ 'GROUP BY' ];
2007-05-04 22:54:13 +00:00
if ( isset ( $options [ 'HAVING' ] ) ) $preLimitTail .= " HAVING { $options [ 'HAVING' ] } " ;
2007-03-19 02:40:32 +00:00
if ( isset ( $options [ 'ORDER BY' ] ) ) $preLimitTail .= " ORDER BY " . $options [ 'ORDER BY' ];
2008-04-14 07:45:50 +00:00
2007-03-11 03:59:37 +00:00
//if (isset($options['LIMIT'])) {
// $tailOpts .= $this->limitResult('', $options['LIMIT'],
2008-04-14 07:45:50 +00:00
// isset($options['OFFSET']) ? $options['OFFSET']
2007-03-11 03:59:37 +00:00
// : false);
//}
2006-12-25 22:54:01 +00:00
2007-03-11 15:49:27 +00:00
if ( isset ( $noKeyOptions [ 'FOR UPDATE' ] ) ) $postLimitTail .= ' FOR UPDATE' ;
if ( isset ( $noKeyOptions [ 'LOCK IN SHARE MODE' ] ) ) $postLimitTail .= ' LOCK IN SHARE MODE' ;
2007-07-07 21:51:06 +00:00
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 () {
return " SearchPostgres " ;
}
2006-12-25 17:25:18 +00:00
} // end DatabasePostgres class