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 {
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
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
$table = $db -> tableName ( $table );
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
*/
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-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 " );
2010-10-24 20:48:48 +00:00
throw new DBConnectionError ( $this , $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__ );
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 ) {
return ( bool ) $this -> open ( $this -> mServer , $this -> mUser , $this -> mPassword , $db );
}
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
*/
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 ) {
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
}
2010-09-05 18:35:34 +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 ;
}
2004-06-07 02:59:58 +00:00
if ( !@ pg_free_result ( $res ) ) {
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 ;
}
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.
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 ;
}
2004-06-10 13:02:27 +00:00
@ $row = pg_fetch_array ( $res );
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 ;
}
2006-01-07 13:09:30 +00:00
@ $n = pg_num_rows ( $res );
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
*/
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 ()
*/
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
*/
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 .
* @ todo FIXME : implement this a little better ( seperate select / insert ) ?
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 ) ) {
$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 " ;
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-04-12 18:59:19 +00:00
function tableName ( $name , $quoted = true ) {
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 :
2011-04-12 18:59:19 +00:00
return parent :: tableName ( $name , $quoted );
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 ()
*/
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 .
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 ;
}
2010-09-05 18:35:34 +00:00
/**
* REPLACE query wrapper
* Postgres simulates this with a DELETE followed by INSERT
* $row is the row to insert , an associative array
* $uniqueIndexes is an array of indexes . Each element may be either a
* field name or an array of field names
*
* 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
* 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
2010-09-05 18:35:34 +00:00
if ( count ( $rows ) == 0 ) {
2004-09-06 09:56:29 +00:00
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 ;
2010-09-05 18:35:34 +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 ) {
2010-09-05 18:35:34 +00:00
throw new DBUnexpectedError ( $this , 'DatabasePostgres::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' " ;
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 );
}
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
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
2010-09-05 18:35:34 +00:00
if ( $ignore || $tempIgnore ) {
wfDebug ( " SQL ERROR (ignored): $error\n " );
2006-11-30 01:00:08 +00:00
$this -> ignoreErrors ( $ignore );
2010-09-05 18:35:34 +00:00
} else {
2010-09-05 17:40:47 +00:00
$message = " A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script \n " .
2006-11-30 01:00:08 +00:00
" Query: $sql\n " .
" Function: $fname\n " .
" Error: $errno $error\n " ;
2010-09-05 18:35:34 +00:00
throw new DBUnexpectedError ( $this , $message );
2006-11-30 01:00:08 +00:00
}
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
*/
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 )
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-02-03 05:17:18 +00:00
$table = $this -> tableName ( $table );
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 .
*/
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 ;
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
/**
* Query whether a given schema exists . Returns the name of the owner
*/
2006-06-29 17:24:04 +00:00
function schemaExists ( $schema ) {
2010-12-04 09:27:02 +00:00
$eschema = str_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 ) {
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
*/
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 ;
}
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
}
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 ) {
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' ] ) ) {
$preLimitTail .= ' GROUP BY ' . $options [ 'GROUP BY' ];
}
if ( isset ( $options [ 'HAVING' ] ) ) {
$preLimitTail .= " HAVING { $options [ 'HAVING' ] } " ;
}
if ( isset ( $options [ 'ORDER BY' ] ) ) {
$preLimitTail .= ' ORDER BY ' . $options [ 'ORDER BY' ];
}
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
}
2006-12-25 17:25:18 +00:00
} // end DatabasePostgres class