Skip to content

Commit

Permalink
copy fix from chihiro-adachi for mysql 5.7
Browse files Browse the repository at this point in the history
copy
fixed doctrine#5622 - mysql 5.7 ONLY_FULL_GROUP_BY doctrine#6143
chihiro-adachi  wants to merge 2 commits into doctrine:master from chihiro-adachi:patch-1
  • Loading branch information
paaacman committed May 19, 2017
1 parent d7f845f commit 2a7b534
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<?php
/**
* Doctrine ORM
*
* LICENSE
/*
* 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 source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
* 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\Tools\Pagination;
Expand All @@ -18,18 +24,9 @@
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\ORM\Query\AST\ArithmeticExpression;
use Doctrine\ORM\Query\AST\ArithmeticFactor;
use Doctrine\ORM\Query\AST\ArithmeticTerm;
use Doctrine\ORM\Query\AST\Literal;
use Doctrine\ORM\Query\AST\OrderByClause;
use Doctrine\ORM\Query\AST\OrderByItem;
use Doctrine\ORM\Query\AST\PartialObjectExpression;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\AST\SelectExpression;
use Doctrine\ORM\Query\AST\SimpleArithmeticExpression;
use Doctrine\ORM\Query\Expr\OrderBy;
use Doctrine\ORM\Query\Expr\Select;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\SelectStatement;

Expand Down Expand Up @@ -376,14 +373,30 @@ private function preserveSqlOrdering(array $sqlIdentifier, $innerSql, $sql, $ord
/* @var array $orderBy an array of rebuilt order by items */
$orderBy = $this->rebuildOrderByClauseForOuterScope($orderByClause);

// Build the select distinct statement
$innerSqlIdentifier = $sqlIdentifier;

foreach ($orderBy as $field) {
$field = preg_replace('/((\S+)\s+(ASC|DESC)\s*,?)*/', '${2}', $field);

// skip fields that are selected by identifiers,
// if those are ordered by in the query
if (in_array($field, $sqlIdentifier, true)) {
continue;
}
$innerSqlIdentifier[] = $field;
}

// Build the innner select statement
$sql = sprintf(
'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s',
implode(', ', $sqlIdentifier),
'SELECT DISTINCT %s FROM (%s) dctrn_result_inner ORDER BY %s',
implode(', ', $innerSqlIdentifier),
$innerSql,
implode(', ', $orderBy)
);

// now only select distinct identifier
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', implode(', ', $sqlIdentifier), $sql);

return $sql;
}

Expand Down Expand Up @@ -440,7 +453,6 @@ private function rebuildOrderByClauseForOuterScope(OrderByClause $orderByClause)
$otherClassMetadata = $this->em->getClassMetadata($fieldMapping['declared']);
if (!$otherClassMetadata->isMappedSuperclass) {
$sqlTableAliasForFieldAlias = $this->getSQLTableAlias($otherClassMetadata->getTableName(), $dqlAliasForFieldAlias);

}
}

Expand Down

0 comments on commit 2a7b534

Please sign in to comment.