Skip to content

Commit 8027c28

Browse files
committed
#14 - Add between expression
1 parent 99e1506 commit 8027c28

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace TgDatabase\Criterion;
4+
5+
use TgDatabase\Criteria;
6+
use TgDatabase\Criterion;
7+
8+
class BetweenExpression implements Criterion {
9+
10+
public function __construct($propertyName, $minValue, $maxValue, $ignoreCase = FALSE) {
11+
$this->propertyName = $propertyName;
12+
$this->minValue = $minValue;
13+
$this->maxValue = $maxValue;
14+
$this->ignoreCase = $ignoreCase;
15+
}
16+
17+
/**
18+
* Impose case-insensitive restriction.
19+
*/
20+
public function ignoreCase() {
21+
$this->ignoreCase = TRUE;
22+
return $this;
23+
}
24+
25+
/**
26+
* Render the SQL fragment.
27+
* @param Criteria $localCriteria - local criteria object (e.g. subquery)
28+
* @param Criteria $overallCriteria - overall criteria object
29+
* @return string - the SQL fragment representing this criterion.
30+
*/
31+
public function toSqlString($localCriteria, $overallCriteria) {
32+
$lower = $this->ignoreCase && is_string($this->minValue) && is_string($this->maxValue);
33+
34+
$rc = '';
35+
if ($lower) {
36+
$rc .= 'LOWER(';
37+
}
38+
$rc .= $overallCriteria->quoteName($localCriteria->getAlias(), $this->propertyName);
39+
if ($lower) {
40+
$rc .= ')';
41+
}
42+
$rc .= ' BETWEEN ';
43+
$rc .= $overallCriteria->prepareValue($this->minValue, $lower);
44+
$rc .= ' AND ';
45+
$rc .= $overallCriteria->prepareValue($this->maxValue, $lower);
46+
return $rc;
47+
}
48+
49+
}

src/TgDatabase/Restrictions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use TgDatabase\Criterion\InExpression;
1010
use TgDatabase\Criterion\NotInExpression;
1111
use TgDatabase\Criterion\PropertyExpression;
12+
use TgDatabase\Criterion\BetweenExpression;
1213

1314
/**
1415
* Provides the built-in citerions.
@@ -80,6 +81,13 @@ public static function le($propertyName, $value) {
8081
return new SimpleExpression($propertyName, $value, '<=');
8182
}
8283

84+
/**
85+
* Apply a "between" constraint to the named property.
86+
*/
87+
public static function between($propertyName, $minValue, $maxValue) {
88+
return new BetweenExpression($propertyName, $minValue, $maxValue);
89+
}
90+
8391
/**
8492
* Apply a "is null" constraint to the named property.
8593
*/

tests/TgDatabase/RestrictionsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public function testle(): void {
6666
$this->testSqlString('`aName` <= \'aValue\'', $expr);
6767
}
6868

69+
public function testBetween(): void {
70+
$expr = Restrictions::between('aName', 'aValue1', 'aValue2');
71+
$this->testSqlString('`aName` BETWEEN \'aValue1\' AND \'aValue2\'', $expr);
72+
}
73+
74+
public function testBetweenIgnoreCase(): void {
75+
$expr = Restrictions::between('aName', 'aValue1', 'aValue2')->ignoreCase();
76+
$this->testSqlString('LOWER(`aName`) BETWEEN \'avalue1\' AND \'avalue2\'', $expr);
77+
}
78+
6979
public function testIsNull(): void {
7080
$expr = Restrictions::isNull('aName');
7181
$this->testSqlString('`aName` IS NULL', $expr);

0 commit comments

Comments
 (0)