Merge pull request #63212 from nextcloud/carl/orderby-sortdirection

feat(db): Add support for SortDirection to addSortBy and sortBy
pull/55501/merge
Daniel 1 week ago committed by GitHub
commit afb421ea33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      lib/private/DB/QueryBuilder/ExtendedQueryBuilder.php
  2. 37
      lib/private/DB/QueryBuilder/QueryBuilder.php
  3. 33
      lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php
  4. 2
      lib/public/AppFramework/ORM/Repository.php
  5. 8
      lib/public/DB/QueryBuilder/IQueryBuilder.php
  6. 6
      lib/public/DB/QueryBuilder/ITypedQueryBuilder.php

@ -10,7 +10,10 @@ namespace OC\DB\QueryBuilder;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\ConflictResolutionMode;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IParameter;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection;
/**
@ -251,13 +254,13 @@ abstract class ExtendedQueryBuilder extends TypedQueryBuilder {
}
#[\Override]
public function orderBy($sort, $order = null) {
public function orderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self {
$this->builder->orderBy($sort, $order);
return $this;
}
#[\Override]
public function addOrderBy($sort, $order = null) {
public function addOrderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self {
$this->builder->addOrderBy($sort, $order);
return $this;
}

@ -1115,19 +1115,14 @@ class QueryBuilder extends TypedQueryBuilder {
return $this;
}
/**
* Specifies an ordering for the query results.
* Replaces any previously specified orderings, if any.
*
* @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression.
* @param string $order The ordering direction.
*
* @return $this This QueryBuilder instance.
*/
#[\Override]
public function orderBy($sort, $order = null) {
if ($order !== null && !in_array(strtoupper((string)$order), ['ASC', 'DESC'], true)) {
$order = null;
public function orderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self {
if ($order === \SortDirection::Ascending) {
$order = 'ASC';
} elseif ($order === \SortDirection::Descending) {
$order = 'DESC';
} elseif ($order !== null && !in_array(strtoupper($order), ['ASC', 'DESC'], true)) {
throw new \InvalidArgumentException('Only ASC or DESC are supported');
}
$this->queryBuilder->orderBy(
@ -1138,18 +1133,14 @@ class QueryBuilder extends TypedQueryBuilder {
return $this;
}
/**
* Adds an ordering to the query results.
*
* @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
* @param string $order The ordering direction.
*
* @return $this This QueryBuilder instance.
*/
#[\Override]
public function addOrderBy($sort, $order = null) {
if ($order !== null && !in_array(strtoupper((string)$order), ['ASC', 'DESC'], true)) {
$order = null;
public function addOrderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self {
if ($order === \SortDirection::Ascending) {
$order = 'ASC';
} elseif ($order === \SortDirection::Descending) {
$order = 'DESC';
} elseif ($order !== null && !in_array(strtoupper($order), ['ASC', 'DESC'], true)) {
throw new \InvalidArgumentException('Only ASC or DESC are supported');
}
$this->queryBuilder->addOrderBy(

@ -12,7 +12,10 @@ use OC\DB\QueryBuilder\CompositeExpression;
use OC\DB\QueryBuilder\ExtendedQueryBuilder;
use OC\DB\QueryBuilder\Parameter;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\ILiteral;
use OCP\DB\QueryBuilder\IParameter;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\IDBConnection;
/**
@ -295,24 +298,34 @@ class ShardedQueryBuilder extends ExtendedQueryBuilder {
}
#[\Override]
public function addOrderBy($sort, $order = null) {
if ($order !== null && !in_array(strtoupper((string)$order), ['ASC', 'DESC'], true)) {
$order = null;
public function addOrderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self {
if ($order === \SortDirection::Ascending) {
$order = 'ASC';
} elseif ($order === \SortDirection::Descending) {
$order = 'DESC';
} elseif ($order !== null && !in_array(strtoupper($order), ['ASC', 'DESC'], true)) {
throw new \InvalidArgumentException('Only ASC or DESC are supported');
}
$this->registerOrder((string)$sort, (string)($order ?? 'ASC'));
return parent::addOrderBy($sort, $order);
$this->registerOrder((string)$sort, $order ?? 'ASC');
parent::addOrderBy($sort, $order);
return $this;
}
#[\Override]
public function orderBy($sort, $order = null) {
if ($order !== null && !in_array(strtoupper((string)$order), ['ASC', 'DESC'], true)) {
$order = null;
public function orderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self {
if ($order === \SortDirection::Ascending) {
$order = 'ASC';
} elseif ($order === \SortDirection::Descending) {
$order = 'DESC';
} elseif ($order !== null && !in_array(strtoupper($order), ['ASC', 'DESC'], true)) {
throw new \InvalidArgumentException('Only ASC or DESC are supported');
}
$this->sortList = [];
$this->registerOrder((string)$sort, (string)($order ?? 'ASC'));
return parent::orderBy($sort, $order);
$this->registerOrder((string)$sort, $order ?? 'ASC');
parent::orderBy($sort, $order);
return $this;
}
private function registerOrder(string $column, string $order): void {

@ -478,7 +478,7 @@ class Repository {
foreach ($orderBy as $field => $direction) {
$column = $entityInfo->mappingPropertyToColumn[$field];
$qb->addOrderBy('e.' . $column, $direction === \SortDirection::Ascending ? 'ASC' : 'DESC');
$qb->addOrderBy('e.' . $column, $direction);
}
return [$qb, $relations];

@ -857,7 +857,7 @@ interface IQueryBuilder {
* Replaces any previously specified orderings, if any.
*
* @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression.
* @param string $order The ordering direction.
* @param 'ASC'|'DESC'|'asc'|'desc'|\SortDirection|null $order The ordering direction.
*
* @return $this This QueryBuilder instance.
* @since 8.2.0
@ -865,13 +865,13 @@ interface IQueryBuilder {
* @psalm-taint-sink sql $sort
* @psalm-taint-sink sql $order
*/
public function orderBy($sort, $order = null);
public function orderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self;
/**
* Adds an ordering to the query results.
*
* @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression.
* @param string $order The ordering direction.
* @param 'ASC'|'DESC'|'asc'|'desc'|\SortDirection|null $order The ordering direction.
*
* @return $this This QueryBuilder instance.
* @since 8.2.0
@ -879,7 +879,7 @@ interface IQueryBuilder {
* @psalm-taint-sink sql $sort
* @psalm-taint-sink sql $order
*/
public function addOrderBy($sort, $order = null);
public function addOrderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self;
/**
* Gets a query part by its name.

@ -293,20 +293,18 @@ interface ITypedQueryBuilder extends IQueryBuilder {
/**
* @inheritDoc
* @return $this
* @psalm-suppress MissingParamType
* @since 34.0.0
*/
#[Override]
public function orderBy($sort, $order = null);
public function orderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self;
/**
* @inheritDoc
* @return $this
* @psalm-suppress MissingParamType
* @since 34.0.0
*/
#[Override]
public function addOrderBy($sort, $order = null);
public function addOrderBy(string|ILiteral|IParameter|IQueryFunction $sort, string|\SortDirection|null $order = null): self;
/**
* @inheritDoc

Loading…
Cancel
Save