Skip to content

Commit a19a1d0

Browse files
authored
[3.8.x] AbstractPlatform - allow any string in interval for date interval expression (#6302)
<!-- Fill in the relevant information below to help triage your pull request. --> | Q | A |------------- | ----------- | Type | improvement #### Summary This change allows these methods to be called with just a `string` in ORM 3.0.x. See doctrine/orm#11243 (comment) DBAL 4.0.x already has `string` there. Also I'd like to note that I noticed that methods `getDateAddQuartersExpression` and `getDateSubQuartersExpression` aren't called from ORM. Shouldn't support for them be added?
1 parent de57e23 commit a19a1d0

File tree

6 files changed

+69
-52
lines changed

6 files changed

+69
-52
lines changed

src/Platforms/AbstractPlatform.php

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,8 +1342,8 @@ public function getDateDiffExpression($date1, $date2)
13421342
/**
13431343
* Returns the SQL to add the number of given seconds to a date.
13441344
*
1345-
* @param string $date
1346-
* @param int|numeric-string $seconds
1345+
* @param string $date
1346+
* @param int|string $seconds
13471347
*
13481348
* @return string
13491349
*
@@ -1365,8 +1365,8 @@ public function getDateAddSecondsExpression($date, $seconds)
13651365
/**
13661366
* Returns the SQL to subtract the number of given seconds from a date.
13671367
*
1368-
* @param string $date
1369-
* @param int|numeric-string $seconds
1368+
* @param string $date
1369+
* @param int|string $seconds
13701370
*
13711371
* @return string
13721372
*
@@ -1388,8 +1388,8 @@ public function getDateSubSecondsExpression($date, $seconds)
13881388
/**
13891389
* Returns the SQL to add the number of given minutes to a date.
13901390
*
1391-
* @param string $date
1392-
* @param int|numeric-string $minutes
1391+
* @param string $date
1392+
* @param int|string $minutes
13931393
*
13941394
* @return string
13951395
*
@@ -1411,8 +1411,8 @@ public function getDateAddMinutesExpression($date, $minutes)
14111411
/**
14121412
* Returns the SQL to subtract the number of given minutes from a date.
14131413
*
1414-
* @param string $date
1415-
* @param int|numeric-string $minutes
1414+
* @param string $date
1415+
* @param int|string $minutes
14161416
*
14171417
* @return string
14181418
*
@@ -1434,8 +1434,8 @@ public function getDateSubMinutesExpression($date, $minutes)
14341434
/**
14351435
* Returns the SQL to add the number of given hours to a date.
14361436
*
1437-
* @param string $date
1438-
* @param int|numeric-string $hours
1437+
* @param string $date
1438+
* @param int|string $hours
14391439
*
14401440
* @return string
14411441
*
@@ -1457,8 +1457,8 @@ public function getDateAddHourExpression($date, $hours)
14571457
/**
14581458
* Returns the SQL to subtract the number of given hours to a date.
14591459
*
1460-
* @param string $date
1461-
* @param int|numeric-string $hours
1460+
* @param string $date
1461+
* @param int|string $hours
14621462
*
14631463
* @return string
14641464
*
@@ -1480,8 +1480,8 @@ public function getDateSubHourExpression($date, $hours)
14801480
/**
14811481
* Returns the SQL to add the number of given days to a date.
14821482
*
1483-
* @param string $date
1484-
* @param int|numeric-string $days
1483+
* @param string $date
1484+
* @param int|string $days
14851485
*
14861486
* @return string
14871487
*
@@ -1503,8 +1503,8 @@ public function getDateAddDaysExpression($date, $days)
15031503
/**
15041504
* Returns the SQL to subtract the number of given days to a date.
15051505
*
1506-
* @param string $date
1507-
* @param int|numeric-string $days
1506+
* @param string $date
1507+
* @param int|string $days
15081508
*
15091509
* @return string
15101510
*
@@ -1526,8 +1526,8 @@ public function getDateSubDaysExpression($date, $days)
15261526
/**
15271527
* Returns the SQL to add the number of given weeks to a date.
15281528
*
1529-
* @param string $date
1530-
* @param int|numeric-string $weeks
1529+
* @param string $date
1530+
* @param int|string $weeks
15311531
*
15321532
* @return string
15331533
*
@@ -1549,8 +1549,8 @@ public function getDateAddWeeksExpression($date, $weeks)
15491549
/**
15501550
* Returns the SQL to subtract the number of given weeks from a date.
15511551
*
1552-
* @param string $date
1553-
* @param int|numeric-string $weeks
1552+
* @param string $date
1553+
* @param int|string $weeks
15541554
*
15551555
* @return string
15561556
*
@@ -1572,8 +1572,8 @@ public function getDateSubWeeksExpression($date, $weeks)
15721572
/**
15731573
* Returns the SQL to add the number of given months to a date.
15741574
*
1575-
* @param string $date
1576-
* @param int|numeric-string $months
1575+
* @param string $date
1576+
* @param int|string $months
15771577
*
15781578
* @return string
15791579
*
@@ -1595,8 +1595,8 @@ public function getDateAddMonthExpression($date, $months)
15951595
/**
15961596
* Returns the SQL to subtract the number of given months to a date.
15971597
*
1598-
* @param string $date
1599-
* @param int|numeric-string $months
1598+
* @param string $date
1599+
* @param int|string $months
16001600
*
16011601
* @return string
16021602
*
@@ -1618,8 +1618,8 @@ public function getDateSubMonthExpression($date, $months)
16181618
/**
16191619
* Returns the SQL to add the number of given quarters to a date.
16201620
*
1621-
* @param string $date
1622-
* @param int|numeric-string $quarters
1621+
* @param string $date
1622+
* @param int|string $quarters
16231623
*
16241624
* @return string
16251625
*
@@ -1641,8 +1641,8 @@ public function getDateAddQuartersExpression($date, $quarters)
16411641
/**
16421642
* Returns the SQL to subtract the number of given quarters from a date.
16431643
*
1644-
* @param string $date
1645-
* @param int|numeric-string $quarters
1644+
* @param string $date
1645+
* @param int|string $quarters
16461646
*
16471647
* @return string
16481648
*
@@ -1664,8 +1664,8 @@ public function getDateSubQuartersExpression($date, $quarters)
16641664
/**
16651665
* Returns the SQL to add the number of given years to a date.
16661666
*
1667-
* @param string $date
1668-
* @param int|numeric-string $years
1667+
* @param string $date
1668+
* @param int|string $years
16691669
*
16701670
* @return string
16711671
*
@@ -1687,8 +1687,8 @@ public function getDateAddYearsExpression($date, $years)
16871687
/**
16881688
* Returns the SQL to subtract the number of given years from a date.
16891689
*
1690-
* @param string $date
1691-
* @param int|numeric-string $years
1690+
* @param string $date
1691+
* @param int|string $years
16921692
*
16931693
* @return string
16941694
*
@@ -1710,11 +1710,11 @@ public function getDateSubYearsExpression($date, $years)
17101710
/**
17111711
* Returns the SQL for a date arithmetic expression.
17121712
*
1713-
* @param string $date The column or literal representing a date
1713+
* @param string $date The column or literal representing a date
17141714
* to perform the arithmetic operation on.
1715-
* @param string $operator The arithmetic operator (+ or -).
1716-
* @param int|numeric-string $interval The interval that shall be calculated into the date.
1717-
* @param string $unit The unit of the interval that shall be calculated into the date.
1715+
* @param string $operator The arithmetic operator (+ or -).
1716+
* @param int|string $interval The interval that shall be calculated into the date.
1717+
* @param string $unit The unit of the interval that shall be calculated into the date.
17181718
* One of the {@see DateIntervalUnit} constants.
17191719
*
17201720
* @return string
@@ -1726,6 +1726,17 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
17261726
throw Exception::notSupported(__METHOD__);
17271727
}
17281728

1729+
/**
1730+
* Generates the SQL expression which represents the given date interval multiplied by a number
1731+
*
1732+
* @param string $interval SQL expression describing the interval value
1733+
* @param int $multiplier Interval multiplier
1734+
*/
1735+
protected function multiplyInterval(string $interval, int $multiplier): string
1736+
{
1737+
return sprintf('(%s * %d)', $interval, $multiplier);
1738+
}
1739+
17291740
/**
17301741
* Returns the SQL bit AND comparison expression.
17311742
*

src/Platforms/DB2Platform.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,13 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
293293
{
294294
switch ($unit) {
295295
case DateIntervalUnit::WEEK:
296-
$interval *= 7;
297-
$unit = DateIntervalUnit::DAY;
296+
$interval = $this->multiplyInterval((string) $interval, 7);
297+
$unit = DateIntervalUnit::DAY;
298298
break;
299299

300300
case DateIntervalUnit::QUARTER:
301-
$interval *= 3;
302-
$unit = DateIntervalUnit::MONTH;
301+
$interval = $this->multiplyInterval((string) $interval, 3);
302+
$unit = DateIntervalUnit::MONTH;
303303
break;
304304
}
305305

src/Platforms/OraclePlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
114114
case DateIntervalUnit::YEAR:
115115
switch ($unit) {
116116
case DateIntervalUnit::QUARTER:
117-
$interval *= 3;
117+
$interval = $this->multiplyInterval((string) $interval, 3);
118118
break;
119119

120120
case DateIntervalUnit::YEAR:
121-
$interval *= 12;
121+
$interval = $this->multiplyInterval((string) $interval, 12);
122122
break;
123123
}
124124

src/Platforms/PostgreSQLPlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public function getLocateExpression($str, $substr, $startPos = false)
136136
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
137137
{
138138
if ($unit === DateIntervalUnit::QUARTER) {
139-
$interval *= 3;
140-
$unit = DateIntervalUnit::MONTH;
139+
$interval = $this->multiplyInterval((string) $interval, 3);
140+
$unit = DateIntervalUnit::MONTH;
141141
}
142142

143143
return '(' . $date . ' ' . $operator . ' (' . $interval . " || ' " . $unit . "')::interval)";

src/Platforms/SqlitePlatform.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
159159

160160
switch ($unit) {
161161
case DateIntervalUnit::WEEK:
162-
$interval *= 7;
163-
$unit = DateIntervalUnit::DAY;
162+
$interval = $this->multiplyInterval((string) $interval, 7);
163+
$unit = DateIntervalUnit::DAY;
164164
break;
165165

166166
case DateIntervalUnit::QUARTER:
167-
$interval *= 3;
168-
$unit = DateIntervalUnit::MONTH;
167+
$interval = $this->multiplyInterval((string) $interval, 3);
168+
$unit = DateIntervalUnit::MONTH;
169169
break;
170170
}
171171

tests/Platforms/DB2PlatformTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,12 @@ public function testGeneratesSQLSnippets(): void
321321
self::assertEquals("'1987/05/02' + 12 HOUR", $this->platform->getDateAddHourExpression("'1987/05/02'", 12));
322322
self::assertEquals("'1987/05/02' + 2 MINUTE", $this->platform->getDateAddMinutesExpression("'1987/05/02'", 2));
323323
self::assertEquals("'1987/05/02' + 102 MONTH", $this->platform->getDateAddMonthExpression("'1987/05/02'", 102));
324-
self::assertEquals("'1987/05/02' + 15 MONTH", $this->platform->getDateAddQuartersExpression("'1987/05/02'", 5));
324+
self::assertEquals(
325+
"'1987/05/02' + (5 * 3) MONTH",
326+
$this->platform->getDateAddQuartersExpression("'1987/05/02'", 5),
327+
);
325328
self::assertEquals("'1987/05/02' + 1 SECOND", $this->platform->getDateAddSecondsExpression("'1987/05/02'", 1));
326-
self::assertEquals("'1987/05/02' + 21 DAY", $this->platform->getDateAddWeeksExpression("'1987/05/02'", 3));
329+
self::assertEquals("'1987/05/02' + (3 * 7) DAY", $this->platform->getDateAddWeeksExpression("'1987/05/02'", 3));
327330
self::assertEquals("'1987/05/02' + 10 YEAR", $this->platform->getDateAddYearsExpression("'1987/05/02'", 10));
328331

329332
self::assertEquals(
@@ -335,9 +338,12 @@ public function testGeneratesSQLSnippets(): void
335338
self::assertEquals("'1987/05/02' - 12 HOUR", $this->platform->getDateSubHourExpression("'1987/05/02'", 12));
336339
self::assertEquals("'1987/05/02' - 2 MINUTE", $this->platform->getDateSubMinutesExpression("'1987/05/02'", 2));
337340
self::assertEquals("'1987/05/02' - 102 MONTH", $this->platform->getDateSubMonthExpression("'1987/05/02'", 102));
338-
self::assertEquals("'1987/05/02' - 15 MONTH", $this->platform->getDateSubQuartersExpression("'1987/05/02'", 5));
341+
self::assertEquals(
342+
"'1987/05/02' - (5 * 3) MONTH",
343+
$this->platform->getDateSubQuartersExpression("'1987/05/02'", 5),
344+
);
339345
self::assertEquals("'1987/05/02' - 1 SECOND", $this->platform->getDateSubSecondsExpression("'1987/05/02'", 1));
340-
self::assertEquals("'1987/05/02' - 21 DAY", $this->platform->getDateSubWeeksExpression("'1987/05/02'", 3));
346+
self::assertEquals("'1987/05/02' - (3 * 7) DAY", $this->platform->getDateSubWeeksExpression("'1987/05/02'", 3));
341347
self::assertEquals("'1987/05/02' - 10 YEAR", $this->platform->getDateSubYearsExpression("'1987/05/02'", 10));
342348
self::assertEquals(' WITH RR USE AND KEEP UPDATE LOCKS', $this->platform->getForUpdateSQL());
343349

0 commit comments

Comments
 (0)