Skip to content
Merged
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
7 changes: 5 additions & 2 deletions lib/private/DB/QueryBuilder/ExtendedQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

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;

/**
Expand Down Expand Up @@ -251,13 +254,13 @@ public function orHaving(...$having) {
}

#[\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;
}
Expand Down
37 changes: 14 additions & 23 deletions lib/private/DB/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1115,19 +1115,14 @@ public function orHaving(...$having) {
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(
Expand All @@ -1138,18 +1133,14 @@ public function orderBy($sort, $order = null) {
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(
Expand Down
33 changes: 23 additions & 10 deletions lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
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;

/**
Expand Down Expand Up @@ -295,24 +298,34 @@ public function setFirstResult($firstResult) {
}

#[\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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/public/AppFramework/ORM/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ private function getJoinedSelectQueryBuilder(array $criteria, array $orderBy = [

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];
Expand Down
8 changes: 4 additions & 4 deletions lib/public/DB/QueryBuilder/IQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,29 +857,29 @@ public function orHaving(...$having);
* 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
*
* @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
*
* @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.
Expand Down
6 changes: 2 additions & 4 deletions lib/public/DB/QueryBuilder/ITypedQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,18 @@ public function orHaving(...$having);
/**
* @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
Expand Down
Loading