-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from orocrm/ticket/BAP-5305_timestampdiff
BAP-5305: Add TIMESTAMPDIFF function
- Loading branch information
Showing
43 changed files
with
560 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Oro\ORM\Query\AST\Functions\Numeric; | ||
|
||
use Doctrine\ORM\Query\Parser; | ||
use Doctrine\ORM\Query\Lexer; | ||
use Oro\ORM\Query\AST\Functions\AbstractPlatformAwareFunctionNode; | ||
|
||
class TimestampDiff extends AbstractPlatformAwareFunctionNode | ||
{ | ||
const UNIT_KEY = 'unit'; | ||
const VAL1_KEY = 'val1'; | ||
const VAL2_KEY = 'val2'; | ||
|
||
/** | ||
* List of supported units. | ||
* | ||
* @var array | ||
*/ | ||
protected $supportedUnits = array( | ||
'MICROSECOND', | ||
'SECOND', | ||
'MINUTE', | ||
'HOUR', | ||
'DAY', | ||
'WEEK', | ||
'MONTH', | ||
'QUARTER', | ||
'YEAR' | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parse(Parser $parser) | ||
{ | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
$parser->match(Lexer::T_OPEN_PARENTHESIS); | ||
$parser->match(Lexer::T_IDENTIFIER); | ||
|
||
$lexer = $parser->getLexer(); | ||
$unit = strtoupper(trim($lexer->token['value'])); | ||
if (!$this->checkUnit($unit)) { | ||
$parser->syntaxError( | ||
sprintf( | ||
'Unit is not valid for TIMESTAMPDIFF function. Supported units are: "%s"', | ||
implode(', ', $this->supportedUnits) | ||
), | ||
$lexer->token | ||
); | ||
} | ||
|
||
$this->parameters[self::UNIT_KEY] = $unit; | ||
$parser->match(Lexer::T_COMMA); | ||
$this->parameters[self::VAL1_KEY] = $parser->ArithmeticPrimary(); | ||
$parser->match(Lexer::T_COMMA); | ||
$this->parameters[self::VAL2_KEY] = $parser->ArithmeticPrimary(); | ||
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | ||
} | ||
|
||
/** | ||
* Check that unit is supported. | ||
* | ||
* @param string $unit | ||
* @return bool | ||
*/ | ||
protected function checkUnit($unit) | ||
{ | ||
return in_array($unit, $this->supportedUnits); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Oro/ORM/Query/AST/Platform/Functions/Mysql/Timestampdiff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Oro\ORM\Query\AST\Platform\Functions\Mysql; | ||
|
||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
use Oro\ORM\Query\AST\Functions\Numeric\TimestampDiff as BaseFunction; | ||
use Oro\ORM\Query\AST\Platform\Functions\PlatformFunctionNode; | ||
|
||
class Timestampdiff extends PlatformFunctionNode | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSql(SqlWalker $sqlWalker) | ||
{ | ||
/** @var string $unit */ | ||
$unit = $this->parameters[BaseFunction::UNIT_KEY]; | ||
/** @var Node $val1 */ | ||
$val1 = $this->parameters[BaseFunction::VAL1_KEY]; | ||
/** @var Node $val2 */ | ||
$val2 = $this->parameters[BaseFunction::VAL2_KEY]; | ||
|
||
return 'TIMESTAMPDIFF(' | ||
. $unit | ||
. ', ' | ||
. $this->getExpressionValue($val1, $sqlWalker) | ||
. ', ' | ||
. $this->getExpressionValue($val2, $sqlWalker) | ||
. ')'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...RM/Query/AST/Platform/Functions/Postgresql/AbstractTimestampAwarePlatformFunctionNode.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Oro\ORM\Query\AST\Platform\Functions\Postgresql; | ||
|
||
use Doctrine\ORM\Query\AST\Node; | ||
use Doctrine\ORM\Query\SqlWalker; | ||
use Oro\ORM\Query\AST\Functions\SimpleFunction; | ||
use Oro\ORM\Query\AST\Platform\Functions\PlatformFunctionNode; | ||
|
||
abstract class AbstractTimestampAwarePlatformFunctionNode extends PlatformFunctionNode | ||
{ | ||
/** | ||
* Get timestamp value for given expression. | ||
* | ||
* @param Node|string $expression | ||
* @param SqlWalker $sqlWalker | ||
* @return string | ||
*/ | ||
protected function getTimestampValue($expression, SqlWalker $sqlWalker) | ||
{ | ||
$value = $this->getExpressionValue($expression, $sqlWalker); | ||
$value = trim(trim($value), '\'"'); | ||
if (is_numeric(substr($value, 0, 1))) { | ||
$timestampFunction = new Timestamp(array(SimpleFunction::PARAMETER_KEY => "'$value'")); | ||
$value = $timestampFunction->getSql($sqlWalker); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.