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 require_once( 'Database.php' );
19 class DatabasePgsql
extends Database
{
20 var $mInsertId = NULL;
21 var $mLastResult = NULL;
23 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
24 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
26 Database
::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
29 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
30 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
32 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
35 # Usually aborts on failure
36 # If the failFunction is set to a non-zero integer, returns success
37 function open( $server, $user, $password, $dbName )
39 # Test for PostgreSQL support, to avoid suppressed fatal error
40 if ( !function_exists( 'pg_connect' ) ) {
41 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
45 $this->mServer
= $server;
47 $this->mPassword
= $password;
48 $this->mDBname
= $dbName;
52 if ( '' != $dbName ) {
53 # start a database connection
54 @$this->mConn
= pg_connect("host=$server dbname=$dbName user=$user password=$password");
55 if ( $this->mConn
== false ) {
56 wfDebug( "DB connection error\n" );
57 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
58 wfDebug( $this->lastError()."\n" );
60 $this->mOpened
= true;
66 # Closes a database connection, if it is open
67 # Returns success, true if already closed
70 $this->mOpened
= false;
72 return pg_close( $this->mConn
);
78 function doQuery( $sql ) {
79 return $this->mLastResult
=pg_query( $this->mConn
, $sql);
82 function queryIgnore( $sql, $fname = '' ) {
83 return $this->query( $sql, $fname, true );
86 function freeResult( $res ) {
87 if ( !@pg_free_result
( $res ) ) {
88 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
91 function fetchObject( $res ) {
92 @$row = pg_fetch_object( $res );
93 # FIXME: HACK HACK HACK HACK debug
96 # hashar : not sure if the following test really trigger if the object
98 if( pg_last_error($this->mConn
) ) {
99 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
104 function fetchRow( $res ) {
105 @$row = pg_fetch_array( $res );
106 if( pg_last_error($this->mConn
) ) {
107 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
112 function numRows( $res ) {
113 @$n = pg_num_rows( $res );
114 if( pg_last_error($this->mConn
) ) {
115 wfDebugDieBacktrace( 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
119 function numFields( $res ) { return pg_num_fields( $res ); }
120 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
122 # This must be called after nextSequenceVal
123 function insertId() {
124 return $this->mInsertId
;
127 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
128 function lastError() { return pg_last_error(); }
129 function lastErrno() { return 1; }
131 function affectedRows() {
132 return pg_affected_rows( $this->mLastResult
);
135 # Returns information about an index
136 # If errors are explicitly ignored, returns NULL on failure
137 function indexInfo( $table, $index, $fname = 'Database::indexExists' )
139 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
140 $res = $this->query( $sql, $fname );
145 while ( $row = $this->fetchObject( $res ) ) {
146 if ( $row->Key_name
== $index ) {
153 function fieldInfo( $table, $field )
155 wfDebugDieBacktrace( 'Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre' );
157 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
158 $n = pg_num_fields( $res );
159 for( $i = 0; $i < $n; $i++ ) {
161 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
162 $meta = mysql_fetch_field( $res, $i );
163 if( $field == $meta->name ) {
170 function insertArray( $table, $a, $fname = 'Database::insertArray', $options = array() ) {
171 # PostgreSQL doesn't support options
172 # We have a go at faking one of them
173 # TODO: DELAYED, LOW_PRIORITY
175 # IGNORE is performed using single-row inserts, ignoring errors in each
176 if ( in_array( 'IGNORE', $options ) ) {
177 # FIXME: need some way to distiguish between key collision and other types of error
178 $oldIgnore = $this->ignoreErrors( true );
179 if ( !is_array( reset( $a ) ) ) {
182 foreach ( $a as $row ) {
183 parent
::insertArray( $table, $row, $fname, array() );
185 $this->ignoreErrors( $oldIgnore );
188 $retVal = parent
::insertArray( $table, $a, $fname, array() );
193 function startTimer( $timeout )
196 wfDebugDieBacktrace( 'Database::startTimer() error : mysql_thread_id() not implemented for postgre' );
197 /*$tid = mysql_thread_id( $this->mConn );
198 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
201 function tableName( $name ) {
202 # First run any transformations from the parent object
203 $name = parent
::tableName( $name );
205 # Now quote PG reserved keywords
216 function strencode( $s ) {
217 return pg_escape_string( $s );
220 # Return the next in a sequence, save the value for retrieval via insertId()
221 function nextSequenceValue( $seqName ) {
222 $value = $this->getField(''," nextval('" . $seqName . "')");
223 $this->mInsertId
= $value;
228 # PostgreSQL doesn't have them and returns ""
229 function useIndexClause( $index ) {
233 # REPLACE query wrapper
234 # PostgreSQL simulates this with a DELETE followed by INSERT
235 # $row is the row to insert, an associative array
236 # $uniqueIndexes is an array of indexes. Each element may be either a
237 # field name or an array of field names
239 # It may be more efficient to leave off unique indexes which are unlikely to collide.
240 # However if you do this, you run the risk of encountering errors which wouldn't have
242 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
243 $table = $this->tableName( $table );
246 if ( !is_array( reset( $rows ) ) ) {
247 $rows = array( $rows );
250 foreach( $rows as $row ) {
251 # Delete rows which collide
252 if ( $uniqueIndexes ) {
253 $sql = "DELETE FROM $table WHERE (";
255 foreach ( $uniqueIndexes as $index ) {
261 if ( is_array( $index ) ) {
264 foreach ( $index as $col ) {
270 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
273 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
277 $this->query( $sql, $fname );
281 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
282 $this->makeList( $row, LIST_COMMA
) . ')';
283 $this->query( $sql, $fname );
287 # DELETE where the condition is a join
288 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
290 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
293 $delTable = $this->tableName( $delTable );
294 $joinTable = $this->tableName( $joinTable );
295 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
296 if ( $conds != '*' ) {
297 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
301 $this->query( $sql, $fname );
304 # Returns the size of a text field, or -1 for "unlimited"
305 function textFieldSize( $table, $field ) {
306 $table = $this->tableName( $table );
307 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
308 $size = pg_field_size( $res, 0 );
309 $this->freeResult( $res );
313 function lowPriorityOption() {
317 function limitResult($limit,$offset) {
318 return " LIMIT $limit ".(is_numeric($offset)?
" OFFSET {$offset} ":"");
321 # FIXME: actually detecting deadlocks might be nice
322 function wasDeadlock() {
326 # Return DB-style timestamp used for MySQL schema
327 function timestamp( $ts=0 ) {
328 return wfTimestamp(TS_DB
,$ts);
331 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
332 $message = "A database error has occurred\n" .
334 "Function: $fname\n" .
335 "Error: $errno $error\n";
336 wfDebugDieBacktrace($message);
341 class DatabasePostgreSQL
extends DatabasePgsql
{