Merge "rdbms: remove various deprecated methods"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMssql.php
index 50aaff2..db029a3 100644 (file)
@@ -59,10 +59,6 @@ class DatabaseMssql extends Database {
        /** @var string[] */
        protected $ignoreErrors = [];
 
-       public function implicitGroupby() {
-               return false;
-       }
-
        public function implicitOrderby() {
                return false;
        }
@@ -79,53 +75,50 @@ class DatabaseMssql extends Database {
        }
 
        protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
-               // Test for driver support, to avoid suppressed fatal error
                if ( !function_exists( 'sqlsrv_connect' ) ) {
                        throw new DBConnectionError(
                                $this,
-                               "Microsoft SQL Server Native (sqlsrv) functions missing.
-                               You can download the driver from: http://go.microsoft.com/fwlink/?LinkId=123470\n"
+                               "Microsoft SQL Server Native (sqlsrv) functions missing.\n
+                               You can download the driver from: http://go.microsoft.com/fwlink/?LinkId=123470"
                        );
                }
 
                $this->close();
+
+               if ( $schema !== null ) {
+                       throw $this->newExceptionAfterConnectError( "Got schema '$schema'; not supported." );
+               }
+
                $this->server = $server;
                $this->user = $user;
                $this->password = $password;
 
                $connectionInfo = [];
-
-               if ( $dbName != '' ) {
+               if ( strlen( $dbName ) ) {
                        $connectionInfo['Database'] = $dbName;
                }
-
-               // Decide which auth scenerio to use
-               // if we are using Windows auth, then don't add credentials to $connectionInfo
                if ( !$this->useWindowsAuth ) {
                        $connectionInfo['UID'] = $user;
                        $connectionInfo['PWD'] = $password;
                }
 
                AtEase::suppressWarnings();
-               $this->conn = sqlsrv_connect( $server, $connectionInfo );
+               $this->conn = sqlsrv_connect( $server, $connectionInfo ) ?: null;
                AtEase::restoreWarnings();
 
-               if ( $this->conn === false ) {
-                       $error = $this->lastError();
-                       $this->connLogger->error(
-                               "Error connecting to {db_server}: {error}",
-                               $this->getLogContext( [ 'method' => __METHOD__, 'error' => $error ] )
-                       );
-                       throw new DBConnectionError( $this, $error );
+               if ( !$this->conn ) {
+                       throw $this->newExceptionAfterConnectError( $this->lastError() );
                }
 
-               $this->currentDomain = new DatabaseDomain(
-                       ( $dbName != '' ) ? $dbName : null,
-                       null,
-                       $tablePrefix
-               );
-
-               return (bool)$this->conn;
+               try {
+                       $this->currentDomain = new DatabaseDomain(
+                               strlen( $dbName ) ? $dbName : null,
+                               null,
+                               $tablePrefix
+                       );
+               } catch ( Exception $e ) {
+                       throw $this->newExceptionAfterConnectError( $e->getMessage() );
+               }
        }
 
        /**
@@ -229,11 +222,7 @@ class DatabaseMssql extends Database {
        }
 
        public function freeResult( $res ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $res = $res->result;
-               }
-
-               sqlsrv_free_stmt( $res );
+               sqlsrv_free_stmt( ResultWrapper::unwrap( $res ) );
        }
 
        /**
@@ -258,12 +247,9 @@ class DatabaseMssql extends Database {
         * @return int
         */
        public function numRows( $res ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $res = $res->result;
-               }
+               $res = ResultWrapper::unwrap( $res );
 
                $ret = sqlsrv_num_rows( $res );
-
                if ( $ret === false ) {
                        // we cannot get an amount of rows from this cursor type
                        // has_rows returns bool true/false if the result has rows
@@ -278,11 +264,7 @@ class DatabaseMssql extends Database {
         * @return int
         */
        public function numFields( $res ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $res = $res->result;
-               }
-
-               return sqlsrv_num_fields( $res );
+               return sqlsrv_num_fields( ResultWrapper::unwrap( $res ) );
        }
 
        /**
@@ -291,11 +273,7 @@ class DatabaseMssql extends Database {
         * @return int
         */
        public function fieldName( $res, $n ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $res = $res->result;
-               }
-
-               return sqlsrv_field_metadata( $res )[$n]['Name'];
+               return sqlsrv_field_metadata( ResultWrapper::unwrap( $res ) )[$n]['Name'];
        }
 
        /**
@@ -730,7 +708,7 @@ class DatabaseMssql extends Database {
                        }
                        $this->scrollableCursor = true;
 
-                       if ( $ret instanceof ResultWrapper && !is_null( $identity ) ) {
+                       if ( $ret instanceof IResultWrapper && !is_null( $identity ) ) {
                                // Then we want to get the identity column value we were assigned and save it off
                                $row = $ret->fetchObject();
                                if ( is_object( $row ) ) {
@@ -1180,7 +1158,10 @@ class DatabaseMssql extends Database {
 
        protected function doSelectDomain( DatabaseDomain $domain ) {
                if ( $domain->getSchema() !== null ) {
-                       throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
+                       throw new DBExpectedError(
+                               $this,
+                               __CLASS__ . ": domain '{$domain->getId()}' has a schema component"
+                       );
                }
 
                $database = $domain->getDatabase();