From e7d5d5965b40995c73c2451fc3d44077bf8ae398 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 1 Mar 2021 16:10:15 +0000 Subject: [PATCH] - Fixed the missing DISTINCT keyword while rendering select() - Added a fragment remover in Repository --- src/Query/Select.php | 3 ++- src/Repository.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Query/Select.php b/src/Query/Select.php index aa2f3d8..4f7c01f 100644 --- a/src/Query/Select.php +++ b/src/Query/Select.php @@ -17,7 +17,7 @@ class Select extends Fragment { protected array $fields = []; const SQL_TOKEN = "SELECT"; - + public function set($fields) : self { $this->fields = is_array($fields) ? $fields : [ $fields ]; @@ -48,6 +48,7 @@ class Select extends Fragment { return $this->renderSegments([ ( $this->union ? 'UNION' : false ), static::SQL_TOKEN, + ( $this->distinct ? 'DISTINCT' : false ), ( $this->top ? sprintf('TOP (%s)', $this->top) : false ), implode(', ', $this->fields) ]); diff --git a/src/Repository.php b/src/Repository.php index db8e757..ec217c2 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -271,6 +271,13 @@ class Repository } } + public function removeQueryFragment(Query\Fragment $fragment) : self + { + $this->queryBuilder->removeFragment($fragment); + + return $this; + } + public function select(/*array|Stringable*/ $fields) : self { $this->queryBuilder->select($fields); @@ -278,6 +285,14 @@ class Repository return $this; } + public function distinct(/*array|Stringable*/ $fields) : self + { + $this->queryBuilder->select($fields); + $this->queryBuilder->getFragment(Query\Select::class)->distinct = true; + + return $this; + } + public function insert(array $fieldlist, string $table, string $alias, ? string $schema) : self { $this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $alias, $this->escapedDatabase(), $schema);