Moved to extensions
[lhc/web/wiklou.git] / includes / Database.php
index 0deaf93..a43f5b0 100644 (file)
@@ -274,17 +274,17 @@ class Database {
                
                $this->mLastQuery = $sql;
                
-               if ( $this->debug() ) {
-                       $sqlx = substr( $sql, 0, 500 );
-                       $sqlx = wordwrap(strtr($sqlx,"\t\n",'  '));
-                       wfDebug( "SQL: $sqlx\n" );
-               }
                # Add a comment for easy SHOW PROCESSLIST interpretation
                if ( $fname ) {
                        $commentedSql = "/* $fname */ $sql";
                } else {
                        $commentedSql = $sql;
                }
+               if ( $this->debug() ) {
+                       $sqlx = substr( $commentedSql, 0, 500 );
+                       $sqlx = wordwrap(strtr($sqlx,"\t\n",'  '));
+                       wfDebug( "SQL: $sqlx\n" );
+               }
                
                # If DBO_TRX is set, start a transaction
                if ( ( $this->mFlags & DBO_TRX ) && !$this->trxLevel() && $sql != 'BEGIN' ) {
@@ -390,7 +390,8 @@ class Database {
        /**
         * Prepare & execute an SQL statement, quoting and inserting arguments
         * in the appropriate places.
-        * @param 
+        * @param string $query
+        * @param string $args (default null)
         */
        function safeQuery( $query, $args = null ) {
                $prepared = $this->prepare( $query, 'Database::safeQuery' );
@@ -510,8 +511,8 @@ class Database {
         * The value inserted should be fetched from nextSequenceValue()
         *
         * Example:
-        * $id = $dbw->nextSequenceValue('cur_cur_id_seq');
-        * $dbw->insert('cur',array('cur_id' => $id));
+        * $id = $dbw->nextSequenceValue('page_page_id_seq');
+        * $dbw->insert('page',array('page_id' => $id));
         * $id = $dbw->insertId();
         */
        function insertId() { return mysql_insert_id( $this->mConn ); }
@@ -526,14 +527,29 @@ class Database {
         * Get the last error number
         * See mysql_errno()
         */
-       function lastErrno() { return mysql_errno(); }
+       function lastErrno() { 
+               if ( $this->mConn ) {
+                       return mysql_errno( $this->mConn ); 
+               } else {
+                       return mysql_errno();
+               }
+       }
        
        /**
         * Get a description of the last error
         * See mysql_error() for more details
         */
-       function lastError() { return mysql_error(); }
-       
+       function lastError() { 
+               if ( $this->mConn ) {
+                       $error = mysql_error( $this->mConn ); 
+               } else {
+                       $error = mysql_error();
+               }
+               if( $error ) {
+                       $error .= ' (' . $this->mServer . ')';
+               }
+               return $error;
+       }       
        /**
         * Get the number of rows affected by the last write query
         * See mysql_affected_rows() for more details
@@ -1105,7 +1121,7 @@ class Database {
        }
 
        /**
-        * @return string Always return 'LOW_PRIORITY'
+        * @return string Returns the text of the low priority option if it is supported, or a blank string otherwise
         */
        function lowPriorityOption() {
                return 'LOW_PRIORITY';
@@ -1133,10 +1149,15 @@ class Database {
         * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
         * Source items may be literals rather than field names, but strings should be quoted with Database::addQuotes()
         * $conds may be "*" to copy the whole table
+        * srcTable may be an array of tables.
         */
        function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'Database::insertSelect' ) {
                $destTable = $this->tableName( $destTable );
-               $srcTable = $this->tableName( $srcTable );
+                if( is_array( $srcTable ) ) {
+                        $srcTable =  implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
+               } else { 
+                       $srcTable = $this->tableName( $srcTable );
+               }
                $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
                        ' SELECT ' . implode( ',', $varMap ) . 
                        " FROM $srcTable";
@@ -1196,7 +1217,7 @@ class Database {
                $this->query( 'BEGIN', $myFname );
                $args = func_get_args();
                $function = array_shift( $args );
-               $oldIgnore = $dbw->ignoreErrors( true );
+               $oldIgnore = $this->ignoreErrors( true );
                $tries = DEADLOCK_TRIES;
                if ( is_array( $function ) ) {
                        $fname = $function[0];
@@ -1210,14 +1231,14 @@ class Database {
                        $sql = $this->lastQuery();
                        
                        if ( $errno ) {
-                               if ( $dbw->wasDeadlock() ) {
+                               if ( $this->wasDeadlock() ) {
                                        # Retry
                                        usleep( mt_rand( DEADLOCK_DELAY_MIN, DEADLOCK_DELAY_MAX ) );
                                } else {
-                                       $dbw->reportQueryError( $error, $errno, $sql, $fname );
+                                       $this->reportQueryError( $error, $errno, $sql, $fname );
                                }
                        }
-               } while( $dbw->wasDeadlock && --$tries > 0 );
+               } while( $this->wasDeadlock() && --$tries > 0 );
                $this->ignoreErrors( $oldIgnore );
                if ( $tries <= 0 ) {
                        $this->query( 'ROLLBACK', $myFname );