1
0
mirror of synced 2025-02-03 05:49:25 +03:00

Should fix MySQL 5.7 issues caused by ONLY_FULL_GROUP_BY

Should fix MySQL 5.7 issues caused by ONLY_FULL_GROUP_BY
This commit is contained in:
Martin Kirilov 2016-08-12 17:24:00 +03:00 committed by Luís Cobucci
parent 668ad4cc29
commit 854ff17ab9
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C

View File

@ -354,33 +354,56 @@ class LimitSubqueryOutputWalker extends SqlWalker
/** /**
* Generates new SQL for statements with an order by clause * Generates new SQL for statements with an order by clause
* *
* @param array $sqlIdentifier * @param array $sqlIdentifier
* @param string $innerSql * @param string $innerSql
* @param string $sql * @param string $sql
* @param OrderByClause $orderByClause * @param OrderByClause $orderByClause
* *
* @return string * @return string
*/ */
private function preserveSqlOrdering(array $sqlIdentifier, $innerSql, $sql, $orderByClause) private function preserveSqlOrdering(array $sqlIdentifier, $innerSql, $sql, $orderByClause) {
{
// If the sql statement has an order by clause, we need to wrap it in a new select distinct // If the sql statement has an order by clause, we need to wrap it in a new select distinct
// statement // statement
if (! $orderByClause instanceof OrderByClause) { if (!$orderByClause instanceof OrderByClause) {
return $sql; return $sql;
} }
// Rebuild the order by clause to work in the scope of the new select statement // Rebuild the order by clause to work in the scope of the new select statement
/* @var array $orderBy an array of rebuilt order by items */ /* @var array $orderBy an array of rebuilt order by items */
$orderBy = $this->rebuildOrderByClauseForOuterScope($orderByClause); $orderBy = $this->rebuildOrderByClauseForOuterScope($orderByClause);
$orderByFields = str_replace([' DESC', ' ASC'], ['', ''], $orderBy);
$innerSqlIdentifier = [];
foreach ($orderByFields as $k => $v) {
// remove fields that are selected by identifiers,
// if those are ordered by in the query
if (in_array($v, $sqlIdentifier)) {
unset($orderByFields[$k]);
}
}
foreach ($sqlIdentifier as $k => $v) {
$innerSqlIdentifier[$k] = 'dctrn_result_inner.' . $v;
$sqlIdentifier[$k] = 'dctrn_result.' . $v;
}
// add the
foreach ($orderByFields as $k => $v) {
$innerSqlIdentifier[$k] = 'dctrn_result_inner.' . $v;
}
// Build the select distinct statement // Build the select distinct statement
$sql = sprintf( $sql = sprintf(
'SELECT DISTINCT %s FROM (%s) dctrn_result ORDER BY %s', 'SELECT DISTINCT %s FROM (%s) dctrn_result_inner ORDER BY %s',
implode(', ', $sqlIdentifier), implode(', ', $innerSqlIdentifier),
$innerSql, $innerSql,
implode(', ', $orderBy) implode(', ', $orderBy)
); );
// now only select distinct identifier
$sql = sprintf('SELECT DISTINCT %s FROM (%s) dctrn_result', implode(', ', $sqlIdentifier), $sql);
return $sql; return $sql;
} }