From 61e51b4bd021dc2fa174cf6257250ddb60a72585 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 16 Jan 2025 18:05:54 +0300 Subject: [PATCH 1/2] add block append prepend table name in group by block --- src/Builder/Syntax/ColumnWriter.php | 14 +++++++++----- src/Builder/Syntax/SelectWriter.php | 23 ++++++++++++++--------- src/Manipulation/Select.php | 15 +++++++++++++-- src/Syntax/Column.php | 16 ++++++++++++++++ 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/Builder/Syntax/ColumnWriter.php b/src/Builder/Syntax/ColumnWriter.php index 999db08..8bd4ddc 100644 --- a/src/Builder/Syntax/ColumnWriter.php +++ b/src/Builder/Syntax/ColumnWriter.php @@ -151,11 +151,15 @@ public function writeColumnWithAlias(Column $column) */ public function writeColumn(Column $column) { - $alias = $column->getTable()->getAlias(); - $table = ($alias) ? $this->writer->writeTableAlias($alias) : $this->writer->writeTable($column->getTable()); - - $columnString = (empty($table)) ? '' : "{$table}."; - $columnString .= $this->writer->writeColumnName($column); + if ( $column->getDisableTablePrependInGroup() === false ) { + $alias = $column->getTable()->getAlias(); + $table = ($alias) ? $this->writer->writeTableAlias($alias) : $this->writer->writeTable($column->getTable()); + + $columnString = (empty($table)) ? '' : "{$table}."; + $columnString .= $this->writer->writeColumnName($column); + } else { + $columnString = $this->writer->writeColumnName($column); + } return $columnString; } diff --git a/src/Builder/Syntax/SelectWriter.php b/src/Builder/Syntax/SelectWriter.php index b124bfe..37fe362 100644 --- a/src/Builder/Syntax/SelectWriter.php +++ b/src/Builder/Syntax/SelectWriter.php @@ -238,20 +238,25 @@ function (&$where) use (&$whereWriter) { /** * @param Select $select - * @param array $parts + * @param array $parts * * @return $this */ public function writeSelectGroupBy(Select $select, array &$parts) { - $groupBy = $this->writeSelectAggrupation( - $select, - $this->columnWriter, - 'getGroupBy', - 'writeColumn', - ', ', - 'GROUP BY ' - ); + $groupBy = ''; + $columns = $select->getGroupBy(); + $columnWriter = $this->columnWriter; + if ( !empty($columns) ) { + \array_walk( + $columns, + function (&$column) use ($select, $columnWriter) { + $column->setDisableTablePrependInGroup($select->getDisableTablePrepend()); + $column = $columnWriter->writeColumn($column); + } + ); + $groupBy = 'GROUP BY' . implode(',', $columns); + } $parts = \array_merge($parts, [$groupBy]); diff --git a/src/Manipulation/Select.php b/src/Manipulation/Select.php index 1580df4..13b4431 100644 --- a/src/Manipulation/Select.php +++ b/src/Manipulation/Select.php @@ -531,16 +531,27 @@ public function setParentQuery(Select $parentQuery) /** * @param string $column * @param string $direction - * @param null $table + * @param null $table * * @return $this */ public function orderBy($column, $direction = OrderBy::ASC, $table = null) { $current = parent::orderBy($column, $direction, $table); - if ($this->getParentQuery() != null) { + if ( $this->getParentQuery() != null ) { $this->getParentQuery()->orderBy($column, $direction, \is_null($table) ? $this->getTable() : $table); } return $current; } + + public function setDisableTablePrepend($disableTablePrepend=false) + { + $this->disableTablePrepend = $disableTablePrepend; + return $this; + } + + public function getDisableTablePrepend() + { + return $this->disableTablePrepend; + } } diff --git a/src/Syntax/Column.php b/src/Syntax/Column.php index aa244f9..183bc70 100644 --- a/src/Syntax/Column.php +++ b/src/Syntax/Column.php @@ -34,6 +34,8 @@ class Column implements QueryPartInterface */ protected $alias; + protected $disableTablePrependInGroup = false; + /** * @param string $name * @param string $table @@ -136,4 +138,18 @@ public function isAll() { return $this->getName() == self::ALL; } + + /** Disable prepend table name in group by + * @param $disableTablePrependInGroup + * @return void + */ + public function setDisableTablePrependInGroup($disableTablePrependInGroup) + { + $this->disableTablePrependInGroup = (bool)$disableTablePrependInGroup; + } + + public function getDisableTablePrependInGroup() + { + return $this->disableTablePrependInGroup; + } } From 815ded53cf249ba7805f3ae048dc61d8a265368f Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 16 Jan 2025 18:20:08 +0300 Subject: [PATCH 2/2] add phpDoc fix type --- src/Manipulation/Select.php | 14 +++++++++++++- src/Syntax/Column.php | 14 ++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Manipulation/Select.php b/src/Manipulation/Select.php index 13b4431..8be81d3 100644 --- a/src/Manipulation/Select.php +++ b/src/Manipulation/Select.php @@ -70,6 +70,11 @@ class Select extends AbstractBaseQuery */ protected $parentQuery; + /** + * @var bool + */ + protected bool $disableTablePrepend=false; + /** * @param string $table * @param array $columns @@ -544,12 +549,19 @@ public function orderBy($column, $direction = OrderBy::ASC, $table = null) return $current; } - public function setDisableTablePrepend($disableTablePrepend=false) + /** + * @param bool $disableTablePrepend + * @return $this + */ + public function setDisableTablePrepend(bool $disableTablePrepend = false) { $this->disableTablePrepend = $disableTablePrepend; return $this; } + /** + * @return bool + */ public function getDisableTablePrepend() { return $this->disableTablePrepend; diff --git a/src/Syntax/Column.php b/src/Syntax/Column.php index 183bc70..96cf814 100644 --- a/src/Syntax/Column.php +++ b/src/Syntax/Column.php @@ -34,7 +34,10 @@ class Column implements QueryPartInterface */ protected $alias; - protected $disableTablePrependInGroup = false; + /** + * @var bool + */ + protected bool $disableTablePrependInGroup = false; /** * @param string $name @@ -140,14 +143,17 @@ public function isAll() } /** Disable prepend table name in group by - * @param $disableTablePrependInGroup + * @param bool $disableTablePrependInGroup * @return void */ - public function setDisableTablePrependInGroup($disableTablePrependInGroup) + public function setDisableTablePrependInGroup(bool $disableTablePrependInGroup) { - $this->disableTablePrependInGroup = (bool)$disableTablePrependInGroup; + $this->disableTablePrependInGroup = $disableTablePrependInGroup; } + /** + * @return bool + */ public function getDisableTablePrependInGroup() { return $this->disableTablePrependInGroup;