? 903110.patch
Index: includes/pager.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/pager.inc,v
retrieving revision 1.81
diff -u -p -r1.81 pager.inc
--- includes/pager.inc	4 Aug 2010 03:46:16 -0000	1.81
+++ includes/pager.inc	9 Sep 2010 21:13:50 -0000
@@ -82,16 +82,16 @@ class PagerDefault extends SelectQueryEx
 
   /**
    * Ensure that there is an element associated with this query.
+   * If an element was not specified previously, then the value of the
+   * $maxElement counter is taken, after which the counter is incremented.
    *
-   * After running this query, access $this->element to get the element for this
+   * After running this method, access $this->element to get the element for this
    * query.
    */
   protected function ensureElement() {
-    if (!empty($this->element)) {
-      return;
+    if (!isset($this->element)) {
+      $this->element = self::$maxElement++;
     }
-
-    $this->element = self::$maxElement++;
   }
 
   /**
@@ -149,10 +149,19 @@ class PagerDefault extends SelectQueryEx
    * whatever reason you want to explicitly define an element for a given query,
    * you may do so here.
    *
+   * Setting the element here also increments the static $maxElement counter,
+   * which is used for determining the $element when there's none specified.
+   *
+   * Note that no collision detection is done when setting an element ID
+   * explicitly, so it is possible for two pagers to end up using the same ID.
+   * 
    * @param $element
    */
   public function element($element) {
     $this->element = $element;
+    if ($element >= self::$maxElement) {
+      self::$maxElement = $element + 1;
+    }
     return $this;
   }
 }
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.100
diff -u -p -r1.100 database_test.test
--- modules/simpletest/tests/database_test.test	3 Sep 2010 19:06:55 -0000	1.100
+++ modules/simpletest/tests/database_test.test	9 Sep 2010 21:13:58 -0000
@@ -2216,6 +2216,43 @@ class DatabaseSelectPagerDefaultTestCase
       ->fetchCol();
     $this->assertEqual($ages, array('George', 'Ringo'), t('Pager query with having expression returned the correct ages.'));
   }
+  
+  /**
+  * Confirm that every pager gets a valid non-overlaping element ID.
+  */
+  function testElementNumbers() {
+    $_GET['page'] = '3, 2, 1, 0';
+    
+    $name = db_select('test', 't')->extend('PagerDefault')
+      ->element(2)
+      ->fields('t', array('name'))
+      ->orderBy('age')
+      ->limit(1)
+      ->execute()
+      ->fetchField();
+    $this->assertEqual($name, 'Paul', t('Pager query #1 with a specified element ID returned the correct results.'));
+    
+    // Setting an element smaller than the previous one
+    // should not overwrite the pager $maxElement with a smaller value.
+    $name = db_select('test', 't')->extend('PagerDefault')
+      ->element(1)
+      ->fields('t', array('name'))
+      ->orderBy('age')
+      ->limit(1)
+      ->execute()
+      ->fetchField();
+    $this->assertEqual($name, 'George', t('Pager query #2 with a specified element ID returned the correct results.'));
+  
+    $name = db_select('test', 't')->extend('PagerDefault')
+      ->fields('t', array('name'))
+      ->orderBy('age')
+      ->limit(1)
+      ->execute()
+      ->fetchField();
+    $this->assertEqual($name, 'John', t('Pager query #3 with a generated element ID returned the correct results.'));
+    
+    unset($_GET['page']);
+  }
 }
 
 
