Skip to content

Commit

Permalink
Merge pull request #6500 from SamKnows/feature/override-aggregate-fun…
Browse files Browse the repository at this point in the history
…ctions

Allow internal functions to be overridden.
  • Loading branch information
Ocramius committed Jun 20, 2017
2 parents e80cd74 + 747c185 commit 2560912
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 89 deletions.
14 changes: 14 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Upgrade to 2.6

## Minor BC BREAK: removed `Doctrine\ORM\Query\Parser#isInternalFunction`

Method `Doctrine\ORM\Query\Parser#isInternalFunction` was removed because
the distinction between internal function and user defined DQL was removed.
[#6500](https://github.com/doctrine/doctrine2/pull/6500)

## Minor BC BREAK: removed `Doctrine\ORM\ORMException#overwriteInternalDQLFunctionNotAllowed`

Method `Doctrine\ORM\Query\Parser#overwriteInternalDQLFunctionNotAllowed` was
removed because of the choice to allow users to overwrite internal functions, ie
`AVG`, `SUM`, `COUNT`, `MIN` and `MAX`. [#6500](https://github.com/doctrine/doctrine2/pull/6500)

# Upgrade to 2.5

## Minor BC BREAK: removed `Doctrine\ORM\Query\SqlWalker#walkCaseExpression()`
Expand Down
18 changes: 0 additions & 18 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,9 @@ public function ensureProductionSettings()
* @param string|callable $className Class name or a callable that returns the function.
*
* @return void
*
* @throws ORMException
*/
public function addCustomStringFunction($name, $className)
{
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}

$this->_attributes['customStringFunctions'][strtolower($name)] = $className;
}

Expand Down Expand Up @@ -478,15 +472,9 @@ public function setCustomStringFunctions(array $functions)
* @param string|callable $className Class name or a callable that returns the function.
*
* @return void
*
* @throws ORMException
*/
public function addCustomNumericFunction($name, $className)
{
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}

$this->_attributes['customNumericFunctions'][strtolower($name)] = $className;
}

Expand Down Expand Up @@ -536,15 +524,9 @@ public function setCustomNumericFunctions(array $functions)
* @param string|callable $className Class name or a callable that returns the function.
*
* @return void
*
* @throws ORMException
*/
public function addCustomDatetimeFunction($name, $className)
{
if (Query\Parser::isInternalFunction($name)) {
throw ORMException::overwriteInternalDQLFunctionNotAllowed($name);
}

$this->_attributes['customDatetimeFunctions'][strtolower($name)] = $className;
}

Expand Down
10 changes: 0 additions & 10 deletions lib/Doctrine/ORM/ORMException.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,6 @@ public static function unrecognizedIdentifierFields($className, $fieldNames)
);
}

/**
* @param string $functionName
*
* @return ORMException
*/
public static function overwriteInternalDQLFunctionNotAllowed($functionName)
{
return new self("It is not allowed to overwrite internal function '$functionName' in the DQL parser through user-defined functions.");
}

/**
* @return ORMException
*/
Expand Down
54 changes: 54 additions & 0 deletions lib/Doctrine/ORM/Query/AST/Functions/AvgFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\AggregateExpression;

/**
* "AVG" "(" ["DISTINCT"] StringPrimary ")"
*
* @since 2.6
* @author Mathew Davies <thepixeldeveloper@icloud.com>
*/
final class AvgFunction extends FunctionNode
{
/**
* @var AggregateExpression
*/
private $aggregateExpression;

/**
* @inheritDoc
*/
public function getSql(SqlWalker $sqlWalker): string
{
return $this->aggregateExpression->dispatch($sqlWalker);
}

/**
* @inheritDoc
*/
public function parse(Parser $parser): void
{
$this->aggregateExpression = $parser->AggregateExpression();
}
}
54 changes: 54 additions & 0 deletions lib/Doctrine/ORM/Query/AST/Functions/CountFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\AggregateExpression;

/**
* "COUNT" "(" ["DISTINCT"] StringPrimary ")"
*
* @since 2.6
* @author Mathew Davies <thepixeldeveloper@icloud.com>
*/
final class CountFunction extends FunctionNode
{
/**
* @var AggregateExpression
*/
private $aggregateExpression;

/**
* @inheritDoc
*/
public function getSql(SqlWalker $sqlWalker): string
{
return $this->aggregateExpression->dispatch($sqlWalker);
}

/**
* @inheritDoc
*/
public function parse(Parser $parser): void
{
$this->aggregateExpression = $parser->AggregateExpression();
}
}
54 changes: 54 additions & 0 deletions lib/Doctrine/ORM/Query/AST/Functions/MaxFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\AggregateExpression;

/**
* "MAX" "(" ["DISTINCT"] StringPrimary ")"
*
* @since 2.6
* @author Mathew Davies <thepixeldeveloper@icloud.com>
*/
final class MaxFunction extends FunctionNode
{
/**
* @var AggregateExpression
*/
private $aggregateExpression;

/**
* @inheritDoc
*/
public function getSql(SqlWalker $sqlWalker): string
{
return $this->aggregateExpression->dispatch($sqlWalker);
}

/**
* @inheritDoc
*/
public function parse(Parser $parser): void
{
$this->aggregateExpression = $parser->AggregateExpression();
}
}
54 changes: 54 additions & 0 deletions lib/Doctrine/ORM/Query/AST/Functions/MinFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\AggregateExpression;

/**
* "MIN" "(" ["DISTINCT"] StringPrimary ")"
*
* @since 2.6
* @author Mathew Davies <thepixeldeveloper@icloud.com>
*/
final class MinFunction extends FunctionNode
{
/**
* @var AggregateExpression
*/
private $aggregateExpression;

/**
* @inheritDoc
*/
public function getSql(SqlWalker $sqlWalker): string
{
return $this->aggregateExpression->dispatch($sqlWalker);
}

/**
* @inheritDoc
*/
public function parse(Parser $parser): void
{
$this->aggregateExpression = $parser->AggregateExpression();
}
}
54 changes: 54 additions & 0 deletions lib/Doctrine/ORM/Query/AST/Functions/SumFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\AggregateExpression;

/**
* "SUM" "(" ["DISTINCT"] StringPrimary ")"
*
* @since 2.6
* @author Mathew Davies <thepixeldeveloper@icloud.com>
*/
final class SumFunction extends FunctionNode
{
/**
* @var AggregateExpression
*/
private $aggregateExpression;

/**
* @inheritDoc
*/
public function getSql(SqlWalker $sqlWalker): string
{
return $this->aggregateExpression->dispatch($sqlWalker);
}

/**
* @inheritDoc
*/
public function parse(Parser $parser): void
{
$this->aggregateExpression = $parser->AggregateExpression();
}
}
Loading

0 comments on commit 2560912

Please sign in to comment.