Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMssql.php
index a6027e6..2aefd5f 100644 (file)
@@ -157,7 +157,6 @@ class DatabaseMssql extends Database {
        /**
         * @param string $sql
         * @return bool|MssqlResultWrapper|resource
-        * @throws DBUnexpectedError
         */
        protected function doQuery( $sql ) {
                // several extensions seem to think that all databases support limits
@@ -1168,8 +1167,18 @@ class DatabaseMssql extends Database {
        }
 
        protected function doSelectDomain( DatabaseDomain $domain ) {
-               $encDatabase = $this->addIdentifierQuotes( $domain->getDatabase() );
-               $this->query( "USE $encDatabase" );
+               if ( $domain->getSchema() !== null ) {
+                       throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
+               }
+
+               $database = $domain->getDatabase();
+               if ( $database !== $this->getDBname() ) {
+                       $encDatabase = $this->addIdentifierQuotes( $database );
+                       $res = $this->doQuery( "USE $encDatabase" );
+                       if ( !$res ) {
+                               throw new DBExpectedError( $this, "Could not select database '$database'." );
+                       }
+               }
                // Update that domain fields on success (no exception thrown)
                $this->currentDomain = $domain;
 
@@ -1319,8 +1328,9 @@ class DatabaseMssql extends Database {
 
        /**
         * @param string $name
-        * @param string $format
-        * @return string
+        * @param string $format One of "quoted" (default), "raw", or "split".
+        * @return string|array When the requested $format is "split", a list of database, schema, and
+        *  table name is returned. Database and schema can be `false`.
         */
        function tableName( $name, $format = 'quoted' ) {
                # Replace reserved words with better ones
@@ -1335,18 +1345,17 @@ class DatabaseMssql extends Database {
        /**
         * call this instead of tableName() in the updater when renaming tables
         * @param string $name
-        * @param string $format One of quoted, raw, or split
-        * @return string
+        * @param string $format One of "quoted" (default), "raw", or "split".
+        * @return string|array When the requested $format is "split", a list of database, schema, and
+        *  table name is returned. Database and schema can be `false`.
+        * @private
         */
        function realTableName( $name, $format = 'quoted' ) {
                $table = parent::tableName( $name, $format );
                if ( $format == 'split' ) {
                        // Used internally, we want the schema split off from the table name and returned
                        // as a list with 3 elements (database, schema, table)
-                       $table = explode( '.', $table );
-                       while ( count( $table ) < 3 ) {
-                               array_unshift( $table, false );
-                       }
+                       return array_pad( explode( '.', $table, 3 ), -3, false );
                }
                return $table;
        }
@@ -1403,6 +1412,9 @@ class DatabaseMssql extends Database {
                return "CAST( $field AS NVARCHAR )";
        }
 
+       public static function getAttributes() {
+               return [ self::ATTR_SCHEMAS_AS_TABLE_GROUPS => true ];
+       }
 }
 
 /**