Somewhat less hacky fix to the French l''''homme''' problem.
[mediawiki.git] / includes / DatabasePostgreSQL.php
blob36b622ceaf3c6590557b50ea3301765eed6169cc
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 class DatabasePgsql extends Database {
18 var $mInsertId = NULL;
20 function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false,
21 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
23 Database::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
26 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
27 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
29 return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
32 # Usually aborts on failure
33 # If the failFunction is set to a non-zero integer, returns success
34 function open( $server, $user, $password, $dbName )
36 # Test for PostgreSQL support, to avoid suppressed fatal error
37 if ( !function_exists( 'pg_connect' ) ) {
38 die( "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
41 $this->close();
42 $this->mServer = $server;
43 $this->mUser = $user;
44 $this->mPassword = $password;
45 $this->mDBname = $dbName;
47 $success = false;
49 if ( "" != $dbName ) {
50 # start a database connection
51 @$this->mConn = pg_connect("host=$server dbname=$dbName user=$user password=$password");
52 if ( $this->mConn == false ) {
53 wfDebug( "DB connection error\n" );
54 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
55 wfDebug( $this->lastError()."\n" );
56 } else {
57 $this->mOpened = true;
60 return $this->mConn;
63 # Closes a database connection, if it is open
64 # Returns success, true if already closed
65 function close()
67 $this->mOpened = false;
68 if ( $this->mConn ) {
69 return pg_close( $this->mConn );
70 } else {
71 return true;
75 function doQuery( $sql ) {
76 return pg_query( $this->mConn , $sql);
79 function queryIgnore( $sql, $fname = "" ) {
80 return $this->query( $sql, $fname, true );
83 function freeResult( $res ) {
84 if ( !@pg_free_result( $res ) ) {
85 wfDebugDieBacktrace( "Unable to free PostgreSQL result\n" );
88 function fetchObject( $res ) {
89 @$row = pg_fetch_object( $res );
90 # FIXME: HACK HACK HACK HACK debug
92 # TODO:
93 # hashar : not sure if the following test really trigger if the object
94 # fetching failled.
95 if( pg_last_error($this->mConn) ) {
96 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
98 return $row;
101 function fetchRow( $res ) {
102 @$row = pg_fetch_array( $res );
103 if( pg_last_error($this->mConn) ) {
104 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
106 return $row;
109 function numRows( $res ) {
110 @$n = pg_num_rows( $res );
111 if( pg_last_error($this->mConn) ) {
112 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn) ) );
114 return $n;
116 function numFields( $res ) { return pg_num_fields( $res ); }
117 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
119 # This must be called after nextSequenceVal
120 function insertId() {
121 return $this->mInsertId;
124 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
125 function lastError() { return pg_last_error(); }
126 function lastErrno() { return 1; }
128 function affectedRows() {
129 return pg_affected_rows( $this->mLastResult );
132 # Returns information about an index
133 # If errors are explicitly ignored, returns NULL on failure
134 function indexInfo( $table, $index, $fname = "Database::indexExists" )
136 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
137 $res = $this->query( $sql, $fname );
138 if ( !$res ) {
139 return NULL;
142 while ( $row = $this->fetchObject( $res ) ) {
143 if ( $row->Key_name == $index ) {
144 return $row;
147 return false;
150 function fieldInfo( $table, $field )
152 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
154 $res = $this->query( "SELECT * FROM '$table' LIMIT 1" );
155 $n = pg_num_fields( $res );
156 for( $i = 0; $i < $n; $i++ ) {
157 // FIXME
158 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
159 $meta = mysql_fetch_field( $res, $i );
160 if( $field == $meta->name ) {
161 return $meta;
164 return false;*/
167 function insertArray( $table, $a, $fname = "Database::insertArray", $options = array() ) {
168 # PostgreSQL doesn't support options
169 # We have a go at faking one of them
170 # TODO: DELAYED, LOW_PRIORITY
172 # IGNORE is performed using single-row inserts, ignoring errors in each
173 if ( in_array( 'IGNORE', $options ) ) {
174 # FIXME: need some way to distiguish between key collision and other types of error
175 $oldIgnore = $this->ignoreErrors( true );
176 if ( !is_array( reset( $a ) ) ) {
177 $a = array( $a );
179 foreach ( $a as $row ) {
180 parent::insertArray( $table, $row, $fname, array() );
182 $this->ignoreErrors( $oldIgnore );
183 $retVal = true;
184 } else {
185 $retVal = parent::insertArray( $table, $a, $fname, array() );
187 return $retVal;
190 function startTimer( $timeout )
192 global $IP;
193 wfDebugDieBacktrace( "Database::startTimer() error : mysql_thread_id() not implemented for postgre" );
194 /*$tid = mysql_thread_id( $this->mConn );
195 exec( "php $IP/killthread.php $timeout $tid &>/dev/null &" );*/
198 function tableName( $name ) {
199 # First run any transformations from the parent object
200 $name = parent::tableName( $name );
202 # Now quote PG reserved keywords
203 switch( $name ) {
204 case 'user':
205 return '"user"';
206 case 'old':
207 return '"old"';
208 default:
209 return $name;
213 function strencode( $s ) {
214 return pg_escape_string( $s );
217 # Return the next in a sequence, save the value for retrieval via insertId()
218 function nextSequenceValue( $seqName ) {
219 $value = $this->getField(""," nextval('" . $seqName . "')");
220 $this->mInsertId = $value;
221 return $value;
224 # USE INDEX clause
225 # PostgreSQL doesn't have them and returns ""
226 function useIndexClause( $index ) {
227 return '';
230 # REPLACE query wrapper
231 # PostgreSQL simulates this with a DELETE followed by INSERT
232 # $row is the row to insert, an associative array
233 # $uniqueIndexes is an array of indexes. Each element may be either a
234 # field name or an array of field names
236 # It may be more efficient to leave off unique indexes which are unlikely to collide.
237 # However if you do this, you run the risk of encountering errors which wouldn't have
238 # occurred in MySQL
239 function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
240 $table = $this->tableName( $table );
242 # Single row case
243 if ( !is_array( reset( $rows ) ) ) {
244 $rows = array( $rows );
247 foreach( $rows as $row ) {
248 # Delete rows which collide
249 if ( $uniqueIndexes ) {
250 $sql = "DELETE FROM $table WHERE (";
251 $first = true;
252 foreach ( $uniqueIndexes as $index ) {
253 if ( $first ) {
254 $first = false;
255 } else {
256 $sql .= ") OR (";
258 if ( is_array( $index ) ) {
259 $first2 = true;
260 $sql .= "(";
261 foreach ( $index as $col ) {
262 if ( $first2 ) {
263 $first2 = false;
264 } else {
265 $sql .= " AND ";
267 $sql .= "$col=" . $this->addQuotes( $row[$col] )
269 } else {
270 $sql .= "$index=" . $this->addQuotes( $row[$index] );
273 $sql .= ")";
274 $this->query( $sql, $fname );
277 # Now insert the row
278 $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
279 $this->makeList( $row, LIST_COMMA ) . ')';
280 $this->query( $sql, $fname );
284 # DELETE where the condition is a join
285 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
286 if ( !$conds ) {
287 wfDebugDieBacktrace( 'Database::deleteJoin() called with empty $conds' );
290 $delTable = $this->tableName( $delTable );
291 $joinTable = $this->tableName( $joinTable );
292 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
293 if ( $conds != '*' ) {
294 $sql .= "WHERE " . $this->makeList( $conds, LIST_AND );
296 $sql .= ")";
298 $this->query( $sql, $fname );
301 # Returns the size of a text field, or -1 for "unlimited"
302 function textFieldSize( $table, $field ) {
303 $table = $this->tableName( $table );
304 $res = $this->query( "SELECT $field FROM $table LIMIT 1", "Database::textFieldLength" );
305 $size = pg_field_size( $res, 0 );
306 $this->freeResult( $res );
307 return $size;
310 function lowPriorityOption() {
311 return '';
314 function limitResult($limit,$offset) {
315 return " LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
318 # FIXME: actually detecting deadlocks might be nice
319 function wasDeadlock() {
320 return false;
324 # Just an alias.
325 class DatabasePostgreSQL extends DatabasePgsql {