5 * DO NOT USE !!! Unless you want to help developping it.
7 * This file is an attempt to port the mysql database layer to postgreSQL. The
8 * only thing done so far is s/mysql/pg/ and dieing if function haven't been
11 * As said brion 07/06/2004 :
12 * "table definitions need to be changed. fulltext index needs to work differently
13 * things that use the last insert id need to be changed. Probably other things
14 * need to be changed. various semantics may be different."
23 require_once( 'Database.php' );
28 class DatabasePgsql
extends Database
{
29 var $mInsertId = NULL;
30 var $mLastResult = NULL;
32 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
33 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
35 Database
::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
38 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
39 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
41 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
45 * Usually aborts on failure
46 * If the failFunction is set to a non-zero integer, returns success
48 function open( $server, $user, $password, $dbName ) {
49 # Test for PostgreSQL support, to avoid suppressed fatal error
50 if ( !function_exists( 'pg_connect' ) ) {
51 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
55 $this->mServer
= $server;
57 $this->mPassword
= $password;
58 $this->mDBname
= $dbName;
62 if ( '' != $dbName ) {
63 # start a database connection
64 @$this->mConn
= pg_connect("host=$server dbname=$dbName user=$user password=$password");
65 if ( $this->mConn
== false ) {
66 wfDebug( "DB connection error\n" );
67 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
68 wfDebug( $this->lastError()."\n" );
70 $this->mOpened
= true;
77 * Closes a database connection, if it is open
78 * Returns success, true if already closed
81 $this->mOpened
= false;
83 return pg_close( $this->mConn
);
89 function doQuery( $sql ) {
90 return $this->mLastResult
=pg_query( $this->mConn
, $sql);
93 function queryIgnore( $sql, $fname = '' ) {
94 return $this->query( $sql, $fname, true );
97 function freeResult( $res ) {
98 if ( !@pg_free_result
( $res ) ) {
99 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
103 function fetchObject( $res ) {
104 @$row = pg_fetch_object( $res );
105 # FIXME: HACK HACK HACK HACK debug
108 # hashar : not sure if the following test really trigger if the object
110 if( pg_last_error($this->mConn
) ) {
111 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
116 function fetchRow( $res ) {
117 @$row = pg_fetch_array( $res );
118 if( pg_last_error($this->mConn
) ) {
119 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
124 function numRows( $res ) {
125 @$n = pg_num_rows( $res );
126 if( pg_last_error($this->mConn
) ) {
127 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
131 function numFields( $res ) { return pg_num_fields( $res ); }
132 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
135 * This must be called after nextSequenceVal
137 function insertId() {
138 return $this->mInsertId
;
141 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
142 function lastError() { return pg_last_error(); }
143 function lastErrno() { return 1; }
145 function affectedRows() {
146 return pg_affected_rows( $this->mLastResult
);
150 * Returns information about an index
151 * If errors are explicitly ignored, returns NULL on failure
153 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
154 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
155 $res = $this->query( $sql, $fname );
160 while ( $row = $this->fetchObject( $res ) ) {
161 if ( $row->Key_name
== $index ) {
168 function fieldInfo( $table, $field ) {
169 wfDebugDieBacktrace( 'Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre' );
171 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
172 $n = pg_num_fields( $res );
173 for( $i = 0; $i < $n; $i++ ) {
175 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
176 $meta = mysql_fetch_field( $res, $i );
177 if( $field == $meta->name ) {
184 function insertArray( $table, $a, $fname = 'Database::insertArray', $options = array() ) {
185 # PostgreSQL doesn't support options
186 # We have a go at faking one of them
187 # TODO: DELAYED, LOW_PRIORITY
189 if ( !is_array($options))
190 $options = array($options);
192 if ( in_array( 'IGNORE', $options ) )
193 $oldIgnore = $this->ignoreErrors( true );
195 # IGNORE is performed using single-row inserts, ignoring errors in each
196 # FIXME: need some way to distiguish between key collision and other types of error
197 $oldIgnore = $this->ignoreErrors( true );
198 if ( !is_array( reset( $a ) ) ) {
201 foreach ( $a as $row ) {
202 parent
::insertArray( $table, $row, $fname, array() );
204 $this->ignoreErrors( $oldIgnore );
207 if ( in_array( 'IGNORE', $options ) )
208 $this->ignoreErrors( $oldIgnore );
213 function startTimer( $timeout )
216 wfDebugDieBacktrace( 'Database::startTimer() error : mysql_thread_id() not implemented for postgre' );
217 /*$tid = mysql_thread_id( $this->mConn );
218 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
221 function tableName( $name ) {
222 # First run any transformations from the parent object
223 $name = parent
::tableName( $name );
225 # Now quote PG reserved keywords
236 function strencode( $s ) {
237 return pg_escape_string( $s );
241 * Return the next in a sequence, save the value for retrieval via insertId()
243 function nextSequenceValue( $seqName ) {
244 $value = $this->getField(''," nextval('" . $seqName . "')");
245 $this->mInsertId
= $value;
251 * PostgreSQL doesn't have them and returns ""
253 function useIndexClause( $index ) {
257 # REPLACE query wrapper
258 # PostgreSQL simulates this with a DELETE followed by INSERT
259 # $row is the row to insert, an associative array
260 # $uniqueIndexes is an array of indexes. Each element may be either a
261 # field name or an array of field names
263 # It may be more efficient to leave off unique indexes which are unlikely to collide.
264 # However if you do this, you run the risk of encountering errors which wouldn't have
266 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
267 $table = $this->tableName( $table );
270 if ( !is_array( reset( $rows ) ) ) {
271 $rows = array( $rows );
274 foreach( $rows as $row ) {
275 # Delete rows which collide
276 if ( $uniqueIndexes ) {
277 $sql = "DELETE FROM $table WHERE (";
279 foreach ( $uniqueIndexes as $index ) {
285 if ( is_array( $index ) ) {
288 foreach ( $index as $col ) {
294 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
297 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
301 $this->query( $sql, $fname );
305 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
306 $this->makeList( $row, LIST_COMMA
) . ')';
307 $this->query( $sql, $fname );
311 # DELETE where the condition is a join
312 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
314 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
317 $delTable = $this->tableName( $delTable );
318 $joinTable = $this->tableName( $joinTable );
319 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
320 if ( $conds != '*' ) {
321 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
325 $this->query( $sql, $fname );
328 # Returns the size of a text field, or -1 for "unlimited"
329 function textFieldSize( $table, $field ) {
330 $table = $this->tableName( $table );
331 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
332 $size = pg_field_size( $res, 0 );
333 $this->freeResult( $res );
337 function lowPriorityOption() {
341 function limitResult($limit,$offset) {
342 return " LIMIT $limit ".(is_numeric($offset)?
" OFFSET {$offset} ":"");
345 # FIXME: actually detecting deadlocks might be nice
346 function wasDeadlock() {
350 # Return DB-style timestamp used for MySQL schema
351 function timestamp( $ts=0 ) {
352 return wfTimestamp(TS_DB
,$ts);
355 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
356 $message = "A database error has occurred\n" .
358 "Function: $fname\n" .
359 "Error: $errno $error\n";
360 wfDebugDieBacktrace($message);
367 class DatabasePostgreSQL
extends DatabasePgsql
{