Unverified Commit 93f89e3f authored by Marco Pivetta's avatar Marco Pivetta Committed by Sergei Morozov

Merge pull request #3498 from morozov/get-other-expr

Reworked AbstractPlatform::get*Expression() methods
parents 91ede91a 8d4d457c
# Upgrade to 3.0 # Upgrade to 3.0
## BC BREAK `AbstractPlatform::get*Expression()` methods no loner accept integer values as arguments
The following methods' arguments do not longer accept integer value:
- the `$expression` argument in `::getCountExpression()`,
- the `$decimals` argument in `::getRoundExpression()`,
- the `$seconds` argument in `::getDateAddSecondsExpression()`,
- the `$seconds` argument in `::getDateSubSecondsExpression()`,
- the `$minutes` argument in `::getDateAddMinutesExpression()`,
- the `$minutes` argument in `::getDateSubMinutesExpression()`,
- the `$hours` argument in `::getDateAddHourExpression()`,
- the `$hours` argument in `::getDateAddHourExpression()`,
- the `$days` argument in `::getDateAddDaysExpression()`,
- the `$days` argument in `::getDateSubDaysExpression()`,
- the `$weeks` argument in `::getDateAddWeeksExpression()`,
- the `$weeks` argument in `::getDateSubWeeksExpression()`,
- the `$months` argument in `::getDateAddMonthExpression()`,
- the `$months` argument in `::getDateSubMonthExpression()`,
- the `$quarters` argument in `::getDateAddQuartersExpression()`,
- the `$quarters` argument in `::getDateSubQuartersExpression()`,
- the `$years` argument in `::getDateAddYearsExpression()`,
- the `$years` argument in `::getDateSubYearsExpression()`.
Please use the strings representing numeric SQL literals instead (e.g. `'1'` instead of `1`).
The signature of `AbstractPlatform::getConcatExpression()` changed to `::getConcatExpression(string ...$string)`.
## BC BREAK The type of `$start` in `AbstractPlatform::getLocateExpression()` changed from `string|false` to `?string` ## BC BREAK The type of `$start` in `AbstractPlatform::getLocateExpression()` changed from `string|false` to `?string`
The default value of `$start` is now `null`, not `false`. The default value of `$start` is now `null`, not `false`.
......
...@@ -37,7 +37,6 @@ use function array_values; ...@@ -37,7 +37,6 @@ use function array_values;
use function assert; use function assert;
use function count; use function count;
use function explode; use function explode;
use function func_get_args;
use function implode; use function implode;
use function in_array; use function in_array;
use function is_array; use function is_array;
...@@ -611,11 +610,9 @@ abstract class AbstractPlatform ...@@ -611,11 +610,9 @@ abstract class AbstractPlatform
/** /**
* Returns the regular expression operator. * Returns the regular expression operator.
* *
* @return string
*
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getRegexpExpression() public function getRegexpExpression() : string
{ {
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
...@@ -623,13 +620,11 @@ abstract class AbstractPlatform ...@@ -623,13 +620,11 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL snippet to get the average value of a column. * Returns the SQL snippet to get the average value of a column.
* *
* @param string $column The column to use. * @param string $value SQL expression producing the value.
*
* @return string Generated SQL including an AVG aggregate function.
*/ */
public function getAvgExpression($column) public function getAvgExpression(string $value) : string
{ {
return 'AVG(' . $column . ')'; return 'AVG(' . $value . ')';
} }
/** /**
...@@ -637,115 +632,97 @@ abstract class AbstractPlatform ...@@ -637,115 +632,97 @@ abstract class AbstractPlatform
* *
* If a '*' is used instead of a column the number of selected rows is returned. * If a '*' is used instead of a column the number of selected rows is returned.
* *
* @param string|int $column The column to use. * @param string $expression The expression to count.
*
* @return string Generated SQL including a COUNT aggregate function.
*/ */
public function getCountExpression($column) public function getCountExpression(string $expression) : string
{ {
return 'COUNT(' . $column . ')'; return 'COUNT(' . $expression . ')';
} }
/** /**
* Returns the SQL snippet to get the highest value of a column. * Returns the SQL snippet to get the maximum value in a set of values.
*
* @param string $column The column to use.
* *
* @return string Generated SQL including a MAX aggregate function. * @param string $value SQL expression producing the value.
*/ */
public function getMaxExpression($column) public function getMaxExpression(string $value) : string
{ {
return 'MAX(' . $column . ')'; return 'MAX(' . $value . ')';
} }
/** /**
* Returns the SQL snippet to get the lowest value of a column. * Returns the SQL snippet to get the minimum value in a set of values.
*
* @param string $column The column to use.
* *
* @return string Generated SQL including a MIN aggregate function. * @param string $value SQL expression producing the value.
*/ */
public function getMinExpression($column) public function getMinExpression(string $value) : string
{ {
return 'MIN(' . $column . ')'; return 'MIN(' . $value . ')';
} }
/** /**
* Returns the SQL snippet to get the total sum of a column. * Returns the SQL snippet to get the total sum of the values in a set.
* *
* @param string $column The column to use. * @param string $value SQL expression producing the value.
*
* @return string Generated SQL including a SUM aggregate function.
*/ */
public function getSumExpression($column) public function getSumExpression(string $value) : string
{ {
return 'SUM(' . $column . ')'; return 'SUM(' . $value . ')';
} }
// scalar functions // scalar functions
/** /**
* Returns the SQL snippet to get the md5 sum of a field. * Returns the SQL snippet to get the md5 sum of the value.
* *
* Note: Not SQL92, but common functionality. * Note: Not SQL92, but common functionality.
* *
* @param string $column * @param string $string SQL expression producing the string.
*
* @return string
*/ */
public function getMd5Expression($column) public function getMd5Expression(string $string) : string
{ {
return 'MD5(' . $column . ')'; return 'MD5(' . $string . ')';
} }
/** /**
* Returns the SQL snippet to get the length of a text field. * Returns the SQL snippet to get the length of a text field.
* *
* @param string $column * @param string $string SQL expression producing the string.
*
* @return string
*/ */
public function getLengthExpression($column) public function getLengthExpression(string $string) : string
{ {
return 'LENGTH(' . $column . ')'; return 'LENGTH(' . $string . ')';
} }
/** /**
* Returns the SQL snippet to get the squared value of a column. * Returns the SQL snippet to get the square root of the value.
*
* @param string $column The column to use.
* *
* @return string Generated SQL including an SQRT aggregate function. * @param string $number SQL expression producing the number.
*/ */
public function getSqrtExpression($column) public function getSqrtExpression(string $number) : string
{ {
return 'SQRT(' . $column . ')'; return 'SQRT(' . $number . ')';
} }
/** /**
* Returns the SQL snippet to round a numeric field to the number of decimals specified. * Returns the SQL snippet to round a number to the number of decimals specified.
* *
* @param string $column * @param string $number SQL expression producing the number to round.
* @param int $decimals * @param string $decimals SQL expression producing the number of decimals.
*
* @return string
*/ */
public function getRoundExpression($column, $decimals = 0) public function getRoundExpression(string $number, string $decimals = '0') : string
{ {
return 'ROUND(' . $column . ', ' . $decimals . ')'; return 'ROUND(' . $number . ', ' . $decimals . ')';
} }
/** /**
* Returns the SQL snippet to get the remainder of the division operation $expression1 / $expression2. * Returns the SQL snippet to get the remainder of the operation of division of dividend by divisor.
*
* @param string $expression1
* @param string $expression2
* *
* @return string * @param string $dividend SQL expression producing the dividend.
* @param string $divisor SQL expression producing the divisor.
*/ */
public function getModExpression($expression1, $expression2) public function getModExpression(string $dividend, string $divisor) : string
{ {
return 'MOD(' . $expression1 . ', ' . $expression2 . ')'; return 'MOD(' . $dividend . ', ' . $divisor . ')';
} }
/** /**
...@@ -800,53 +777,45 @@ abstract class AbstractPlatform ...@@ -800,53 +777,45 @@ abstract class AbstractPlatform
} }
/** /**
* Returns the SQL snippet to trim trailing space characters from the expression. * Returns the SQL snippet to trim trailing space characters from the string.
*
* @param string $str Literal string or column name.
* *
* @return string * @param string $string SQL expression producing the string.
*/ */
public function getRtrimExpression($str) public function getRtrimExpression(string $string) : string
{ {
return 'RTRIM(' . $str . ')'; return 'RTRIM(' . $string . ')';
} }
/** /**
* Returns the SQL snippet to trim leading space characters from the expression. * Returns the SQL snippet to trim leading space characters from the string.
* *
* @param string $str Literal string or column name. * @param string $string SQL expression producing the string.
*
* @return string
*/ */
public function getLtrimExpression($str) public function getLtrimExpression(string $string) : string
{ {
return 'LTRIM(' . $str . ')'; return 'LTRIM(' . $string . ')';
} }
/** /**
* Returns the SQL snippet to change all characters from the expression to uppercase, * Returns the SQL snippet to change all characters from the string to uppercase,
* according to the current character set mapping. * according to the current character set mapping.
* *
* @param string $str Literal string or column name. * @param string $string SQL expression producing the string.
*
* @return string
*/ */
public function getUpperExpression($str) public function getUpperExpression(string $string) : string
{ {
return 'UPPER(' . $str . ')'; return 'UPPER(' . $string . ')';
} }
/** /**
* Returns the SQL snippet to change all characters from the expression to lowercase, * Returns the SQL snippet to change all characters from the string to lowercase,
* according to the current character set mapping. * according to the current character set mapping.
* *
* @param string $str Literal string or column name. * @param string $string SQL expression producing the string.
*
* @return string
*/ */
public function getLowerExpression($str) public function getLowerExpression(string $string) : string
{ {
return 'LOWER(' . $str . ')'; return 'LOWER(' . $string . ')';
} }
/** /**
...@@ -866,10 +835,8 @@ abstract class AbstractPlatform ...@@ -866,10 +835,8 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL snippet to get the current system date. * Returns the SQL snippet to get the current system date.
*
* @return string
*/ */
public function getNowExpression() public function getNowExpression() : string
{ {
return 'NOW()'; return 'NOW()';
} }
...@@ -894,111 +861,87 @@ abstract class AbstractPlatform ...@@ -894,111 +861,87 @@ abstract class AbstractPlatform
} }
/** /**
* Returns a SQL snippet to concatenate the given expressions. * Returns a SQL snippet to concatenate the given strings.
* *
* Accepts an arbitrary number of string parameters. Each parameter must contain an expression. * @param string[] ...$string
*
* @return string
*/ */
public function getConcatExpression() public function getConcatExpression(string ...$string) : string
{ {
return implode(' || ', func_get_args()); return implode(' || ', $string);
} }
/** /**
* Returns the SQL for a logical not. * Returns the SQL for a logical not.
* *
* Example: * @param string $value SQL expression producing the value to negate.
* <code>
* $q = new Doctrine_Query();
* $e = $q->expr;
* $q->select('*')->from('table')
* ->where($e->eq('id', $e->not('null'));
* </code>
*
* @param string $expression
*
* @return string The logical expression.
*/ */
public function getNotExpression($expression) public function getNotExpression(string $value) : string
{ {
return 'NOT(' . $expression . ')'; return 'NOT(' . $value . ')';
} }
/** /**
* Returns the SQL that checks if an expression is null. * Returns the SQL that checks if an expression is null.
* *
* @param string $expression The expression that should be compared to null. * @param string $value SQL expression producing the to be compared to null.
*
* @return string The logical expression.
*/ */
public function getIsNullExpression($expression) public function getIsNullExpression(string $value) : string
{ {
return $expression . ' IS NULL'; return $value . ' IS NULL';
} }
/** /**
* Returns the SQL that checks if an expression is not null. * Returns the SQL that checks if an expression is not null.
* *
* @param string $expression The expression that should be compared to null. * @param string $value SQL expression producing the to be compared to null.
*
* @return string The logical expression.
*/ */
public function getIsNotNullExpression($expression) public function getIsNotNullExpression(string $value) : string
{ {
return $expression . ' IS NOT NULL'; return $value . ' IS NOT NULL';
} }
/** /**
* Returns the SQL that checks if an expression evaluates to a value between two values. * Returns the SQL that checks if an expression evaluates to a value between two values.
* *
* The parameter $expression is checked if it is between $value1 and $value2. * The parameter $value is checked if it is between $min and $max.
* *
* Note: There is a slight difference in the way BETWEEN works on some databases. * Note: There is a slight difference in the way BETWEEN works on some databases.
* http://www.w3schools.com/sql/sql_between.asp. If you want complete database * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
* independence you should avoid using between(). * independence you should avoid using between().
* *
* @param string $expression The value to compare to. * @param string $value SQL expression producing the value to compare.
* @param string $value1 The lower value to compare with. * @param string $min SQL expression producing the lower value to compare with.
* @param string $value2 The higher value to compare with. * @param string $max SQL expression producing the higher value to compare with.
*
* @return string The logical expression.
*/ */
public function getBetweenExpression($expression, $value1, $value2) public function getBetweenExpression(string $value, string $min, string $max) : string
{ {
return $expression . ' BETWEEN ' . $value1 . ' AND ' . $value2; return $value . ' BETWEEN ' . $min . ' AND ' . $max;
} }
/** /**
* Returns the SQL to get the arccosine of a value. * Returns the SQL to get the arccosine of a value.
* *
* @param string $value * @param string $number SQL expression producing the number.
*
* @return string
*/ */
public function getAcosExpression($value) public function getAcosExpression(string $number) : string
{ {
return 'ACOS(' . $value . ')'; return 'ACOS(' . $number . ')';
} }
/** /**
* Returns the SQL to get the sine of a value. * Returns the SQL to get the sine of a value.
* *
* @param string $value * @param string $number SQL expression producing the number.
*
* @return string
*/ */
public function getSinExpression($value) public function getSinExpression(string $number) : string
{ {
return 'SIN(' . $value . ')'; return 'SIN(' . $number . ')';
} }
/** /**
* Returns the SQL to get the PI value. * Returns the SQL to get the PI value.
*
* @return string
*/ */
public function getPiExpression() public function getPiExpression() : string
{ {
return 'PI()'; return 'PI()';
} }
...@@ -1006,13 +949,11 @@ abstract class AbstractPlatform ...@@ -1006,13 +949,11 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to get the cosine of a value. * Returns the SQL to get the cosine of a value.
* *
* @param string $value * @param string $number SQL expression producing the number.
*
* @return string
*/ */
public function getCosExpression($value) public function getCosExpression(string $number) : string
{ {
return 'COS(' . $value . ')'; return 'COS(' . $number . ')';
} }
/** /**
...@@ -1020,14 +961,9 @@ abstract class AbstractPlatform ...@@ -1020,14 +961,9 @@ abstract class AbstractPlatform
* *
* Computes diff = date1 - date2. * Computes diff = date1 - date2.
* *
* @param string $date1
* @param string $date2
*
* @return string
*
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
...@@ -1035,14 +971,12 @@ abstract class AbstractPlatform ...@@ -1035,14 +971,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given seconds to a date. * Returns the SQL to add the number of given seconds to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $seconds * @param string $seconds SQL expression producing the number of seconds.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddSecondsExpression($date, $seconds) public function getDateAddSecondsExpression(string $date, string $seconds) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, DateIntervalUnit::SECOND); return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, DateIntervalUnit::SECOND);
} }
...@@ -1050,14 +984,12 @@ abstract class AbstractPlatform ...@@ -1050,14 +984,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given seconds from a date. * Returns the SQL to subtract the number of given seconds from a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $seconds * @param string $seconds SQL expression producing the number of seconds.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubSecondsExpression($date, $seconds) public function getDateSubSecondsExpression(string $date, string $seconds) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, DateIntervalUnit::SECOND); return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, DateIntervalUnit::SECOND);
} }
...@@ -1065,14 +997,12 @@ abstract class AbstractPlatform ...@@ -1065,14 +997,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given minutes to a date. * Returns the SQL to add the number of given minutes to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $minutes * @param string $minutes SQL expression producing the number of minutes.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddMinutesExpression($date, $minutes) public function getDateAddMinutesExpression(string $date, string $minutes) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, DateIntervalUnit::MINUTE); return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, DateIntervalUnit::MINUTE);
} }
...@@ -1080,14 +1010,12 @@ abstract class AbstractPlatform ...@@ -1080,14 +1010,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given minutes from a date. * Returns the SQL to subtract the number of given minutes from a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $minutes * @param string $minutes SQL expression producing the number of minutes.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubMinutesExpression($date, $minutes) public function getDateSubMinutesExpression(string $date, string $minutes) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, DateIntervalUnit::MINUTE); return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, DateIntervalUnit::MINUTE);
} }
...@@ -1095,14 +1023,12 @@ abstract class AbstractPlatform ...@@ -1095,14 +1023,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given hours to a date. * Returns the SQL to add the number of given hours to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $hours * @param string $hours SQL expression producing the number of hours.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddHourExpression($date, $hours) public function getDateAddHourExpression(string $date, string $hours) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $hours, DateIntervalUnit::HOUR); return $this->getDateArithmeticIntervalExpression($date, '+', $hours, DateIntervalUnit::HOUR);
} }
...@@ -1110,14 +1036,12 @@ abstract class AbstractPlatform ...@@ -1110,14 +1036,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given hours to a date. * Returns the SQL to subtract the number of given hours to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $hours * @param string $hours SQL expression producing the number of hours.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubHourExpression($date, $hours) public function getDateSubHourExpression(string $date, string $hours) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $hours, DateIntervalUnit::HOUR); return $this->getDateArithmeticIntervalExpression($date, '-', $hours, DateIntervalUnit::HOUR);
} }
...@@ -1125,14 +1049,12 @@ abstract class AbstractPlatform ...@@ -1125,14 +1049,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given days to a date. * Returns the SQL to add the number of given days to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $days * @param string $days SQL expression producing the number of days.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddDaysExpression($date, $days) public function getDateAddDaysExpression(string $date, string $days) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $days, DateIntervalUnit::DAY); return $this->getDateArithmeticIntervalExpression($date, '+', $days, DateIntervalUnit::DAY);
} }
...@@ -1140,14 +1062,12 @@ abstract class AbstractPlatform ...@@ -1140,14 +1062,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given days to a date. * Returns the SQL to subtract the number of given days to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $days * @param string $days SQL expression producing the number of days.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubDaysExpression($date, $days) public function getDateSubDaysExpression(string $date, string $days) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $days, DateIntervalUnit::DAY); return $this->getDateArithmeticIntervalExpression($date, '-', $days, DateIntervalUnit::DAY);
} }
...@@ -1155,14 +1075,12 @@ abstract class AbstractPlatform ...@@ -1155,14 +1075,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given weeks to a date. * Returns the SQL to add the number of given weeks to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $weeks * @param string $weeks SQL expression producing the number of weeks.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddWeeksExpression($date, $weeks) public function getDateAddWeeksExpression(string $date, string $weeks) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, DateIntervalUnit::WEEK); return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, DateIntervalUnit::WEEK);
} }
...@@ -1170,14 +1088,12 @@ abstract class AbstractPlatform ...@@ -1170,14 +1088,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given weeks from a date. * Returns the SQL to subtract the number of given weeks from a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $weeks * @param string $weeks SQL expression producing the number of weeks.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubWeeksExpression($date, $weeks) public function getDateSubWeeksExpression(string $date, string $weeks) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, DateIntervalUnit::WEEK); return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, DateIntervalUnit::WEEK);
} }
...@@ -1185,14 +1101,12 @@ abstract class AbstractPlatform ...@@ -1185,14 +1101,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given months to a date. * Returns the SQL to add the number of given months to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $months * @param string $months SQL expression producing the number of months.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddMonthExpression($date, $months) public function getDateAddMonthExpression(string $date, string $months) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $months, DateIntervalUnit::MONTH); return $this->getDateArithmeticIntervalExpression($date, '+', $months, DateIntervalUnit::MONTH);
} }
...@@ -1200,14 +1114,12 @@ abstract class AbstractPlatform ...@@ -1200,14 +1114,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given months to a date. * Returns the SQL to subtract the number of given months to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $months * @param string $months SQL expression producing the number of months.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateSubMonthExpression(string $date, string $months) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $months, DateIntervalUnit::MONTH); return $this->getDateArithmeticIntervalExpression($date, '-', $months, DateIntervalUnit::MONTH);
} }
...@@ -1215,14 +1127,12 @@ abstract class AbstractPlatform ...@@ -1215,14 +1127,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given quarters to a date. * Returns the SQL to add the number of given quarters to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $quarters * @param string $quarters SQL expression producing the number of quarters.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddQuartersExpression($date, $quarters) public function getDateAddQuartersExpression(string $date, string $quarters) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $quarters, DateIntervalUnit::QUARTER); return $this->getDateArithmeticIntervalExpression($date, '+', $quarters, DateIntervalUnit::QUARTER);
} }
...@@ -1230,14 +1140,12 @@ abstract class AbstractPlatform ...@@ -1230,14 +1140,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given quarters from a date. * Returns the SQL to subtract the number of given quarters from a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $quarters * @param string $quarters SQL expression producing the number of quarters.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubQuartersExpression($date, $quarters) public function getDateSubQuartersExpression(string $date, string $quarters) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $quarters, DateIntervalUnit::QUARTER); return $this->getDateArithmeticIntervalExpression($date, '-', $quarters, DateIntervalUnit::QUARTER);
} }
...@@ -1245,14 +1153,12 @@ abstract class AbstractPlatform ...@@ -1245,14 +1153,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to add the number of given years to a date. * Returns the SQL to add the number of given years to a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $years * @param string $years SQL expression producing the number of years.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateAddYearsExpression($date, $years) public function getDateAddYearsExpression(string $date, string $years) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '+', $years, DateIntervalUnit::YEAR); return $this->getDateArithmeticIntervalExpression($date, '+', $years, DateIntervalUnit::YEAR);
} }
...@@ -1260,14 +1166,12 @@ abstract class AbstractPlatform ...@@ -1260,14 +1166,12 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL to subtract the number of given years from a date. * Returns the SQL to subtract the number of given years from a date.
* *
* @param string $date * @param string $date SQL expression producing the date.
* @param int $years * @param string $years SQL expression producing the number of years.
*
* @return string
* *
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
public function getDateSubYearsExpression($date, $years) public function getDateSubYearsExpression(string $date, string $years) : string
{ {
return $this->getDateArithmeticIntervalExpression($date, '-', $years, DateIntervalUnit::YEAR); return $this->getDateArithmeticIntervalExpression($date, '-', $years, DateIntervalUnit::YEAR);
} }
...@@ -1275,30 +1179,40 @@ abstract class AbstractPlatform ...@@ -1275,30 +1179,40 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL for a date arithmetic expression. * Returns the SQL for a date arithmetic expression.
* *
* @param string $date The column or literal representing a date to perform the arithmetic operation on. * @param string $date SQL expression representing a date to perform the arithmetic operation on.
* @param string $operator The arithmetic operator (+ or -). * @param string $operator The arithmetic operator (+ or -).
* @param int $interval The interval that shall be calculated into the date. * @param string $interval SQL expression representing the value of the interval that shall be calculated
* into the date.
* @param string $unit The unit of the interval that shall be calculated into the date. * @param string $unit The unit of the interval that shall be calculated into the date.
* One of the DATE_INTERVAL_UNIT_* constants. * One of the DATE_INTERVAL_UNIT_* constants.
* *
* @return string
*
* @throws DBALException If not supported on this platform. * @throws DBALException If not supported on this platform.
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
/** /**
* Returns the SQL bit AND comparison expression. * Generates the SQL expression which represents the given date interval multiplied by a number
* *
* @param string $value1 * @param string $interval SQL expression describing the interval value
* @param string $value2 * @param int $multiplier Interval multiplier
* *
* @return string * @throws DBALException
*/ */
public function getBitAndComparisonExpression($value1, $value2) protected function multiplyInterval(string $interval, int $multiplier) : string
{
return sprintf('(%s * %d)', $interval, $multiplier);
}
/**
* Returns the SQL bit AND comparison expression.
*
* @param string $value1 SQL expression producing the first value.
* @param string $value2 SQL expression producing the second value.
*/
public function getBitAndComparisonExpression(string $value1, string $value2) : string
{ {
return '(' . $value1 . ' & ' . $value2 . ')'; return '(' . $value1 . ' & ' . $value2 . ')';
} }
...@@ -1306,12 +1220,10 @@ abstract class AbstractPlatform ...@@ -1306,12 +1220,10 @@ abstract class AbstractPlatform
/** /**
* Returns the SQL bit OR comparison expression. * Returns the SQL bit OR comparison expression.
* *
* @param string $value1 * @param string $value1 SQL expression producing the first value.
* @param string $value2 * @param string $value2 SQL expression producing the second value.
*
* @return string
*/ */
public function getBitOrComparisonExpression($value1, $value2) public function getBitOrComparisonExpression(string $value1, string $value2) : string
{ {
return '(' . $value1 . ' | ' . $value2 . ')'; return '(' . $value1 . ' | ' . $value2 . ')';
} }
......
...@@ -186,7 +186,7 @@ class DB2Platform extends AbstractPlatform ...@@ -186,7 +186,7 @@ class DB2Platform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getBitAndComparisonExpression($value1, $value2) public function getBitAndComparisonExpression(string $value1, string $value2) : string
{ {
return 'BITAND(' . $value1 . ', ' . $value2 . ')'; return 'BITAND(' . $value1 . ', ' . $value2 . ')';
} }
...@@ -194,7 +194,7 @@ class DB2Platform extends AbstractPlatform ...@@ -194,7 +194,7 @@ class DB2Platform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getBitOrComparisonExpression($value1, $value2) public function getBitOrComparisonExpression(string $value1, string $value2) : string
{ {
return 'BITOR(' . $value1 . ', ' . $value2 . ')'; return 'BITOR(' . $value1 . ', ' . $value2 . ')';
} }
...@@ -202,16 +202,16 @@ class DB2Platform extends AbstractPlatform ...@@ -202,16 +202,16 @@ class DB2Platform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
switch ($unit) { switch ($unit) {
case DateIntervalUnit::WEEK: case DateIntervalUnit::WEEK:
$interval *= 7; $interval = $this->multiplyInterval($interval, 7);
$unit = DateIntervalUnit::DAY; $unit = DateIntervalUnit::DAY;
break; break;
case DateIntervalUnit::QUARTER: case DateIntervalUnit::QUARTER:
$interval *= 3; $interval = $this->multiplyInterval($interval, 3);
$unit = DateIntervalUnit::MONTH; $unit = DateIntervalUnit::MONTH;
break; break;
} }
...@@ -222,7 +222,7 @@ class DB2Platform extends AbstractPlatform ...@@ -222,7 +222,7 @@ class DB2Platform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return 'DAYS(' . $date1 . ') - DAYS(' . $date2 . ')'; return 'DAYS(' . $date1 . ') - DAYS(' . $date2 . ')';
} }
......
...@@ -16,7 +16,6 @@ use function array_merge; ...@@ -16,7 +16,6 @@ use function array_merge;
use function array_unique; use function array_unique;
use function array_values; use function array_values;
use function count; use function count;
use function func_get_args;
use function implode; use function implode;
use function in_array; use function in_array;
use function is_numeric; use function is_numeric;
...@@ -73,7 +72,7 @@ class MySqlPlatform extends AbstractPlatform ...@@ -73,7 +72,7 @@ class MySqlPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getRegexpExpression() public function getRegexpExpression() : string
{ {
return 'RLIKE'; return 'RLIKE';
} }
...@@ -93,15 +92,15 @@ class MySqlPlatform extends AbstractPlatform ...@@ -93,15 +92,15 @@ class MySqlPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getConcatExpression() public function getConcatExpression(string ...$string) : string
{ {
return sprintf('CONCAT(%s)', implode(', ', func_get_args())); return sprintf('CONCAT(%s)', implode(', ', $string));
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
$function = $operator === '+' ? 'DATE_ADD' : 'DATE_SUB'; $function = $operator === '+' ? 'DATE_ADD' : 'DATE_SUB';
...@@ -111,7 +110,7 @@ class MySqlPlatform extends AbstractPlatform ...@@ -111,7 +110,7 @@ class MySqlPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
} }
......
...@@ -59,7 +59,7 @@ class OraclePlatform extends AbstractPlatform ...@@ -59,7 +59,7 @@ class OraclePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getNowExpression($type = 'timestamp') public function getNowExpression($type = 'timestamp') : string
{ {
switch ($type) { switch ($type) {
case 'date': case 'date':
...@@ -85,7 +85,7 @@ class OraclePlatform extends AbstractPlatform ...@@ -85,7 +85,7 @@ class OraclePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
switch ($unit) { switch ($unit) {
case DateIntervalUnit::MONTH: case DateIntervalUnit::MONTH:
...@@ -93,11 +93,11 @@ class OraclePlatform extends AbstractPlatform ...@@ -93,11 +93,11 @@ class OraclePlatform extends AbstractPlatform
case DateIntervalUnit::YEAR: case DateIntervalUnit::YEAR:
switch ($unit) { switch ($unit) {
case DateIntervalUnit::QUARTER: case DateIntervalUnit::QUARTER:
$interval *= 3; $interval = $this->multiplyInterval($interval, 3);
break; break;
case DateIntervalUnit::YEAR: case DateIntervalUnit::YEAR:
$interval *= 12; $interval = $this->multiplyInterval($interval, 12);
break; break;
} }
...@@ -131,7 +131,7 @@ class OraclePlatform extends AbstractPlatform ...@@ -131,7 +131,7 @@ class OraclePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return sprintf('TRUNC(%s) - TRUNC(%s)', $date1, $date2); return sprintf('TRUNC(%s) - TRUNC(%s)', $date1, $date2);
} }
...@@ -139,7 +139,7 @@ class OraclePlatform extends AbstractPlatform ...@@ -139,7 +139,7 @@ class OraclePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getBitAndComparisonExpression($value1, $value2) public function getBitAndComparisonExpression(string $value1, string $value2) : string
{ {
return 'BITAND(' . $value1 . ', ' . $value2 . ')'; return 'BITAND(' . $value1 . ', ' . $value2 . ')';
} }
...@@ -147,7 +147,7 @@ class OraclePlatform extends AbstractPlatform ...@@ -147,7 +147,7 @@ class OraclePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getBitOrComparisonExpression($value1, $value2) public function getBitOrComparisonExpression(string $value1, string $value2) : string
{ {
return '(' . $value1 . '-' . return '(' . $value1 . '-' .
$this->getBitAndComparisonExpression($value1, $value2) $this->getBitAndComparisonExpression($value1, $value2)
......
...@@ -78,7 +78,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -78,7 +78,7 @@ class PostgreSqlPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getNowExpression() public function getNowExpression() : string
{ {
return 'LOCALTIMESTAMP(0)'; return 'LOCALTIMESTAMP(0)';
} }
...@@ -86,7 +86,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -86,7 +86,7 @@ class PostgreSqlPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getRegexpExpression() public function getRegexpExpression() : string
{ {
return 'SIMILAR TO'; return 'SIMILAR TO';
} }
...@@ -108,10 +108,10 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -108,10 +108,10 @@ class PostgreSqlPlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
if ($unit === DateIntervalUnit::QUARTER) { if ($unit === DateIntervalUnit::QUARTER) {
$interval *= 3; $interval = $this->multiplyInterval($interval, 3);
$unit = DateIntervalUnit::MONTH; $unit = DateIntervalUnit::MONTH;
} }
...@@ -121,7 +121,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -121,7 +121,7 @@ class PostgreSqlPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))'; return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
} }
......
...@@ -21,7 +21,6 @@ use function array_unique; ...@@ -21,7 +21,6 @@ use function array_unique;
use function array_values; use function array_values;
use function count; use function count;
use function explode; use function explode;
use function func_get_args;
use function get_class; use function get_class;
use function implode; use function implode;
use function in_array; use function in_array;
...@@ -373,9 +372,9 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -373,9 +372,9 @@ class SQLAnywherePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getConcatExpression() public function getConcatExpression(string ...$string) : string
{ {
return 'STRING(' . implode(', ', (array) func_get_args()) . ')'; return 'STRING(' . implode(', ', $string) . ')';
} }
/** /**
...@@ -470,7 +469,7 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -470,7 +469,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
$factorClause = ''; $factorClause = '';
...@@ -484,7 +483,7 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -484,7 +483,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return 'DATEDIFF(day, ' . $date2 . ', ' . $date1 . ')'; return 'DATEDIFF(day, ' . $date2 . ', ' . $date1 . ')';
} }
...@@ -986,15 +985,15 @@ SQL ...@@ -986,15 +985,15 @@ SQL
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMd5Expression($column) public function getMd5Expression(string $string) : string
{ {
return 'HASH(' . $column . ", 'MD5')"; return 'HASH(' . $string . ", 'MD5')";
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getRegexpExpression() public function getRegexpExpression() : string
{ {
return 'REGEXP'; return 'REGEXP';
} }
......
...@@ -18,7 +18,6 @@ use function count; ...@@ -18,7 +18,6 @@ use function count;
use function crc32; use function crc32;
use function dechex; use function dechex;
use function explode; use function explode;
use function func_get_args;
use function implode; use function implode;
use function in_array; use function in_array;
use function is_array; use function is_array;
...@@ -74,7 +73,7 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -74,7 +73,7 @@ class SQLServerPlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
$factorClause = ''; $factorClause = '';
...@@ -88,7 +87,7 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -88,7 +87,7 @@ class SQLServerPlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return 'DATEDIFF(day, ' . $date2 . ',' . $date1 . ')'; return 'DATEDIFF(day, ' . $date2 . ',' . $date1 . ')';
} }
...@@ -1028,9 +1027,9 @@ SQL ...@@ -1028,9 +1027,9 @@ SQL
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getModExpression($expression1, $expression2) public function getModExpression(string $dividend, string $divisor) : string
{ {
return $expression1 . ' % ' . $expression2; return $dividend . ' % ' . $divisor;
} }
/** /**
...@@ -1082,11 +1081,9 @@ SQL ...@@ -1082,11 +1081,9 @@ SQL
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getConcatExpression() public function getConcatExpression(string ...$string) : string
{ {
$args = func_get_args(); return '(' . implode(' + ', $string) . ')';
return '(' . implode(' + ', $args) . ')';
} }
/** /**
...@@ -1120,9 +1117,9 @@ SQL ...@@ -1120,9 +1117,9 @@ SQL
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getLengthExpression($column) public function getLengthExpression(string $string) : string
{ {
return 'LEN(' . $column . ')'; return 'LEN(' . $string . ')';
} }
/** /**
......
...@@ -17,7 +17,6 @@ use function array_merge; ...@@ -17,7 +17,6 @@ use function array_merge;
use function array_unique; use function array_unique;
use function array_values; use function array_values;
use function implode; use function implode;
use function is_numeric;
use function sprintf; use function sprintf;
use function sqrt; use function sqrt;
use function str_replace; use function str_replace;
...@@ -37,7 +36,7 @@ class SqlitePlatform extends AbstractPlatform ...@@ -37,7 +36,7 @@ class SqlitePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getRegexpExpression() public function getRegexpExpression() : string
{ {
return 'REGEXP'; return 'REGEXP';
} }
...@@ -45,7 +44,7 @@ class SqlitePlatform extends AbstractPlatform ...@@ -45,7 +44,7 @@ class SqlitePlatform extends AbstractPlatform
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getNowExpression($type = 'timestamp') public function getNowExpression($type = 'timestamp') : string
{ {
switch ($type) { switch ($type) {
case 'time': case 'time':
...@@ -122,39 +121,31 @@ class SqlitePlatform extends AbstractPlatform ...@@ -122,39 +121,31 @@ class SqlitePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, string $unit) : string
{ {
switch ($unit) {
case DateIntervalUnit::SECOND:
case DateIntervalUnit::MINUTE:
case DateIntervalUnit::HOUR:
return 'DATETIME(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')";
default:
switch ($unit) { switch ($unit) {
case DateIntervalUnit::WEEK: case DateIntervalUnit::WEEK:
$interval *= 7; $interval = $this->multiplyInterval($interval, 7);
$unit = DateIntervalUnit::DAY; $unit = DateIntervalUnit::DAY;
break; break;
case DateIntervalUnit::QUARTER: case DateIntervalUnit::QUARTER:
$interval *= 3; $interval = $this->multiplyInterval($interval, 3);
$unit = DateIntervalUnit::MONTH; $unit = DateIntervalUnit::MONTH;
break; break;
} }
if (! is_numeric($interval)) { return 'DATETIME(' . $date . ',' . $this->getConcatExpression(
$interval = "' || " . $interval . " || '"; $this->quoteStringLiteral($operator),
} $interval,
$this->quoteStringLiteral(' ' . $unit)
return 'DATE(' . $date . ",'" . $operator . $interval . ' ' . $unit . "')"; ) . ')';
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateDiffExpression($date1, $date2) public function getDateDiffExpression(string $date1, string $date2) : string
{ {
return sprintf("JULIANDAY(%s, 'start of day') - JULIANDAY(%s, 'start of day')", $date1, $date2); return sprintf("JULIANDAY(%s, 'start of day') - JULIANDAY(%s, 'start of day')", $date1, $date2);
} }
......
...@@ -12,6 +12,7 @@ use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver; ...@@ -12,6 +12,7 @@ use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\TrimMode; use Doctrine\DBAL\Platforms\TrimMode;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
...@@ -577,49 +578,313 @@ class DataAccessTest extends DbalFunctionalTestCase ...@@ -577,49 +578,313 @@ class DataAccessTest extends DbalFunctionalTestCase
} }
/** /**
* @group DDC-1014 * @dataProvider modeProvider
*/ */
public function testDateArithmetics() : void public function testDateAddSeconds(callable $buildQuery, callable $bindParams) : void
{ {
$p = $this->connection->getDatabasePlatform(); $this->assertDateExpression(
$sql = 'SELECT '; $buildQuery,
$sql .= $p->getDateAddSecondsExpression('test_datetime', 1) . ' AS add_seconds, '; $bindParams,
$sql .= $p->getDateSubSecondsExpression('test_datetime', 1) . ' AS sub_seconds, '; static function (AbstractPlatform $platform, string $interval) : string {
$sql .= $p->getDateAddMinutesExpression('test_datetime', 5) . ' AS add_minutes, '; return $platform->getDateAddSecondsExpression('test_datetime', $interval);
$sql .= $p->getDateSubMinutesExpression('test_datetime', 5) . ' AS sub_minutes, '; },
$sql .= $p->getDateAddHourExpression('test_datetime', 3) . ' AS add_hour, '; 1,
$sql .= $p->getDateSubHourExpression('test_datetime', 3) . ' AS sub_hour, '; '2010-01-01 10:10:11'
$sql .= $p->getDateAddDaysExpression('test_datetime', 10) . ' AS add_days, '; );
$sql .= $p->getDateSubDaysExpression('test_datetime', 10) . ' AS sub_days, '; }
$sql .= $p->getDateAddWeeksExpression('test_datetime', 1) . ' AS add_weeks, ';
$sql .= $p->getDateSubWeeksExpression('test_datetime', 1) . ' AS sub_weeks, ';
$sql .= $p->getDateAddMonthExpression('test_datetime', 2) . ' AS add_month, ';
$sql .= $p->getDateSubMonthExpression('test_datetime', 2) . ' AS sub_month, ';
$sql .= $p->getDateAddQuartersExpression('test_datetime', 3) . ' AS add_quarters, ';
$sql .= $p->getDateSubQuartersExpression('test_datetime', 3) . ' AS sub_quarters, ';
$sql .= $p->getDateAddYearsExpression('test_datetime', 6) . ' AS add_years, ';
$sql .= $p->getDateSubYearsExpression('test_datetime', 6) . ' AS sub_years ';
$sql .= 'FROM fetch_table';
$row = $this->connection->fetchAssoc($sql); /**
$row = array_change_key_case($row, CASE_LOWER); * @dataProvider modeProvider
*/
public function testDateSubSeconds(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubSecondsExpression('test_datetime', $interval);
},
1,
'2010-01-01 10:10:09'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateAddMinutes(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddMinutesExpression('test_datetime', $interval);
},
5,
'2010-01-01 10:15:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateSubMinutes(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubMinutesExpression('test_datetime', $interval);
},
5,
'2010-01-01 10:05:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateAddHours(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddHourExpression('test_datetime', $interval);
},
3,
'2010-01-01 13:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateSubHours(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubHourExpression('test_datetime', $interval);
},
3,
'2010-01-01 07:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateAddDays(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddDaysExpression('test_datetime', $interval);
},
10,
'2010-01-11 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateSubDays(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubDaysExpression('test_datetime', $interval);
},
10,
'2009-12-22 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateAddWeeks(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddWeeksExpression('test_datetime', $interval);
},
1,
'2010-01-08 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateSubWeeks(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubWeeksExpression('test_datetime', $interval);
},
1,
'2009-12-25 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateAddMonths(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddMonthExpression('test_datetime', $interval);
},
2,
'2010-03-01 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateSubMonths(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubMonthExpression('test_datetime', $interval);
},
2,
'2009-11-01 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateAddQuarters(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddQuartersExpression('test_datetime', $interval);
},
3,
'2010-10-01 10:10:10'
);
}
self::assertEquals('2010-01-01 10:10:11', date('Y-m-d H:i:s', strtotime($row['add_seconds'])), 'Adding second should end up on 2010-01-01 10:10:11'); /**
self::assertEquals('2010-01-01 10:10:09', date('Y-m-d H:i:s', strtotime($row['sub_seconds'])), 'Subtracting second should end up on 2010-01-01 10:10:09'); * @dataProvider modeProvider
self::assertEquals('2010-01-01 10:15:10', date('Y-m-d H:i:s', strtotime($row['add_minutes'])), 'Adding minutes should end up on 2010-01-01 10:15:10'); */
self::assertEquals('2010-01-01 10:05:10', date('Y-m-d H:i:s', strtotime($row['sub_minutes'])), 'Subtracting minutes should end up on 2010-01-01 10:05:10'); public function testDateSubQuarters(callable $buildQuery, callable $bindParams) : void
self::assertEquals('2010-01-01 13:10', date('Y-m-d H:i', strtotime($row['add_hour'])), 'Adding date should end up on 2010-01-01 13:10'); {
self::assertEquals('2010-01-01 07:10', date('Y-m-d H:i', strtotime($row['sub_hour'])), 'Subtracting date should end up on 2010-01-01 07:10'); $this->assertDateExpression(
self::assertEquals('2010-01-11', date('Y-m-d', strtotime($row['add_days'])), 'Adding date should end up on 2010-01-11'); $buildQuery,
self::assertEquals('2009-12-22', date('Y-m-d', strtotime($row['sub_days'])), 'Subtracting date should end up on 2009-12-22'); $bindParams,
self::assertEquals('2010-01-08', date('Y-m-d', strtotime($row['add_weeks'])), 'Adding week should end up on 2010-01-08'); static function (AbstractPlatform $platform, string $interval) : string {
self::assertEquals('2009-12-25', date('Y-m-d', strtotime($row['sub_weeks'])), 'Subtracting week should end up on 2009-12-25'); return $platform->getDateSubQuartersExpression('test_datetime', $interval);
self::assertEquals('2010-03-01', date('Y-m-d', strtotime($row['add_month'])), 'Adding month should end up on 2010-03-01'); },
self::assertEquals('2009-11-01', date('Y-m-d', strtotime($row['sub_month'])), 'Subtracting month should end up on 2009-11-01'); 3,
self::assertEquals('2010-10-01', date('Y-m-d', strtotime($row['add_quarters'])), 'Adding quarters should end up on 2010-04-01'); '2009-04-01 10:10:10'
self::assertEquals('2009-04-01', date('Y-m-d', strtotime($row['sub_quarters'])), 'Subtracting quarters should end up on 2009-10-01'); );
self::assertEquals('2016-01-01', date('Y-m-d', strtotime($row['add_years'])), 'Adding years should end up on 2016-01-01'); }
self::assertEquals('2004-01-01', date('Y-m-d', strtotime($row['sub_years'])), 'Subtracting years should end up on 2004-01-01');
/**
* @dataProvider modeProvider
*/
public function testDateAddYears(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateAddYearsExpression('test_datetime', $interval);
},
6,
'2016-01-01 10:10:10'
);
}
/**
* @dataProvider modeProvider
*/
public function testDateSubYears(callable $buildQuery, callable $bindParams) : void
{
$this->assertDateExpression(
$buildQuery,
$bindParams,
static function (AbstractPlatform $platform, string $interval) : string {
return $platform->getDateSubYearsExpression('test_datetime', $interval);
},
6,
'2004-01-01 10:10:10'
);
}
/**
* @param callable $buildQuery Builds the portion of the query representing the interval value
* @param callable $bindParams Binds the interval value to the statement
* @param callable $expression Builds the platform-specific interval expression
* @param int $interval Interval value
* @param string $expected Expected value
*/
private function assertDateExpression(callable $buildQuery, callable $bindParams, callable $expression, int $interval, string $expected) : void
{
$connection = $this->connection;
$platform = $connection->getDatabasePlatform();
$query = sprintf('SELECT %s FROM fetch_table', $expression($platform, $buildQuery($interval)));
$stmt = $connection->prepare($query);
$bindParams($stmt, $interval);
$stmt->execute();
$date = $stmt->fetchColumn();
$this->assertEquals($expected, date('Y-m-d H:i:s', strtotime($date)));
}
/**
* @return mixed[][]
*/
public static function modeProvider() : array
{
return [
'bind' => [
static function (int $interval) : string {
return '?';
},
static function (Statement $stmt, int $interval) : void {
$stmt->bindParam(1, $interval, ParameterType::INTEGER);
},
],
'literal' => [
static function (int $interval) : string {
return sprintf('%d', $interval);
},
static function (Statement $stmt, int $interval) : void {
},
],
'expression' => [
static function (int $interval) : string {
return sprintf('(0 + %d)', $interval);
},
static function (Statement $stmt, int $interval) : void {
},
],
];
} }
public function testSqliteDateArithmeticWithDynamicInterval() : void public function testSqliteDateArithmeticWithDynamicInterval() : void
......
...@@ -348,18 +348,18 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -348,18 +348,18 @@ class DB2PlatformTest extends AbstractPlatformTestCase
self::assertEquals("'1987/05/02' + 12 HOUR", $this->platform->getDateAddHourExpression("'1987/05/02'", 12)); self::assertEquals("'1987/05/02' + 12 HOUR", $this->platform->getDateAddHourExpression("'1987/05/02'", 12));
self::assertEquals("'1987/05/02' + 2 MINUTE", $this->platform->getDateAddMinutesExpression("'1987/05/02'", 2)); self::assertEquals("'1987/05/02' + 2 MINUTE", $this->platform->getDateAddMinutesExpression("'1987/05/02'", 2));
self::assertEquals("'1987/05/02' + 102 MONTH", $this->platform->getDateAddMonthExpression("'1987/05/02'", 102)); self::assertEquals("'1987/05/02' + 102 MONTH", $this->platform->getDateAddMonthExpression("'1987/05/02'", 102));
self::assertEquals("'1987/05/02' + 15 MONTH", $this->platform->getDateAddQuartersExpression("'1987/05/02'", 5)); self::assertEquals("'1987/05/02' + (5 * 3) MONTH", $this->platform->getDateAddQuartersExpression("'1987/05/02'", 5));
self::assertEquals("'1987/05/02' + 1 SECOND", $this->platform->getDateAddSecondsExpression("'1987/05/02'", 1)); self::assertEquals("'1987/05/02' + 1 SECOND", $this->platform->getDateAddSecondsExpression("'1987/05/02'", 1));
self::assertEquals("'1987/05/02' + 21 DAY", $this->platform->getDateAddWeeksExpression("'1987/05/02'", 3)); self::assertEquals("'1987/05/02' + (3 * 7) DAY", $this->platform->getDateAddWeeksExpression("'1987/05/02'", 3));
self::assertEquals("'1987/05/02' + 10 YEAR", $this->platform->getDateAddYearsExpression("'1987/05/02'", 10)); self::assertEquals("'1987/05/02' + 10 YEAR", $this->platform->getDateAddYearsExpression("'1987/05/02'", 10));
self::assertEquals("DAYS('1987/05/02') - DAYS('1987/04/01')", $this->platform->getDateDiffExpression("'1987/05/02'", "'1987/04/01'")); self::assertEquals("DAYS('1987/05/02') - DAYS('1987/04/01')", $this->platform->getDateDiffExpression("'1987/05/02'", "'1987/04/01'"));
self::assertEquals("'1987/05/02' - 4 DAY", $this->platform->getDateSubDaysExpression("'1987/05/02'", 4)); self::assertEquals("'1987/05/02' - 4 DAY", $this->platform->getDateSubDaysExpression("'1987/05/02'", 4));
self::assertEquals("'1987/05/02' - 12 HOUR", $this->platform->getDateSubHourExpression("'1987/05/02'", 12)); self::assertEquals("'1987/05/02' - 12 HOUR", $this->platform->getDateSubHourExpression("'1987/05/02'", 12));
self::assertEquals("'1987/05/02' - 2 MINUTE", $this->platform->getDateSubMinutesExpression("'1987/05/02'", 2)); self::assertEquals("'1987/05/02' - 2 MINUTE", $this->platform->getDateSubMinutesExpression("'1987/05/02'", 2));
self::assertEquals("'1987/05/02' - 102 MONTH", $this->platform->getDateSubMonthExpression("'1987/05/02'", 102)); self::assertEquals("'1987/05/02' - 102 MONTH", $this->platform->getDateSubMonthExpression("'1987/05/02'", 102));
self::assertEquals("'1987/05/02' - 15 MONTH", $this->platform->getDateSubQuartersExpression("'1987/05/02'", 5)); self::assertEquals("'1987/05/02' - (5 * 3) MONTH", $this->platform->getDateSubQuartersExpression("'1987/05/02'", 5));
self::assertEquals("'1987/05/02' - 1 SECOND", $this->platform->getDateSubSecondsExpression("'1987/05/02'", 1)); self::assertEquals("'1987/05/02' - 1 SECOND", $this->platform->getDateSubSecondsExpression("'1987/05/02'", 1));
self::assertEquals("'1987/05/02' - 21 DAY", $this->platform->getDateSubWeeksExpression("'1987/05/02'", 3)); self::assertEquals("'1987/05/02' - (3 * 7) DAY", $this->platform->getDateSubWeeksExpression("'1987/05/02'", 3));
self::assertEquals("'1987/05/02' - 10 YEAR", $this->platform->getDateSubYearsExpression("'1987/05/02'", 10)); self::assertEquals("'1987/05/02' - 10 YEAR", $this->platform->getDateSubYearsExpression("'1987/05/02'", 10));
self::assertEquals(' WITH RR USE AND KEEP UPDATE LOCKS', $this->platform->getForUpdateSQL()); self::assertEquals(' WITH RR USE AND KEEP UPDATE LOCKS', $this->platform->getForUpdateSQL());
self::assertEquals('LOCATE(substring_column, string_column)', $this->platform->getLocateExpression('string_column', 'substring_column')); self::assertEquals('LOCATE(substring_column, string_column)', $this->platform->getLocateExpression('string_column', 'substring_column'));
......
...@@ -791,12 +791,12 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -791,12 +791,12 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
public function testDateAddStaticNumberOfDays() : void public function testDateAddStaticNumberOfDays() : void
{ {
self::assertSame("DATE(rentalBeginsOn,'+12 DAY')", $this->platform->getDateAddDaysExpression('rentalBeginsOn', 12)); self::assertSame("DATETIME(rentalBeginsOn,'+' || 12 || ' DAY')", $this->platform->getDateAddDaysExpression('rentalBeginsOn', 12));
} }
public function testDateAddNumberOfDaysFromColumn() : void public function testDateAddNumberOfDaysFromColumn() : void
{ {
self::assertSame("DATE(rentalBeginsOn,'+' || duration || ' DAY')", $this->platform->getDateAddDaysExpression('rentalBeginsOn', 'duration')); self::assertSame("DATETIME(rentalBeginsOn,'+' || duration || ' DAY')", $this->platform->getDateAddDaysExpression('rentalBeginsOn', 'duration'));
} }
public function testSupportsColumnCollation() : void public function testSupportsColumnCollation() : void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment