20b2617222e5921709bbcea5396e1c9b1cbb4843
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."
24 require_once( 'Database.php' );
30 class DatabasePgsql
extends Database
{
31 var $mInsertId = NULL;
32 var $mLastResult = NULL;
34 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
35 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
37 Database
::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
40 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
41 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
43 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
47 * Usually aborts on failure
48 * If the failFunction is set to a non-zero integer, returns success
50 function open( $server, $user, $password, $dbName ) {
51 # Test for PostgreSQL support, to avoid suppressed fatal error
52 if ( !function_exists( 'pg_connect' ) ) {
53 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
57 $this->mServer
= $server;
59 $this->mPassword
= $password;
60 $this->mDBname
= $dbName;
64 if ( '' != $dbName ) {
65 # start a database connection
66 @$this->mConn
= pg_connect("host=$server dbname=$dbName user=$user password=$password");
67 if ( $this->mConn
== false ) {
68 wfDebug( "DB connection error\n" );
69 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
70 wfDebug( $this->lastError()."\n" );
72 $this->mOpened
= true;
79 * Closes a database connection, if it is open
80 * Returns success, true if already closed
83 $this->mOpened
= false;
85 return pg_close( $this->mConn
);
91 function doQuery( $sql ) {
92 return $this->mLastResult
=pg_query( $this->mConn
, $sql);
95 function queryIgnore( $sql, $fname = '' ) {
96 return $this->query( $sql, $fname, true );
99 function freeResult( $res ) {
100 if ( !@pg_free_result
( $res ) ) {
101 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
105 function fetchObject( $res ) {
106 @$row = pg_fetch_object( $res );
107 # FIXME: HACK HACK HACK HACK debug
110 # hashar : not sure if the following test really trigger if the object
112 if( pg_last_error($this->mConn
) ) {
113 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
118 function fetchRow( $res ) {
119 @$row = pg_fetch_array( $res );
120 if( pg_last_error($this->mConn
) ) {
121 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
126 function numRows( $res ) {
127 @$n = pg_num_rows( $res );
128 if( pg_last_error($this->mConn
) ) {
129 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
133 function numFields( $res ) { return pg_num_fields( $res ); }
134 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
137 * This must be called after nextSequenceVal
139 function insertId() {
140 return $this->mInsertId
;
143 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
144 function lastError() { return pg_last_error(); }
145 function lastErrno() { return 1; }
147 function affectedRows() {
148 return pg_affected_rows( $this->mLastResult
);
152 * Returns information about an index
153 * If errors are explicitly ignored, returns NULL on failure
155 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
156 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
157 $res = $this->query( $sql, $fname );
162 while ( $row = $this->fetchObject( $res ) ) {
163 if ( $row->Key_name
== $index ) {
170 function fieldInfo( $table, $field ) {
171 wfDebugDieBacktrace( 'Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre' );
173 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
174 $n = pg_num_fields( $res );
175 for( $i = 0; $i < $n; $i++ ) {
177 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
178 $meta = mysql_fetch_field( $res, $i );
179 if( $field == $meta->name ) {
186 function insertArray( $table, $a, $fname = 'Database::insertArray', $options = array() ) {
187 # PostgreSQL doesn't support options
188 # We have a go at faking one of them
189 # TODO: DELAYED, LOW_PRIORITY
191 if ( !is_array($options))
192 $options = array($options);
194 if ( in_array( 'IGNORE', $options ) )
195 $oldIgnore = $this->ignoreErrors( true );
197 # IGNORE is performed using single-row inserts, ignoring errors in each
198 # FIXME: need some way to distiguish between key collision and other types of error
199 $oldIgnore = $this->ignoreErrors( true );
200 if ( !is_array( reset( $a ) ) ) {
203 foreach ( $a as $row ) {
204 parent
::insertArray( $table, $row, $fname, array() );
206 $this->ignoreErrors( $oldIgnore );
209 if ( in_array( 'IGNORE', $options ) )
210 $this->ignoreErrors( $oldIgnore );
215 function startTimer( $timeout )
218 wfDebugDieBacktrace( 'Database::startTimer() error : mysql_thread_id() not implemented for postgre' );
219 /*$tid = mysql_thread_id( $this->mConn );
220 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
223 function tableName( $name ) {
224 # First run any transformations from the parent object
225 $name = parent
::tableName( $name );
227 # Now quote PG reserved keywords
238 function strencode( $s ) {
239 return pg_escape_string( $s );
243 * Return the next in a sequence, save the value for retrieval via insertId()
245 function nextSequenceValue( $seqName ) {
246 $value = $this->getField(''," nextval('" . $seqName . "')");
247 $this->mInsertId
= $value;
253 * PostgreSQL doesn't have them and returns ""
255 function useIndexClause( $index ) {
259 # REPLACE query wrapper
260 # PostgreSQL simulates this with a DELETE followed by INSERT
261 # $row is the row to insert, an associative array
262 # $uniqueIndexes is an array of indexes. Each element may be either a
263 # field name or an array of field names
265 # It may be more efficient to leave off unique indexes which are unlikely to collide.
266 # However if you do this, you run the risk of encountering errors which wouldn't have
268 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
269 $table = $this->tableName( $table );
272 if ( !is_array( reset( $rows ) ) ) {
273 $rows = array( $rows );
276 foreach( $rows as $row ) {
277 # Delete rows which collide
278 if ( $uniqueIndexes ) {
279 $sql = "DELETE FROM $table WHERE (";
281 foreach ( $uniqueIndexes as $index ) {
287 if ( is_array( $index ) ) {
290 foreach ( $index as $col ) {
296 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
299 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
303 $this->query( $sql, $fname );
307 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
308 $this->makeList( $row, LIST_COMMA
) . ')';
309 $this->query( $sql, $fname );
313 # DELETE where the condition is a join
314 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
316 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
319 $delTable = $this->tableName( $delTable );
320 $joinTable = $this->tableName( $joinTable );
321 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
322 if ( $conds != '*' ) {
323 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
327 $this->query( $sql, $fname );
330 # Returns the size of a text field, or -1 for "unlimited"
331 function textFieldSize( $table, $field ) {
332 $table = $this->tableName( $table );
333 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
334 $size = pg_field_size( $res, 0 );
335 $this->freeResult( $res );
339 function lowPriorityOption() {
343 function limitResult($limit,$offset) {
344 return " LIMIT $limit ".(is_numeric($offset)?
" OFFSET {$offset} ":"");
347 # FIXME: actually detecting deadlocks might be nice
348 function wasDeadlock() {
352 # Return DB-style timestamp used for MySQL schema
353 function timestamp( $ts=0 ) {
354 return wfTimestamp(TS_DB
,$ts);
357 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
358 $message = "A database error has occurred\n" .
360 "Function: $fname\n" .
361 "Error: $errno $error\n";
362 wfDebugDieBacktrace($message);
370 class DatabasePostgreSQL
extends DatabasePgsql
{