Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Just an idea to avoid limitations in date intervals to days and months on more flexible DBMS ... #62

Closed
wants to merge 5 commits into from

5 participants

@cariboo

Different implementation of the date/time manipulation functions so that those are no more limited to days and months intervals in PostgreSQL without impacting over DBMS (and can be improved later by developers with sufficient skills on the other DBMS)

This pull request should be associated with the one i sent on the doctrine main lib.

@sdecleire sdecleire Different implementation of the date/time manipulation functions so t…
…hat those are no more limited to days and months intervals in PostgreSQL without impacting over DBMS (and can be improved later by developers with sufficient skills on the other DBMS)
926cdda
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
((7 lines not shown))
- return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
- }
-
- public function getDateSubDaysExpression($date, $days)
- {
- return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
- }
-
- public function getDateAddMonthExpression($date, $months)
- {
- return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
+ if ($unit == "day") {
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
+ } else if ($unit == "month") {
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
+ } else {
@stof
stof added a note

please fix the indentation. You should use spaces, not tabs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
((6 lines not shown))
{
- return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
- }
-
- public function getDateSubDaysExpression($date, $days)
- {
- return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
- }
-
- public function getDateAddMonthExpression($date, $months)
- {
- return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
+ if ($unit == "day") {
@stof
stof added a note

what is $unit ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
((29 lines not shown))
{
- return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)';
+ if ($unit == "day") {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
+ } else if ($unit == "month") {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)';
@stof
stof added a note

same issues for this method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/DBAL/Platforms/OraclePlatform.php
((6 lines not shown))
{
- return '(' . $date . '+' . $days . ')';
- }
-
- public function getDateSubDaysExpression($date, $days)
- {
- return '(' . $date . '-' . $days . ')';
+ if ($unit == "day") {
+ return '(' . $date . '+' . $days . ')';
+ } else if ($unit == "month") {
@stof
stof added a note

same issues here. The indentation is wrong and $unit is undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
((6 lines not shown))
{
- return "DATE(" . $date . ",'+". $days . " day')";
- }
-
- public function getDateSubDaysExpression($date, $days)
- {
- return "DATE(" . $date . ",'-". $days . " day')";
- }
-
- public function getDateAddMonthExpression($date, $months)
- {
- return "DATE(" . $date . ",'+". $months . " month')";
+ if ($unit == "day") {
+ return "DATE(" . $date . ",'+". $days . " day')";
+ } else if ($unit == "month") {
@stof
stof added a note

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof

Another point: DBAL is stable so the BC should be kept. So you should not remove the previous methods

@cariboo

I have made the corrections you requested : sorry for the tabs (bad config of my editor) and for the evil cut and paste (i have not other DBMS than PostgreSQL to test).
I have a question about the BC of DBAL : the functions i have removed are no more called and are in the platform specific implementations, not in the "public interface" of the library so they should not be called directly, should they ?

@stof

DBAL is not only for the ORM. It is a separate project that may be used by other people outside the ORM. Also, the platforms are part of the public interface as they are the way to get the needed SQL to be cross-platform.

@cariboo

Ok, thanks for the explanation. I get back those functions right now.

@beberlei
Owner

Did you run the tests with this? I suppose they fail now.

Also what does this patch achieve? It seems to me really nothing by default. DBAL is about abstracting database differences, and I think only day and month can be done by all of them.

@asm89
Collaborator

Closing this as the common denominator between all DBAL supported RDMSes seems to be date function with day and month.

@asm89 asm89 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 27, 2011
  1. @sdecleire

    Different implementation of the date/time manipulation functions so t…

    sdecleire authored
    …hat those are no more limited to days and months intervals in PostgreSQL without impacting over DBMS (and can be improved later by developers with sufficient skills on the other DBMS)
Commits on Sep 28, 2011
  1. @sdecleire
  2. @sdecleire
Commits on Oct 3, 2011
  1. @cariboo

    Functions that handle intervals are renamed to avoid conflicts with e…

    cariboo authored
    …xisting functions of the DBAL
Commits on Jan 3, 2012
  1. Mise à jour à la dernière version de DBAL

    Stephane authored
This page is out of date. Refresh to see the latest.
View
50 lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -785,25 +785,40 @@ public function getDateDiffExpression($date1, $date2)
}
/**
- * Add the number of given days to a date.
+ * Calculate the difference between the two passed dates as an interval.
+ *
+ * Computes diff = date1 - date2
+ *
+ * @param string $date1
+ * @param string $date2
+ * @return string
+ */
+ public function getDateDiffIntervalExpression($date1, $date2)
+ {
+ throw DBALException::notSupported(__METHOD__);
+ }
+
+ /**
+ * Add the number of given units to a date.
*
* @param string $date
- * @param int $days
+ * @param int $value
+ * @param string $unit
* @return string
*/
- public function getDateAddDaysExpression($date, $days)
+ public function getDateAddIntervalExpression($date, $value, $unit)
{
throw DBALException::notSupported(__METHOD__);
}
/**
- * Substract the number of given days to a date.
+ * Add the number of given days to a date.
*
* @param string $date
* @param int $days
* @return string
*/
- public function getDateSubDaysExpression($date, $days)
+ public function getDateAddDaysExpression($date, $days)
{
throw DBALException::notSupported(__METHOD__);
}
@@ -821,6 +836,31 @@ public function getDateAddMonthExpression($date, $months)
}
/**
+ * Substract the number of given units from a date.
+ *
+ * @param string $date
+ * @param int $value
+ * @param string $unit
+ * @return string
+ */
+ public function getDateSubIntervalExpression($date, $value, $unit)
+ {
+ throw DBALException::notSupported(__METHOD__);
+ }
+
+ /**
+ * Substract the number of given days to a date.
+ *
+ * @param string $date
+ * @param int $days
+ * @return string
+ */
+ public function getDateSubDaysExpression($date, $days)
+ {
+ throw DBALException::notSupported(__METHOD__);
+ }
+
+ /**
* Substract the number of given months to a date.
*
* @param string $date
View
67 lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
@@ -101,26 +101,85 @@ public function getConcatExpression()
return 'CONCAT(' . join(', ', (array) $args) . ')';
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateDiffExpression($date1, $date2)
{
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
}
- public function getDateAddDaysExpression($date, $days)
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateAddIntervalExpression($date, $value, $unit)
{
- return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
+ $unitl = strtolower($unit);
+ if ($unitl == "day") {
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $value . ' DAY)';
+ } else if ($unitl == "month") {
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $value . ' MONTH)';
+ } else {
+ throw QueryException::semanticalError('DATE_ADD() only supports units of type day and month.');
+ }
}
- public function getDateSubDaysExpression($date, $days)
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateAddDaysExpression($date, $days)
{
- return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
+ return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateAddMonthExpression($date, $months)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateSubIntervalExpression($date, $value, $unit)
+ {
+ $unitl = strtolower($unit);
+ if ($unitl == "day") {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $value . ' DAY)';
+ } else if ($unitl == "month") {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $value . ' MONTH)';
+ } else {
+ throw QueryException::semanticalError('DATE_SUB() only supports units of type day and month.');
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateSubDaysExpression($date, $days)
+ {
+ return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateSubMonthExpression($date, $months)
{
return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)';
View
11 lib/Doctrine/DBAL/Platforms/OraclePlatform.php
@@ -121,7 +121,14 @@ public function getDateDiffExpression($date1, $date2)
*/
public function getDateAddDaysExpression($date, $days)
{
- return '(' . $date . '+' . $days . ')';
+ $unitl = strtolower($unit);
+ if ($unitl == "day") {
+ return '(' . $date . '+' . $value . ')';
+ } else if ($unitl == "month") {
+ return "ADD_MONTHS(" . $date . ", " . $value . ")";
+ } else {
+ throw QueryException::semanticalError('DATE_ADD() only supports units of type day and month.');
+ }
}
/**
@@ -129,7 +136,7 @@ public function getDateAddDaysExpression($date, $days)
*/
public function getDateSubDaysExpression($date, $days)
{
- return '(' . $date . '-' . $days . ')';
+ return '(' . $date . '+' . $days . ')';
}
/**
View
79 lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
@@ -92,26 +92,81 @@ public function getLocateExpression($str, $substr, $startPos = false)
}
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateDiffExpression($date1, $date2)
{
return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
}
- public function getDateAddDaysExpression($date, $days)
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateDiffIntervalExpression($date1, $date2)
{
return "(" . $date ." + (" . $days . " || ' day')::interval)";
}
- public function getDateSubDaysExpression($date, $days)
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateAddIntervalExpression($date, $value, $unit)
{
return "(" . $date ." - (" . $days . " || ' day')::interval)";
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateAddDaysExpression($date, $days)
+ {
+ return "(" . $date . "+ interval '" . $days . " day')";
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateAddMonthExpression($date, $months)
{
return "(" . $date ." + (" . $months . " || ' month')::interval)";
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateSubIntervalExpression($date, $value, $unit)
+ {
+ return "(" . $date . "- interval '" . $value . " " . $unit . "')";
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateSubDaysExpression($date, $days)
+ {
+ return "(" . $date . "- interval '" . $days . " day')";
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateSubMonthExpression($date, $months)
{
return "(" . $date ." - (" . $months . " || ' month')::interval)";
@@ -651,6 +706,26 @@ public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
}
/**
+ * Gets the SQL specific for the platform to get the current timestamp
+ *
+ * @return string
+ */
+ public function getCurrentTimestampSQL()
+ {
+ return 'CURRENT_TIMESTAMP(0)';
+ }
+
+ /**
+ * Gets the SQL specific for the platform to get the current time.
+ *
+ * @return string
+ */
+ public function getCurrentTimeSQL()
+ {
+ return 'CURRENT_TIME(0)';
+ }
+
+ /**
* @override
*/
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
View
69 lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
@@ -125,31 +125,90 @@ public function getLocateExpression($str, $substr, $startPos = false)
}
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateDiffExpression($date1, $date2)
{
return 'ROUND(JULIANDAY('.$date1 . ')-JULIANDAY('.$date2.'))';
}
- public function getDateAddDaysExpression($date, $days)
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateAddIntervalExpression($date, $value, $unit)
{
- return "DATE(" . $date . ",'+". $days . " day')";
+ $unitl = strtolower($unit);
+ if ($unitl == "day") {
+ return "DATE(" . $date . ",'+". $value . " day')";
+ } else if ($unitl == "month") {
+ return "DATE(" . $date . ",'+". $value . " month')";
+ } else {
+ throw QueryException::semanticalError('DATE_ADD() only supports units of type day and month.');
+ }
}
- public function getDateSubDaysExpression($date, $days)
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateAddDaysExpression($date, $days)
{
- return "DATE(" . $date . ",'-". $days . " day')";
+ return "DATE(" . $date . ",'+". $days . " day')";
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateAddMonthExpression($date, $months)
{
return "DATE(" . $date . ",'+". $months . " month')";
}
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateSubIntervalExpression($date, $value, $unit)
+ {
+ $unitl = strtolower($unit);
+ if ($unitl == "day") {
+ return "DATE(" . $date . ",'-". $value . " day')";
+ } else if ($unitl == "month") {
+ return "DATE(" . $date . ",'-". $value . " month')";
+ } else {
+ throw QueryException::semanticalError('DATE_SUB() only supports units of type day and month.');
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
+ public function getDateSubDaysExpression($date, $days)
+ {
+ return "DATE(" . $date . ",'-". $days . " day')";
+ }
+
+ /**
+ * {@inheritdoc}
+ *
+ * @override
+ */
public function getDateSubMonthExpression($date, $months)
{
return "DATE(" . $date . ",'-". $months . " month')";
}
-
+
protected function _getTransactionIsolationLevelSQL($level)
{
switch ($level) {
Something went wrong with that request. Please try again.