database: Throw exceptions when dead mysql DB handles are used instead of fatals
[lhc/web/wiklou.git] / includes / db / DatabaseMysqlBase.php
index c1f2969..5f27dd7 100644 (file)
@@ -104,9 +104,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                }
 
                if ( $dbName != '' ) {
-                       wfSuppressWarnings();
+                       MediaWiki\suppressWarnings();
                        $success = $this->selectDB( $dbName );
-                       wfRestoreWarnings();
+                       MediaWiki\restoreWarnings();
                        if ( !$success ) {
                                wfLogDBError(
                                        "Error selecting database {db_name} on server {db_server}",
@@ -132,6 +132,15 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                if ( is_string( $wgSQLMode ) ) {
                        $set[] = 'sql_mode = ' . $this->addQuotes( $wgSQLMode );
                }
+               // Set any custom settings defined by site config
+               // (e.g. https://dev.mysql.com/doc/refman/4.1/en/innodb-parameters.html)
+               foreach ( $this->mSessionVars as $var => $val ) {
+                       // Escape strings but not numbers to avoid MySQL complaining
+                       if ( !is_int( $val ) && !is_float( $val ) ) {
+                               $val = $this->addQuotes( $val );
+                       }
+                       $set[] = $this->addIdentifierQuotes( $var ) . ' = ' . $val;
+               }
 
                if ( $set ) {
                        // Use doQuery() to avoid opening implicit transactions (DBO_TRX)
@@ -194,9 +203,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $ok = $this->mysqlFreeResult( $res );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
                if ( !$ok ) {
                        throw new DBUnexpectedError( $this, "Unable to free MySQL result" );
                }
@@ -219,9 +228,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $row = $this->mysqlFetchObject( $res );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
 
                $errno = $this->lastErrno();
                // Unfortunately, mysql_fetch_object does not reset the last errno.
@@ -255,9 +264,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $row = $this->mysqlFetchArray( $res );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
 
                $errno = $this->lastErrno();
                // Unfortunately, mysql_fetch_array does not reset the last errno.
@@ -291,9 +300,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
                }
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $n = $this->mysqlNumRows( $res );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
 
                // Unfortunately, mysql_num_rows does not reset the last errno.
                // We are not checking for any errors here, since
@@ -404,12 +413,12 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        function lastError() {
                if ( $this->mConn ) {
                        # Even if it's non-zero, it can still be invalid
-                       wfSuppressWarnings();
+                       MediaWiki\suppressWarnings();
                        $error = $this->mysqlError( $this->mConn );
                        if ( !$error ) {
                                $error = $this->mysqlError();
                        }
-                       wfRestoreWarnings();
+                       MediaWiki\restoreWarnings();
                } else {
                        $error = $this->mysqlError();
                }
@@ -873,6 +882,10 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                return ( $row->lockstatus == 1 );
        }
 
+       public function namedLocksEnqueue() {
+               return true;
+       }
+
        /**
         * @param array $read
         * @param array $write
@@ -1045,6 +1058,28 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                        ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
        }
 
+       /**
+        * Get the underlying binding handle, mConn
+        *
+        * Makes sure that mConn is set (disconnects and ping() failure can unset it).
+        * This catches broken callers than catch and ignore disconnection exceptions.
+        * Unlike checking isOpen(), this is safe to call inside of open().
+        *
+        * @return resource|object
+        * @throws DBUnexpectedError
+        * @since 1.26
+        */
+       protected function getBindingHandle() {
+               if ( !$this->mConn ) {
+                       throw new DBUnexpectedError(
+                               $this,
+                               'DB connection was already closed or the connection dropped.'
+                       );
+               }
+
+               return $this->mConn;
+       }
+
        /**
         * @param string $oldName
         * @param string $newName
@@ -1188,7 +1223,8 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
  */
 class MySQLField implements Field {
        private $name, $tablename, $default, $max_length, $nullable,
-               $is_pk, $is_unique, $is_multiple, $is_key, $type, $binary;
+               $is_pk, $is_unique, $is_multiple, $is_key, $type, $binary,
+               $is_numeric, $is_blob, $is_unsigned, $is_zerofill;
 
        function __construct( $info ) {
                $this->name = $info->name;
@@ -1201,8 +1237,11 @@ class MySQLField implements Field {
                $this->is_multiple = $info->multiple_key;
                $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
                $this->type = $info->type;
-               $this->flags = $info->flags;
                $this->binary = isset( $info->binary ) ? $info->binary : false;
+               $this->is_numeric = isset( $info->numeric ) ? $info->numeric : false;
+               $this->is_blob = isset( $info->blob ) ? $info->blob : false;
+               $this->is_unsigned = isset( $info->unsigned ) ? $info->unsigned : false;
+               $this->is_zerofill = isset( $info->zerofill ) ? $info->zerofill : false;
        }
 
        /**
@@ -1252,15 +1291,39 @@ class MySQLField implements Field {
        }
 
        /**
-        * @return int
+        * @return bool
         */
-       function flags() {
-               return $this->flags;
-       }
-
        function isBinary() {
                return $this->binary;
        }
+
+       /**
+        * @return bool
+        */
+       function isNumeric() {
+               return $this->is_numeric;
+       }
+
+       /**
+        * @return bool
+        */
+       function isBlob() {
+               return $this->is_blob;
+       }
+
+       /**
+        * @return bool
+        */
+       function isUnsigned() {
+               return $this->is_unsigned;
+       }
+
+       /**
+        * @return bool
+        */
+       function isZerofill() {
+               return $this->is_zerofill;
+       }
 }
 
 class MySQLMasterPos implements DBMasterPos {