Make RC patroling an optional feature that can be turned off by setting wgUseRCPatrol...
[mediawiki.git] / includes / DatabasePostgreSQL.php
blob6ada5ed04de1ac5190b3f7713b08dc81321bd733
1 <?php
2 # $Id$
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
8 # ported.
9 #
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."
15 # Hashar
17 require_once( "Database.php" );
19 class DatabasePgsql extends Database {
20 var $mInsertId = NULL;
22 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
23 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
25 Database::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
28 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
29 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
31 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
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" );
43 $this->close();
44 $this->mServer = $server;
45 $this->mUser = $user;
46 $this->mPassword = $password;
47 $this->mDBname = $dbName;
49 $success = false;
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" );
58 } else {
59 $this->mOpened = true;
62 return $this->mConn;
65 # Closes a database connection, if it is open
66 # Returns success, true if already closed
67 function close()
69 $this->mOpened = false;
70 if ( $this->mConn ) {
71 return pg_close( $this->mConn );
72 } else {
73 return true;
77 function doQuery( $sql ) {
78 return pg_query( $this->mConn , $sql);
81 function queryIgnore( $sql, $fname = "" ) {
82 return $this->query( $sql, $fname, true );
85 function freeResult( $res ) {
86 if ( !@pg_free_result( $res ) ) {
87 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
90 function fetchObject( $res ) {
91 @$row = pg_fetch_object( $res );
92 # FIXME: HACK HACK HACK HACK debug
94 # TODO:
95 # hashar : not sure if the following test really trigger if the object
96 # fetching failled.
97 if( pg_last_error($this->mConn) ) {
98 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
100 return $row;
103 function fetchRow( $res ) {
104 @$row = pg_fetch_array( $res );
105 if( pg_last_error($this->mConn) ) {
106 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
108 return $row;
111 function numRows( $res ) {
112 @$n = pg_num_rows( $res );
113 if( pg_last_error($this->mConn) ) {
114 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
116 return $n;
118 function numFields( $res ) { return pg_num_fields( $res ); }
119 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
121 # This must be called after nextSequenceVal
122 function insertId() {
123 return $this->mInsertId;
126 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
127 function lastError() { return pg_last_error(); }
128 function lastErrno() { return 1; }
130 function affectedRows() {
131 return pg_affected_rows( $this->mLastResult );
134 # Returns information about an index
135 # If errors are explicitly ignored, returns NULL on failure
136 function indexInfo( $table, $index, $fname = "Database::indexExists" )
138 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
139 $res = $this->query( $sql, $fname );
140 if ( !$res ) {
141 return NULL;
144 while ( $row = $this->fetchObject( $res ) ) {
145 if ( $row->Key_name == $index ) {
146 return $row;
149 return false;
152 function fieldInfo( $table, $field )
154 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
156 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
157 $n = pg_num_fields( $res );
158 for( $i = 0; $i < $n; $i++ ) {
159 // FIXME
160 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
161 $meta = mysql_fetch_field( $res, $i );
162 if( $field == $meta->name ) {
163 return $meta;
166 return false;*/
169 function insertArray( $table, $a, $fname = "Database::insertArray", $options = array() ) {
170 # PostgreSQL doesn't support options
171 # We have a go at faking one of them
172 # TODO: DELAYED, LOW_PRIORITY
174 # IGNORE is performed using single-row inserts, ignoring errors in each
175 if ( in_array( 'IGNORE', $options ) ) {
176 # FIXME: need some way to distiguish between key collision and other types of error
177 $oldIgnore = $this->ignoreErrors( true );
178 if ( !is_array( reset( $a ) ) ) {
179 $a = array( $a );
181 foreach ( $a as $row ) {
182 parent::insertArray( $table, $row, $fname, array() );
184 $this->ignoreErrors( $oldIgnore );
185 $retVal = true;
186 } else {
187 $retVal = parent::insertArray( $table, $a, $fname, array() );
189 return $retVal;
192 function startTimer( $timeout )
194 global $IP;
195 wfDebugDieBacktrace( "Database::startTimer() error : mysql_thread_id() not implemented for postgre" );
196 /*$tid = mysql_thread_id( $this->mConn );
197 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
200 function tableName( $name ) {
201 # First run any transformations from the parent object
202 $name = parent::tableName( $name );
204 # Now quote PG reserved keywords
205 switch( $name ) {
206 case 'user':
207 return '"user"';
208 case 'old':
209 return '"old"';
210 default:
211 return $name;
215 function strencode( $s ) {
216 return pg_escape_string( $s );
219 # Return the next in a sequence, save the value for retrieval via insertId()
220 function nextSequenceValue( $seqName ) {
221 $value = $this->getField(""," nextval('" . $seqName . "')");
222 $this->mInsertId = $value;
223 return $value;
226 # USE INDEX clause
227 # PostgreSQL doesn't have them and returns ""
228 function useIndexClause( $index ) {
229 return '';
232 # REPLACE query wrapper
233 # PostgreSQL simulates this with a DELETE followed by INSERT
234 # $row is the row to insert, an associative array
235 # $uniqueIndexes is an array of indexes. Each element may be either a
236 # field name or an array of field names
238 # It may be more efficient to leave off unique indexes which are unlikely to collide.
239 # However if you do this, you run the risk of encountering errors which wouldn't have
240 # occurred in MySQL
241 function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
242 $table = $this->tableName( $table );
244 # Single row case
245 if ( !is_array( reset( $rows ) ) ) {
246 $rows = array( $rows );
249 foreach( $rows as $row ) {
250 # Delete rows which collide
251 if ( $uniqueIndexes ) {
252 $sql = "DELETE FROM $table WHERE (";
253 $first = true;
254 foreach ( $uniqueIndexes as $index ) {
255 if ( $first ) {
256 $first = false;
257 } else {
258 $sql .= ") OR (";
260 if ( is_array( $index ) ) {
261 $first2 = true;
262 $sql .= "(";
263 foreach ( $index as $col ) {
264 if ( $first2 ) {
265 $first2 = false;
266 } else {
267 $sql .= " AND ";
269 $sql .= "$col=" . $this->addQuotes( $row[$col] );
271 } else {
272 $sql .= "$index=" . $this->addQuotes( $row[$index] );
275 $sql .= ")";
276 $this->query( $sql, $fname );
279 # Now insert the row
280 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
281 $this->makeList( $row, LIST_COMMA ) . ')';
282 $this->query( $sql, $fname );
286 # DELETE where the condition is a join
287 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
288 if ( !$conds ) {
289 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
292 $delTable = $this->tableName( $delTable );
293 $joinTable = $this->tableName( $joinTable );
294 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
295 if ( $conds != '*' ) {
296 $sql .= "WHERE " . $this->makeList( $conds, LIST_AND );
298 $sql .= ")";
300 $this->query( $sql, $fname );
303 # Returns the size of a text field, or -1 for "unlimited"
304 function textFieldSize( $table, $field ) {
305 $table = $this->tableName( $table );
306 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
307 $size = pg_field_size( $res, 0 );
308 $this->freeResult( $res );
309 return $size;
312 function lowPriorityOption() {
313 return '';
316 function limitResult($limit,$offset) {
317 return " LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
320 # FIXME: actually detecting deadlocks might be nice
321 function wasDeadlock() {
322 return false;
326 # Just an alias.
327 class DatabasePostgreSQL extends DatabasePgsql {