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

Closed
wants to merge 3 commits into
from
@@ -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';
@@ -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(
@@ -90,6 +92,11 @@ class Select extends AbstractSql implements SqlInterface, PreparableSqlInterface
*/
protected $prefixColumnsWithTable = true;
+ /**
+ * @var null|bool
+ */
+ protected $distinct = null;
@marc-mabe

marc-mabe Jan 16, 2013

Member

Shouldn't it be false by default?

+
/**
* @var string|TableIdentifier
*/
@@ -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;
@marc-mabe

marc-mabe Jan 16, 2013

Member

Argument to set to true / false

+ return $this;
+ }
+
/**
* Create from clause
*
@@ -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;
@@ -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;
}
@@ -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)
Oops, something went wrong.