Merge "Fix: numFields wrong for sqlite"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 21 May 2014 17:49:27 +0000 (17:49 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 21 May 2014 17:49:27 +0000 (17:49 +0000)
includes/db/DatabaseSqlite.php
tests/phpunit/includes/db/DatabaseSqliteTest.php

index 468ed6d..3e063c6 100644 (file)
@@ -342,8 +342,13 @@ class DatabaseSqlite extends DatabaseBase {
         */
        function numFields( $res ) {
                $r = $res instanceof ResultWrapper ? $res->result : $res;
-
-               return is_array( $r ) ? count( $r[0] ) : 0;
+               if ( is_array($r) && count( $r ) > 0 ){
+                       // The size of the result array is twice the number of fields. (Bug: 65578)
+                       return count( $r[0] ) / 2 ;
+               } else {
+                       // If the result is empty return 0
+                       return 0;
+               }
        }
 
        /**
index b4c1953..88bf7d9 100644 (file)
@@ -431,4 +431,22 @@ class DatabaseSqliteTest extends MediaWikiTestCase {
                $row = $res->fetchRow();
                $this->assertFalse( (bool)$row['a'] );
        }
+
+       /**
+        * @covers DatabaseSqlite::numFields
+        */
+       public function testNumFields() {
+               $db = new DatabaseSqliteStandalone( ':memory:' );
+
+               $databaseCreation = $db->query( 'CREATE TABLE a ( a_1 )', __METHOD__ );
+               $this->assertInstanceOf( 'ResultWrapper', $databaseCreation, "Failed to create table a" );
+               $res = $db->select( 'a' , '*');
+               $this->assertEquals( 0,  $db->numFields($res), "expects to get 0 fields for an empty table" );
+               $insertion = $db->insert( 'a', array( 'a_1' => 10 ), __METHOD__ );
+               $this->assertTrue( $insertion, "Insertion failed" );
+               $res = $db->select( 'a' , '*');
+               $this->assertEquals( 1,  $db->numFields($res), "wrong number of fields" );
+
+               $this->assertTrue( $db->close(), "closing database" );
+       }
 }