4 # DO NOT USE !!! Unless you want to help developping it.
6 # This file is an attempt to port the mysql database layer to postgreSQL. The
7 # only thing done so far is s/mysql/pg/ and dieing if function haven't been
10 # As said brion 07/06/2004 :
11 # "table definitions need to be changed. fulltext index needs to work differently
12 # things that use the last insert id need to be changed. Probably other things
13 # need to be changed. various semantics may be different."
17 class DatabasePgsql
extends Database
{
18 var $mInsertId = NULL;
20 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
21 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
23 Database
::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
26 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
27 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
29 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
32 # Usually aborts on failure
33 # If the failFunction is set to a non-zero integer, returns success
34 function open( $server, $user, $password, $dbName )
36 # Test for PostgreSQL support, to avoid suppressed fatal error
37 if ( !function_exists( 'pg_connect' ) ) {
38 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
42 $this->mServer
= $server;
44 $this->mPassword
= $password;
45 $this->mDBname
= $dbName;
49 if ( "" != $dbName ) {
50 # start a database connection
51 @$this->mConn
= pg_connect("host=$server dbname=$dbName user=$user password=$password");
52 if ( $this->mConn
== false ) {
53 wfDebug( "DB connection error\n" );
54 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
55 wfDebug( $this->lastError()."\n" );
57 $this->mOpened
= true;
63 # Closes a database connection, if it is open
64 # Returns success, true if already closed
67 $this->mOpened
= false;
69 return pg_close( $this->mConn
);
75 function doQuery( $sql ) {
76 return pg_query( $this->mConn
, $sql);
79 function queryIgnore( $sql, $fname = "" ) {
80 return $this->query( $sql, $fname, true );
83 function freeResult( $res ) {
84 if ( !@pg_free_result
( $res ) ) {
85 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
88 function fetchObject( $res ) {
89 @$row = pg_fetch_object( $res );
90 # FIXME: HACK HACK HACK HACK debug
93 # hashar : not sure if the following test really trigger if the object
95 if( pg_last_error($this->mConn
) ) {
96 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
101 function fetchRow( $res ) {
102 @$row = pg_fetch_array( $res );
103 if( pg_last_error($this->mConn
) ) {
104 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
109 function numRows( $res ) {
110 @$n = pg_num_rows( $res );
111 if( pg_last_error($this->mConn
) ) {
112 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
116 function numFields( $res ) { return pg_num_fields( $res ); }
117 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
119 # This must be called after nextSequenceVal
120 function insertId() {
121 return $this->mInsertId
;
124 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
125 function lastError() { return pg_last_error(); }
126 function lastErrno() { return 1; }
128 function affectedRows() {
129 return pg_affected_rows( $this->mLastResult
);
132 # Returns information about an index
133 # If errors are explicitly ignored, returns NULL on failure
134 function indexInfo( $table, $index, $fname = "Database::indexExists" )
136 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
137 $res = $this->query( $sql, $fname );
142 while ( $row = $this->fetchObject( $res ) ) {
143 if ( $row->Key_name
== $index ) {
150 function fieldInfo( $table, $field )
152 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
154 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
155 $n = pg_num_fields( $res );
156 for( $i = 0; $i < $n; $i++ ) {
158 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
159 $meta = mysql_fetch_field( $res, $i );
160 if( $field == $meta->name ) {
167 function insertArray( $table, $a, $fname = "Database::insertArray", $options = array() ) {
168 # PostgreSQL doesn't support options
169 # We have a go at faking one of them
170 # TODO: DELAYED, LOW_PRIORITY
172 # IGNORE is performed using single-row inserts, ignoring errors in each
173 if ( in_array( 'IGNORE', $options ) ) {
174 # FIXME: need some way to distiguish between key collision and other types of error
175 $oldIgnore = $this->ignoreErrors( true );
176 if ( !is_array( reset( $a ) ) ) {
179 foreach ( $a as $row ) {
180 parent
::insertArray( $table, $row, $fname, array() );
182 $this->ignoreErrors( $oldIgnore );
185 $retVal = parent
::insertArray( $table, $a, $fname, array() );
190 function startTimer( $timeout )
193 wfDebugDieBacktrace( "Database::startTimer() error : mysql_thread_id() not implemented for postgre" );
194 /*$tid = mysql_thread_id( $this->mConn );
195 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
198 function tableName( $name ) {
199 # First run any transformations from the parent object
200 $name = parent
::tableName( $name );
202 # Now quote PG reserved keywords
213 function strencode( $s ) {
214 return pg_escape_string( $s );
217 # Return the next in a sequence, save the value for retrieval via insertId()
218 function nextSequenceValue( $seqName ) {
219 $value = $this->getField(""," nextval('" . $seqName . "')");
220 $this->mInsertId
= $value;
225 # PostgreSQL doesn't have them and returns ""
226 function useIndexClause( $index ) {
230 # REPLACE query wrapper
231 # PostgreSQL simulates this with a DELETE followed by INSERT
232 # $row is the row to insert, an associative array
233 # $uniqueIndexes is an array of indexes. Each element may be either a
234 # field name or an array of field names
236 # It may be more efficient to leave off unique indexes which are unlikely to collide.
237 # However if you do this, you run the risk of encountering errors which wouldn't have
239 function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
240 $table = $this->tableName( $table );
243 if ( !is_array( reset( $rows ) ) ) {
244 $rows = array( $rows );
247 foreach( $rows as $row ) {
248 # Delete rows which collide
249 if ( $uniqueIndexes ) {
250 $sql = "DELETE FROM $table WHERE (";
252 foreach ( $uniqueIndexes as $index ) {
258 if ( is_array( $index ) ) {
261 foreach ( $index as $col ) {
267 $sql .= "$col=" . $this->addQuotes( $row[$col] )
270 $sql .= "$index=" . $this->addQuotes( $row[$index] );
274 $this->query( $sql, $fname );
278 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
279 $this->makeList( $row, LIST_COMMA
) . ')';
280 $this->query( $sql, $fname );
284 # DELETE where the condition is a join
285 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
287 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
290 $delTable = $this->tableName( $delTable );
291 $joinTable = $this->tableName( $joinTable );
292 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
293 if ( $conds != '*' ) {
294 $sql .= "WHERE " . $this->makeList( $conds, LIST_AND
);
298 $this->query( $sql, $fname );
301 # Returns the size of a text field, or -1 for "unlimited"
302 function textFieldSize( $table, $field ) {
303 $table = $this->tableName( $table );
304 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
305 $size = pg_field_size( $res, 0 );
306 $this->freeResult( $res );
310 function lowPriorityOption() {
314 function limitResult($limit,$offset) {
315 return " LIMIT $limit ".(is_numeric($offset)?
" OFFSET {$offset} ":"");
318 # FIXME: actually detecting deadlocks might be nice
319 function wasDeadlock() {
325 class DatabasePostgreSQL
extends DatabasePgsql
{