Add select distinct to the query builder
This commit is contained in:
parent
f2c7acb3c0
commit
a3391248e4
|
@ -324,6 +324,28 @@ class QueryBuilder implements IQueryBuilder {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies an item that is to be returned uniquely in the query result.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* $qb = $conn->getQueryBuilder()
|
||||||
|
* ->selectDistinct('type')
|
||||||
|
* ->from('users');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param mixed $select The selection expressions.
|
||||||
|
*
|
||||||
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
|
||||||
|
*/
|
||||||
|
public function selectDistinct($select) {
|
||||||
|
|
||||||
|
$this->queryBuilder->addSelect(
|
||||||
|
'DISTINCT ' . $this->helper->quoteColumnName($select)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an item that is to be returned in the query result.
|
* Adds an item that is to be returned in the query result.
|
||||||
*
|
*
|
||||||
|
|
|
@ -256,6 +256,22 @@ interface IQueryBuilder {
|
||||||
*/
|
*/
|
||||||
public function selectAlias($select, $alias);
|
public function selectAlias($select, $alias);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specifies an item that is to be returned uniquely in the query result.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* $qb = $conn->getQueryBuilder()
|
||||||
|
* ->selectDistinct('type')
|
||||||
|
* ->from('users');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param mixed $select The selection expressions.
|
||||||
|
*
|
||||||
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
|
||||||
|
* @since 9.0.0
|
||||||
|
*/
|
||||||
|
public function selectDistinct($select);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an item that is to be returned in the query result.
|
* Adds an item that is to be returned in the query result.
|
||||||
*
|
*
|
||||||
|
|
|
@ -48,12 +48,12 @@ class QueryBuilderTest extends \Test\TestCase {
|
||||||
$this->queryBuilder = new QueryBuilder($this->connection);
|
$this->queryBuilder = new QueryBuilder($this->connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function createTestingRows() {
|
protected function createTestingRows($appId = 'testFirstResult') {
|
||||||
$qB = $this->connection->getQueryBuilder();
|
$qB = $this->connection->getQueryBuilder();
|
||||||
for ($i = 1; $i < 10; $i++) {
|
for ($i = 1; $i < 10; $i++) {
|
||||||
$qB->insert('*PREFIX*appconfig')
|
$qB->insert('*PREFIX*appconfig')
|
||||||
->values([
|
->values([
|
||||||
'appid' => $qB->expr()->literal('testFirstResult'),
|
'appid' => $qB->expr()->literal($appId),
|
||||||
'configkey' => $qB->expr()->literal('testing' . $i),
|
'configkey' => $qB->expr()->literal('testing' . $i),
|
||||||
'configvalue' => $qB->expr()->literal(100 - $i),
|
'configvalue' => $qB->expr()->literal(100 - $i),
|
||||||
])
|
])
|
||||||
|
@ -80,11 +80,11 @@ class QueryBuilderTest extends \Test\TestCase {
|
||||||
return $rows;
|
return $rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function deleteTestingRows() {
|
protected function deleteTestingRows($appId = 'testFirstResult') {
|
||||||
$qB = $this->connection->getQueryBuilder();
|
$qB = $this->connection->getQueryBuilder();
|
||||||
|
|
||||||
$qB->delete('*PREFIX*appconfig')
|
$qB->delete('*PREFIX*appconfig')
|
||||||
->where($qB->expr()->eq('appid', $qB->expr()->literal('testFirstResult')))
|
->where($qB->expr()->eq('appid', $qB->expr()->literal($appId)))
|
||||||
->execute();
|
->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,6 +272,34 @@ class QueryBuilderTest extends \Test\TestCase {
|
||||||
$this->deleteTestingRows();
|
$this->deleteTestingRows();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSelectDistinct() {
|
||||||
|
$this->deleteTestingRows('testFirstResult1');
|
||||||
|
$this->deleteTestingRows('testFirstResult2');
|
||||||
|
$this->createTestingRows('testFirstResult1');
|
||||||
|
$this->createTestingRows('testFirstResult2');
|
||||||
|
|
||||||
|
$this->queryBuilder->selectDistinct('appid');
|
||||||
|
|
||||||
|
$this->queryBuilder->from('*PREFIX*appconfig')
|
||||||
|
->where($this->queryBuilder->expr()->in(
|
||||||
|
'appid',
|
||||||
|
[$this->queryBuilder->expr()->literal('testFirstResult1'), $this->queryBuilder->expr()->literal('testFirstResult2')]
|
||||||
|
))
|
||||||
|
->orderBy('appid', 'DESC');
|
||||||
|
|
||||||
|
$query = $this->queryBuilder->execute();
|
||||||
|
$rows = $query->fetchAll();
|
||||||
|
$query->closeCursor();
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
[['appid' => 'testFirstResult2'], ['appid' => 'testFirstResult1']],
|
||||||
|
$rows
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->deleteTestingRows('testFirstResult1');
|
||||||
|
$this->deleteTestingRows('testFirstResult2');
|
||||||
|
}
|
||||||
|
|
||||||
public function dataAddSelect() {
|
public function dataAddSelect() {
|
||||||
$queryBuilder = new QueryBuilder(\OC::$server->getDatabaseConnection());
|
$queryBuilder = new QueryBuilder(\OC::$server->getDatabaseConnection());
|
||||||
return [
|
return [
|
||||||
|
|
Loading…
Reference in New Issue