Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable table name prepend in group by #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Builder/Syntax/ColumnWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 14 additions & 9 deletions src/Builder/Syntax/SelectWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
27 changes: 25 additions & 2 deletions src/Manipulation/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class Select extends AbstractBaseQuery
*/
protected $parentQuery;

/**
* @var bool
*/
protected bool $disableTablePrepend=false;

/**
* @param string $table
* @param array $columns
Expand Down Expand Up @@ -531,16 +536,34 @@ 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;
}

/**
* @param bool $disableTablePrepend
* @return $this
*/
public function setDisableTablePrepend(bool $disableTablePrepend = false)
{
$this->disableTablePrepend = $disableTablePrepend;
return $this;
}

/**
* @return bool
*/
public function getDisableTablePrepend()
{
return $this->disableTablePrepend;
}
}
22 changes: 22 additions & 0 deletions src/Syntax/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Column implements QueryPartInterface
*/
protected $alias;

/**
* @var bool
*/
protected bool $disableTablePrependInGroup = false;

/**
* @param string $name
* @param string $table
Expand Down Expand Up @@ -136,4 +141,21 @@ public function isAll()
{
return $this->getName() == self::ALL;
}

/** Disable prepend table name in group by
* @param bool $disableTablePrependInGroup
* @return void
*/
public function setDisableTablePrependInGroup(bool $disableTablePrependInGroup)
{
$this->disableTablePrependInGroup = $disableTablePrependInGroup;
}

/**
* @return bool
*/
public function getDisableTablePrependInGroup()
{
return $this->disableTablePrependInGroup;
}
}