[FEATURE] Extbase: Add between() operator to Query object 13/42813/2
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 21 Aug 2015 13:17:56 +0000 (15:17 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Tue, 8 Sep 2015 07:56:11 +0000 (09:56 +0200)
Support for ``between`` has been added to the Extbase Query object.
As there is no performance advantage to using BETWEEN on the DBMS
side (the query optimizers converts it to `min <= expr AND expr <= max)`
this method replicates the DBMS behaviour by building a logical AND
condition that has the advantage of working on all DBMS.

Resolves: #47812
Releases: master
Change-Id: Ic3b416515eedc651faf69de1db21eab288a8ad33
Reviewed-on: http://review.typo3.org/42813
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-47812-QuerySupportForBETWEENAdded.rst [new file with mode: 0644]
typo3/sysext/extbase/Classes/Persistence/Generic/Query.php
typo3/sysext/extbase/Tests/Functional/Persistence/OperatorTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-47812-QuerySupportForBETWEENAdded.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-47812-QuerySupportForBETWEENAdded.rst
new file mode 100644 (file)
index 0000000..cd1e470
--- /dev/null
@@ -0,0 +1,19 @@
+=================================================
+Feature: #47812 - Query support for BETWEEN added
+=================================================
+
+Description
+===========
+
+Support for ``between`` has been added to the Extbase ``Query`` object. As there is no performance
+advantage to using BETWEEN on the DBMS side (the optimizer converts it to ``(min <= expr AND expr <= max)``
+this function replicates the DBMS behaviour by building a logical AND condition that has the advantage
+of working on all DBMS.
+
+Example:
+
+.. code-block:: php
+
+       $query->matching(
+               $query->between('uid', 3, 5)
+       );
index 4e284aa..2566f2e 100644 (file)
@@ -553,6 +553,23 @@ class Query implements QueryInterface {
        }
 
        /**
+        * Returns a greater than or equal criterion used for matching objects against a query
+        *
+        * @param string $propertyName The name of the property to compare against
+        * @param $operandLower The value of the lower boundary to compare against
+        * @param $operandUpper The value of the upper boundary to compare against
+        * @return \TYPO3\CMS\Extbase\Persistence\Generic\Qom\AndInterface
+        * @throws \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidNumberOfConstraintsException
+        * @api
+        */
+       public function between($propertyName, $operandLower, $operandUpper) {
+               return $this->logicalAnd(
+                       $this->greaterThanOrEqual($propertyName, $operandLower),
+                       $this->lessThanOrEqual($propertyName, $operandUpper)
+               );
+       }
+
+       /**
         * @return void
         */
        public function __wakeup() {
index b5b44f8..f80f5c3 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
  */
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Persistence\QueryInterface;
 
 class OperatorTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
 
@@ -87,4 +88,22 @@ class OperatorTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
                $this->assertSame(2, $query->count());
        }
 
+       /**
+        * @test
+        */
+       public function betweenSetsBoundariesCorrectly() {
+               $query = $this->postRepository->createQuery();
+               $query->setOrderings(array('uid' => QueryInterface::ORDER_ASCENDING));
+
+               $query->matching(
+                       $query->between('uid', 3, 5)
+               );
+
+               $result = array_map(
+                       function($row) { return $row['uid']; },
+                       $query->execute(TRUE)
+               );
+               $this->assertEquals(array(3,4,5), $result);
+       }
+
 }