From 26ff265652bc4824ff585b9915c3a5bcb2d82b30 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Wed, 14 Apr 2010 23:27:33 -0300 Subject: [PATCH] [2.0][DDC-431] Added coverage, fixing the ticket. --- lib/Doctrine/ORM/Configuration.php | 42 ++++++++++++++++- .../ORM/Query/SelectSqlGenerationTest.php | 47 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 609bbfcd1..229126635 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -50,7 +50,14 @@ class Configuration extends \Doctrine\DBAL\Configuration 'proxyDir' => null, 'useCExtension' => false, 'autoGenerateProxyClasses' => true, - 'proxyNamespace' => null + 'proxyNamespace' => null, + 'entityNamespaces' => array(), + 'namedNativeQueries' => array(), + 'namedQueries' => array(), + // Custom DQL Functions + 'customDatetimeFunctions' => array(), + 'customNumericFunctions' => array(), + 'customStringFunctions' => array() )); } @@ -366,10 +373,21 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getCustomStringFunction($name) { + $name = strtolower($name); + return isset($this->_attributes['customStringFunctions'][$name]) ? $this->_attributes['customStringFunctions'][$name] : null; } + /** + * Clean custom DQL functions that produces string values. + * + */ + public function clearCustomStringFunctions() + { + $this->_attributes['customStringFunctions'] = array(); + } + /** * Registers a custom DQL function that produces a numeric value. * Such a function can then be used in any DQL statement in any place where numeric @@ -391,10 +409,21 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getCustomNumericFunction($name) { + $name = strtolower($name); + return isset($this->_attributes['customNumericFunctions'][$name]) ? $this->_attributes['customNumericFunctions'][$name] : null; } + /** + * Clean custom DQL functions that produces numeric values. + * + */ + public function clearCustomNumericFunctions() + { + $this->_attributes['customNumericFunctions'] = array(); + } + /** * Registers a custom DQL function that produces a date/time value. * Such a function can then be used in any DQL statement in any place where date/time @@ -416,7 +445,18 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function getCustomDatetimeFunction($name) { + $name = strtolower($name); + return isset($this->_attributes['customDatetimeFunctions'][$name]) ? $this->_attributes['customDatetimeFunctions'][$name] : null; } + + /** + * Clean custom DQL functions that produces date/time values. + * + */ + public function clearCustomDatetimeFunctions() + { + $this->_attributes['customDatetimeFunctions'] = array(); + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index dda612152..1ef832571 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -596,4 +596,51 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ WHERE c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ? OR c0_.id = ?" ); } + + /** + * DDC-431 + */ + public function testSupportToCustomDQLFunctions() + { + $config = $this->_em->getConfiguration(); + $config->addCustomNumericFunction('MYABS', 'Doctrine\Tests\ORM\Query\MyAbsFunction'); + + $this->assertSqlGeneration( + 'SELECT MYABS(p.phonenumber) FROM Doctrine\Tests\Models\CMS\CmsPhonenumber p', + 'SELECT ABS(c0_.phonenumber) AS sclr0 FROM cms_phonenumbers c0_' + ); + + $config->clearCustomNumericFunctions(); + } } + + +class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode +{ + public $simpleArithmeticExpression; + + /** + * @override + */ + public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) + { + return 'ABS(' . $sqlWalker->walkSimpleArithmeticExpression( + $this->simpleArithmeticExpression + ) . ')'; + } + + /** + * @override + */ + public function parse(\Doctrine\ORM\Query\Parser $parser) + { + $lexer = $parser->getLexer(); + + $parser->match(\Doctrine\ORM\Query\Lexer::T_IDENTIFIER); + $parser->match(\Doctrine\ORM\Query\Lexer::T_OPEN_PARENTHESIS); + + $this->simpleArithmeticExpression = $parser->SimpleArithmeticExpression(); + + $parser->match(\Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS); + } +} \ No newline at end of file