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;
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" );
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 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
95 # hashar : not sure if the following test really trigger if the object
97 if( pg_last_error($this->mConn
) ) {
98 wfDebugDieBacktrace( "SQL error: " . htmlspecialchars( pg_last_error($this->mConn
) ) );
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
) ) );
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
) ) );
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 );
144 while ( $row = $this->fetchObject( $res ) ) {
145 if ( $row->Key_name
== $index ) {
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++ ) {
160 wfDebugDieBacktrace( "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" );
161 $meta = mysql_fetch_field( $res, $i );
162 if( $field == $meta->name ) {
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 ) ) ) {
181 foreach ( $a as $row ) {
182 parent
::insertArray( $table, $row, $fname, array() );
184 $this->ignoreErrors( $oldIgnore );
187 $retVal = parent
::insertArray( $table, $a, $fname, array() );
192 function startTimer( $timeout )
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
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;
227 # PostgreSQL doesn't have them and returns ""
228 function useIndexClause( $index ) {
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
241 function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
242 $table = $this->tableName( $table );
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 (";
254 foreach ( $uniqueIndexes as $index ) {
260 if ( is_array( $index ) ) {
263 foreach ( $index as $col ) {
269 $sql .= "$col=" . $this->addQuotes( $row[$col] );
272 $sql .= "$index=" . $this->addQuotes( $row[$index] );
276 $this->query( $sql, $fname );
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" ) {
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
);
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 );
312 function lowPriorityOption() {
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() {
327 class DatabasePostgreSQL
extends DatabasePgsql
{