* some comments (using # instead of /* */ )
[lhc/web/wiklou.git] / includes / DatabasePostgreSQL.php
index 1b22a8e..66c93d6 100644 (file)
 #
 # Hashar
 
+require_once( "Database.php" );
+
 class DatabasePgsql extends Database {
        var $mInsertId = NULL;
 
        function DatabasePgsql($server = false, $user = false, $password = false, $dbName = false, 
-               $failFunction = false, $debug = false, $bufferResults = true, $ignoreErrors = false)
+               $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
        {
-               Database::Database( $server, $user, $password, $dbName, $failFunction, $debug, 
-                 $bufferResults, $ignoreErrors );
+               Database::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
        }
 
-       /* static */ function newFromParams( $server, $user, $password, $dbName, 
-               $failFunction = false, $debug = false, $bufferResults = true, $ignoreErrors = false )
+       /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false, 
+               $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
        {
-               return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $debug, 
-                 $bufferResults, $ignoreErrors );
+               return new DatabasePgsql( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
        }
 
        # Usually aborts on failure
@@ -74,53 +74,10 @@ class DatabasePgsql extends Database {
                }
        }
        
-       # Usually aborts on failure
-       # If errors are explicitly ignored, returns success
-       function query( $sql, $fname = "", $tempIgnore = false )
-       {
-               global $wgProfiling;
-               
-               if ( $wgProfiling ) {
-                       # generalizeSQL will probably cut down the query to reasonable
-                       # logging size most of the time. The substr is really just a sanity check.
-                       $profName = "query: " . substr( Database::generalizeSQL( $sql ), 0, 255 ); 
-                       wfProfileIn( $profName );
-               }
-               
-               $this->mLastQuery = $sql;
-               
-               if ( $this->mDebug ) {
-                       $sqlx = substr( $sql, 0, 500 );
-                       $sqlx = wordwrap(strtr($sqlx,"\t\n","  "));
-                       wfDebug( "SQL: $sqlx\n" );
-               }
-
-               $ret = pg_query( $this->mConn , $sql);
-               $this->mLastResult = $ret;
-               if ( false == $ret ) {
-                       // Ignore errors during error handling to prevent infinite recursion
-                       $ignore = $this->setIgnoreErrors( true );
-                       $error = pg_last_error( $this->mConn );
-                       // TODO FIXME : no error number function in postgre
-                       // $errno = mysql_errno( $this->mConn );
-                       if( $ignore || $tempIgnore ) {
-                               wfDebug("SQL ERROR (ignored): " . $error . "\n");
-                       } else {
-                               wfDebug("SQL ERROR: " . $error . "\n");
-                               if ( $this->mOut ) {
-                                       // this calls wfAbruptExit()
-                                       $this->mOut->databaseError( $fname, $sql, $error, 0 );                          
-                               }
-                       }
-                       $this->setIgnoreErrors( $ignore );
-               }
-               
-               if ( $wgProfiling ) {
-                       wfProfileOut( $profName );
-               }
-               return $ret;
+       function doQuery( $sql ) {
+               return pg_query( $this->mConn , $sql);
        }
-       
+               
        function queryIgnore( $sql, $fname = "" ) {
                return $this->query( $sql, $fname, true );
        }
@@ -168,6 +125,8 @@ class DatabasePgsql extends Database {
 
        function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
        function lastError() { return pg_last_error(); }
+       function lastErrno() { return 1; }
+
        function affectedRows() { 
                return pg_affected_rows( $this->mLastResult ); 
        }
@@ -209,14 +168,23 @@ class DatabasePgsql extends Database {
 
        function insertArray( $table, $a, $fname = "Database::insertArray", $options = array() ) {
                # PostgreSQL doesn't support options
-               # We have a go at faking some of them
+               # We have a go at faking one of them
+               # TODO: DELAYED, LOW_PRIORITY 
+
+               # IGNORE is performed using single-row inserts, ignoring errors in each
                if ( in_array( 'IGNORE', $options ) ) {
-                       $ignore = true;
-                       $oldIgnore = $this->setIgnoreErrors( true );
-               }
-               $retVal = parent::insertArray( $table, $a, $fname, array() );
-               if ( $ignore ) {
-                       $this->setIgnoreErrors( $oldIgnore );
+                       # FIXME: need some way to distiguish between key collision and other types of error
+                       $oldIgnore = $this->ignoreErrors( true );
+                       if ( !is_array( reset( $a ) ) ) {
+                               $a = array( $a );
+                       }
+                       foreach ( $a as $row ) {
+                               parent::insertArray( $table, $row, $fname, array() );
+                       }
+                       $this->ignoreErrors( $oldIgnore );
+                       $retVal = true;
+               } else {
+                       $retVal = parent::insertArray( $table, $a, $fname, array() );
                }
                return $retVal;
        }
@@ -273,54 +241,46 @@ class DatabasePgsql extends Database {
        function replace( $table, $uniqueIndexes, $rows, $fname = "Database::replace" ) {
                $table = $this->tableName( $table );
 
-               # Delete rows which collide
-               if ( $uniqueIndexes ) {
-                       $sql = "DELETE FROM $table WHERE (";
-                       $first = true;
-                       foreach ( $uniqueIndexes as $index ) {
-                               if ( $first ) {
-                                       $first = false;
-                               } else {
-                                       $sql .= ") OR (";
-                               }
-                               if ( is_array( $col ) ) {
-                                       $first2 = true;
-                                       $sql .= "(";
-                                       foreach ( $index as $col ) {
-                                               if ( $first2 ) { 
-                                                       $first2 = false;
-                                               } else {
-                                                       $sql .= " AND ";
+               # Single row case
+               if ( !is_array( reset( $rows ) ) ) {
+                       $rows = array( $rows );
+               }
+
+               foreach( $rows as $row ) {
+                       # Delete rows which collide
+                       if ( $uniqueIndexes ) {
+                               $sql = "DELETE FROM $table WHERE (";
+                               $first = true;
+                               foreach ( $uniqueIndexes as $index ) {
+                                       if ( $first ) {
+                                               $first = false;
+                                       } else {
+                                               $sql .= ") OR (";
+                                       }
+                                       if ( is_array( $index ) ) {
+                                               $first2 = true;
+                                               $sql .= "(";
+                                               foreach ( $index as $col ) {
+                                                       if ( $first2 ) { 
+                                                               $first2 = false;
+                                                       } else {
+                                                               $sql .= " AND ";
+                                                       }
+                                                       $sql .= "$col=" . $this->addQuotes( $row[$col] );
                                                }
-                                               $sql .= "$col = " $this->strencode
-                                               
-                               if ( $first ) {
-                                       $first = false;
                                } else {
-                                       $sql .= "OR ";
+                                               $sql .= "$index=" . $this->addQuotes( $row[$index] );
                                }
-                               $sql .= "$col IN (";
-                               $indexValues = array();
-                               foreach ( $rows as $row ) {
-                                       $indexValues[] = $row[$col];
                                }
-                               $sql .= $this->makeList( $indexValues, LIST_COMMA ) . ") ";
+                               $sql .= ")";
+                               $this->query( $sql, $fname );
                        }
-                       $this->query( $sql, $fname );
-               }
 
-               # Now insert the rows
-               $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $rows[0] ) ) .") VALUES ";
-               $first = true;
-               foreach ( $rows as $row ) {
-                       if ( $first ) {
-                               $first = false;
-                       } else {
-                               $sql .= ",";
-                       }
-                       $sql .= "(" . $this->makeList( $row, LIST_COMMA ) . ")";
+                       # Now insert the row
+                       $sql = "INSERT INTO $table (" . $this->makeList( array_flip( $row ) ) .') VALUES (' .
+                               $this->makeList( $row, LIST_COMMA ) . ')';
+                       $this->query( $sql, $fname );
                }
-               $this->query( $sql, $fname );
        }
 
        # DELETE where the condition is a join
@@ -352,10 +312,24 @@ class DatabasePgsql extends Database {
        function lowPriorityOption() {
                return '';
        }
+
+       function limitResult($limit,$offset) {
+               return " LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
+       }
+
+       # FIXME: actually detecting deadlocks might be nice
+       function wasDeadlock() {
+               return false;
+       }
+
+        # Return DB-style timestamp used for MySQL schema
+        function timestamp( $ts=0 ) {
+                return wfTimestamp(TS_DB,$ts);
+        }
 }
 
-function wfLimitResult( $limit, $offset ) {
-        return " LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
+# Just an alias.
+class DatabasePostgreSQL extends DatabasePgsql {
 }
 
 ?>