Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Added DISTINCT on Zend\Db\Sql\Select #3455

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 36 additions & 12 deletions library/Zend/Db/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Select extends AbstractSql implements SqlInterface, PreparableSqlInterface
* @const
*/
const SELECT = 'select';
const DISTINCT = 'distinct';
const COLUMNS = 'columns';
const TABLE = 'table';
const JOINS = 'joins';
Expand All @@ -54,9 +55,10 @@ class Select extends AbstractSql implements SqlInterface, PreparableSqlInterface
*/
protected $specifications = array(
self::SELECT => array(
'SELECT %1$s FROM %2$s' => array(
'SELECT %3$s%1$s FROM %2$s' => array(
array(1 => '%1$s', 2 => '%1$s AS %2$s', 'combinedby' => ', '),
null
null,
array(1 => '%1$s')
)
),
self::JOINS => array(
Expand Down Expand Up @@ -90,6 +92,11 @@ class Select extends AbstractSql implements SqlInterface, PreparableSqlInterface
*/
protected $prefixColumnsWithTable = true;

/**
* @var null|bool
*/
protected $distinct = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be false by default?


/**
* @var string|TableIdentifier
*/
Expand Down Expand Up @@ -151,6 +158,17 @@ public function __construct($table = null)
$this->having = new Having;
}

/**
* Add a distinct operator to the select clause
*
* @return Select
*/
public function distinct()
{
$this->distinct = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument to set to true / false

return $this;
}

/**
* Create from clause
*
Expand Down Expand Up @@ -389,6 +407,9 @@ public function reset($part)
}
$this->table = null;
break;
case self::DISTINCT:
$this->distinct = null;
break;
case self::COLUMNS:
$this->columns = array();
break;
Expand Down Expand Up @@ -429,15 +450,16 @@ public function setSpecification($index, $specification)
public function getRawState($key = null)
{
$rawState = array(
self::TABLE => $this->table,
self::COLUMNS => $this->columns,
self::JOINS => $this->joins,
self::WHERE => $this->where,
self::ORDER => $this->order,
self::GROUP => $this->group,
self::HAVING => $this->having,
self::LIMIT => $this->limit,
self::OFFSET => $this->offset
self::TABLE => $this->table,
self::DISTINCT => $this->distinct,
self::COLUMNS => $this->columns,
self::JOINS => $this->joins,
self::WHERE => $this->where,
self::ORDER => $this->order,
self::GROUP => $this->group,
self::HAVING => $this->having,
self::LIMIT => $this->limit,
self::OFFSET => $this->offset
);
return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState;
}
Expand Down Expand Up @@ -604,7 +626,9 @@ protected function processSelect(PlatformInterface $platform, Adapter $adapter =
}
}

return array($columns, $table);
$distinct = ($this->distinct) ? 'DISTINCT ' : '';

return array($columns, $table, $distinct);
}

protected function processJoins(PlatformInterface $platform, Adapter $adapter = null, ParameterContainer $parameterContainer = null)
Expand Down
Loading