Skip to content

Commit b5c433e

Browse files
committed
fix QB->parseSelectPath
1 parent 587aad0 commit b5c433e

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/AbstractQueryBuilder.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ protected function parseSelectPath()
409409
{
410410
$selects = [];
411411
foreach ($this->getSqlPart($this->queryBuilder, 'select') as $part) {
412-
$selects = array_merge(array_map('trim', explode(',', $part)), $selects);
412+
$selects = array_merge($this->separateSelectString($part), $selects);
413413
}
414414
$selects = array_map(
415415
function ($str) {
@@ -422,8 +422,41 @@ function ($str) {
422422
$selects
423423
);
424424
foreach ($selects as $select) {
425-
$array = explode(' ', $select);
426-
$this->aliasMapping[isset($array[1]) ? $array[1] : $array[0]] = $array[0];
425+
$lastSpacePos = strrpos($select, ' ');
426+
$body = substr($select, 0, $lastSpacePos);
427+
$alias = substr($select, $lastSpacePos + 1);
428+
$this->aliasMapping[$alias ?: $body] = $body;
427429
}
428430
}
431+
432+
/**
433+
* @param string $string
434+
*
435+
* @return array
436+
*/
437+
private function separateSelectString(string $string)
438+
{
439+
if (false === strpos($string, '(')) {
440+
return array_map('trim', explode(',', $string));
441+
}
442+
443+
$opened = $closed = 0;
444+
$cnt = mb_strlen($string);
445+
$arrayStrings = [];
446+
$substring = '';
447+
448+
for ($i = 0; $i < $cnt; $i ++) {
449+
$substring .= $string[$i];
450+
if ('(' === $string[$i]) {
451+
$opened ++;
452+
} elseif (')' === $string[$i]) {
453+
$closed ++;
454+
} elseif (',' === $string[$i] && $opened === $closed) {
455+
$arrayStrings[] = trim($substring);
456+
}
457+
}
458+
$arrayStrings[] = trim($substring);
459+
460+
return $arrayStrings;
461+
}
429462
}

0 commit comments

Comments
 (0)