diff --git a/lib/private/DB/QueryBuilder/ExtendedQueryBuilder.php b/lib/private/DB/QueryBuilder/ExtendedQueryBuilder.php index 5cc0771c0bed0..5b63e1792bacb 100644 --- a/lib/private/DB/QueryBuilder/ExtendedQueryBuilder.php +++ b/lib/private/DB/QueryBuilder/ExtendedQueryBuilder.php @@ -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; /** @@ -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; } diff --git a/lib/private/DB/QueryBuilder/QueryBuilder.php b/lib/private/DB/QueryBuilder/QueryBuilder.php index f5252fcfaccc7..11264d28361e3 100644 --- a/lib/private/DB/QueryBuilder/QueryBuilder.php +++ b/lib/private/DB/QueryBuilder/QueryBuilder.php @@ -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( @@ -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( diff --git a/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php b/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php index 0c2bf2fa1db14..9bdf08acd56cc 100644 --- a/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php +++ b/lib/private/DB/QueryBuilder/Sharded/ShardedQueryBuilder.php @@ -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; /** @@ -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 { diff --git a/lib/public/AppFramework/ORM/Repository.php b/lib/public/AppFramework/ORM/Repository.php index 3a007a9194c83..098ea261335d4 100644 --- a/lib/public/AppFramework/ORM/Repository.php +++ b/lib/public/AppFramework/ORM/Repository.php @@ -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]; diff --git a/lib/public/DB/QueryBuilder/IQueryBuilder.php b/lib/public/DB/QueryBuilder/IQueryBuilder.php index 89d6b61e0abdf..8cb89d8f976d4 100644 --- a/lib/public/DB/QueryBuilder/IQueryBuilder.php +++ b/lib/public/DB/QueryBuilder/IQueryBuilder.php @@ -857,7 +857,7 @@ 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 @@ -865,13 +865,13 @@ public function orHaving(...$having); * @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 @@ public function orderBy($sort, $order = null); * @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. diff --git a/lib/public/DB/QueryBuilder/ITypedQueryBuilder.php b/lib/public/DB/QueryBuilder/ITypedQueryBuilder.php index f096b5cff0f41..823cb862786e8 100644 --- a/lib/public/DB/QueryBuilder/ITypedQueryBuilder.php +++ b/lib/public/DB/QueryBuilder/ITypedQueryBuilder.php @@ -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