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, $debug = false, $bufferResults = true, $ignoreErrors = false)
23 Database
::Database( $server, $user, $password, $dbName, $failFunction, $debug,
24 $bufferResults, $ignoreErrors );
27 /* static */ function newFromParams( $server, $user, $password, $dbName,
28 $failFunction = false, $debug = false, $bufferResults = true, $ignoreErrors = false )
30 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $debug,
31 $bufferResults, $ignoreErrors );
34 # Usually aborts on failure
35 # If the failFunction is set to a non-zero integer, returns success
36 function open( $server, $user, $password, $dbName )
38 # Test for PostgreSQL support, to avoid suppressed fatal error
39 if ( !function_exists( 'pg_connect' ) ) {
40 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
44 $this->mServer
= $server;
46 $this->mPassword
= $password;
47 $this->mDBname
= $dbName;
51 if ( "" != $dbName ) {
52 # start a database connection
53 @$this->mConn
= pg_connect("host=$server dbname=$dbName user=$user password=$password");
54 if ( $this->mConn
== false ) {
55 wfDebug( "DB connection error\n" );
56 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
57 wfDebug( $this->lastError()."\n" );
59 $this->mOpened
= true;
65 # Closes a database connection, if it is open
66 # Returns success, true if already closed
69 $this->mOpened
= false;
71 return pg_close( $this->mConn
);
77 # Usually aborts on failure
78 # If errors are explicitly ignored, returns success
79 function query( $sql, $fname = "", $tempIgnore = false )
84 # generalizeSQL will probably cut down the query to reasonable
85 # logging size most of the time. The substr is really just a sanity check.
86 $profName = "query: " . substr( Database
::generalizeSQL( $sql ), 0, 255 );
87 wfProfileIn( $profName );
90 $this->mLastQuery
= $sql;
92 if ( $this->mDebug
) {
93 $sqlx = substr( $sql, 0, 500 );
94 $sqlx = wordwrap(strtr($sqlx,"\t\n"," "));
95 wfDebug( "SQL: $sqlx\n" );
98 $ret = pg_query( $this->mConn
, $sql);
99 $this->mLastResult
= $ret;
100 if ( false == $ret ) {
101 // Ignore errors during error handling to prevent infinite recursion
102 $ignore = $this->setIgnoreErrors( true );
103 $error = pg_last_error( $this->mConn
);
104 // TODO FIXME : no error number function in postgre
105 // $errno = mysql_errno( $this->mConn );
106 if( $ignore ||
$tempIgnore ) {
107 wfDebug("SQL ERROR (ignored): " . $error . "\n");
109 wfDebug("SQL ERROR: " . $error . "\n");
111 // this calls wfAbruptExit()
112 $this->mOut
->databaseError( $fname, $sql, $error, 0 );
115 $this->setIgnoreErrors( $ignore );
118 if ( $wgProfiling ) {
119 wfProfileOut( $profName );
124 function queryIgnore( $sql, $fname = "" ) {
125 return $this->query( $sql, $fname, true );
128 function freeResult( $res ) {
129 if ( !@pg_free_result
( $res ) ) {
130 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
133 function fetchObject( $res ) {
134 @$row = pg_fetch_object( $res );
135 # FIXME: HACK HACK HACK HACK debug
138 # hashar : not sure if the following test really trigger if the object
140 if( pg_last_error($this->mConn
) ) {
141 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
146 function fetchRow( $res ) {
147 @$row = pg_fetch_array( $res );
148 if( pg_last_error($this->mConn
) ) {
149 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
154 function numRows( $res ) {
155 @$n = pg_num_rows( $res );
156 if( pg_last_error($this->mConn
) ) {
157 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
161 function numFields( $res ) { return pg_num_fields( $res ); }
162 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
164 # This must be called after nextSequenceVal
165 function insertId() {
166 return $this->mInsertId
;
169 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
170 function lastError() { return pg_last_error(); }
171 function affectedRows() {
172 return pg_affected_rows( $this->mLastResult
);
175 # Returns information about an index
176 # If errors are explicitly ignored, returns NULL on failure
177 function indexInfo( $table, $index, $fname = "Database::indexExists" )
179 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
180 $res = $this->query( $sql, $fname );
185 while ( $row = $this->fetchObject( $res ) ) {
186 if ( $row->Key_name
== $index ) {
193 function fieldInfo( $table, $field )
195 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
197 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
198 $n = pg_num_fields( $res );
199 for( $i = 0; $i < $n; $i++ ) {
201 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
202 $meta = mysql_fetch_field( $res, $i );
203 if( $field == $meta->name ) {
210 function insertArray( $table, $a, $fname = "Database::insertArray", $options = array() ) {
211 # PostgreSQL doesn't support options
212 # We have a go at faking some of them
213 if ( in_array( 'IGNORE', $options ) ) {
215 $oldIgnore = $this->setIgnoreErrors( true );
217 $retVal = parent
::insertArray( $table, $a, $fname, array() );
219 $this->setIgnoreErrors( $oldIgnore );
224 function startTimer( $timeout )
227 wfDebugDieBacktrace( "Database::startTimer() error : mysql_thread_id() not implemented for postgre" );
228 /*$tid = mysql_thread_id( $this->mConn );
229 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
232 function tableName( $name ) {
233 # First run any transformations from the parent object
234 $name = parent
::tableName( $name );
236 # Now quote PG reserved keywords
247 function strencode( $s ) {
248 return pg_escape_string( $s );
251 # Return the next in a sequence, save the value for retrieval via insertId()
252 function nextSequenceValue( $seqName ) {
253 $value = $this->getField(""," nextval('" . $seqName . "')");
254 $this->mInsertId
= $value;
259 # PostgreSQL doesn't have them and returns ""
260 function useIndexClause( $index ) {
264 # REPLACE query wrapper
265 # PostgreSQL simulates this with a DELETE followed by INSERT
266 # $row is the row to insert, an associative array
267 # $uniqueIndexes is an array of indexes. Each element may be either a
268 # field name or an array of field names
270 # It may be more efficient to leave off unique indexes which are unlikely to collide.
271 # However if you do this, you run the risk of encountering errors which wouldn't have
273 function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
274 $table = $this->tableName( $table );
276 # Delete rows which collide
277 if ( $uniqueIndexes ) {
278 $sql = "DELETE FROM $table WHERE (";
280 foreach ( $uniqueIndexes as $index ) {
286 if ( is_array( $col ) ) {
289 foreach ( $index as $col ) {
295 $sql .= "$col = " $this->strencode
303 $indexValues = array();
304 foreach ( $rows as $row ) {
305 $indexValues[] = $row[$col];
307 $sql .= $this->makeList( $indexValues, LIST_COMMA
) . ") ";
309 $this->query( $sql, $fname );
312 # Now insert the rows
313 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $rows[0] ) ) .") VALUES ";
315 foreach ( $rows as $row ) {
321 $sql .= "(" . $this->makeList( $row, LIST_COMMA
) . ")";
323 $this->query( $sql, $fname );
326 # DELETE where the condition is a join
327 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
329 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
332 $delTable = $this->tableName( $delTable );
333 $joinTable = $this->tableName( $joinTable );
334 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
335 if ( $conds != '*' ) {
336 $sql .= "WHERE " . $this->makeList( $conds, LIST_AND
);
340 $this->query( $sql, $fname );
343 # Returns the size of a text field, or -1 for "unlimited"
344 function textFieldSize( $table, $field ) {
345 $table = $this->tableName( $table );
346 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
347 $size = pg_field_size( $res, 0 );
348 $this->freeResult( $res );
352 function lowPriorityOption() {
357 function wfLimitResult( $limit, $offset ) {
358 return " LIMIT $limit ".(is_numeric($offset)?
" OFFSET {$offset} ":"");