Skip to content

Commit

Permalink
fixup! Introduce Doctrine ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlSchwan committed Jul 11, 2022
1 parent 4b94c6f commit b0d3b32
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
10 changes: 5 additions & 5 deletions lib/private/DB/ORM/EntityRepositoryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public function __construct(EntityRepository $entityRepository) {
$this->entityRepository = $entityRepository;
}

public function find($id) {
public function find($id): ?object {
return $this->entityRepository->find($id);
}

public function findAll() {
public function findAll(): array {
return $this->entityRepository->findAll();
}

public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null) {
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array {
return $this->entityRepository->findBy($criteria, $orderBy, $limit, $offset);
}

public function findOneBy(array $criteria) {
public function findOneBy(array $criteria): ?object {
return $this->entityRepository->findOneBy($criteria);
}

public function getClassName() {
public function getClassName(): string {
return $this->entityRepository->getClassName();
}
}
41 changes: 38 additions & 3 deletions lib/private/DB/ORM/QueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OC\DB\ORM;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Query;
use OC\DB\QueryBuilder\Parameter;
use OCP\DB\ORM\IParameter;
Expand All @@ -26,11 +27,13 @@ public function isCacheable(): bool {
}

public function getParameter($key): ?IParameter {
return new ParameterAdapter($this->query->getParameter($key));
$internal = $this->query->getParameter($key);
return $internal === null ? new ParameterAdapter($internal) : null;
}

public function setParameters($parameters): IQuery {
$this->query->setParameters($parameters);
return $this;
}

public function setParameter($key, $value, $type = null): IQuery {
Expand All @@ -47,15 +50,47 @@ public function getResult() {
return $this->query->getResult();
}

public function getArrayResult() {
return $this->query->getArrayResult();
}

public function getOneOrNullResult() {
return $this->query->getOneOrNullResult();
}

public function getSingleResult() {
return $this->query->getSingleResult();
try {
return $this->query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
throw new NoResultException($e);
} catch (\Doctrine\ORM\NonUniqueResultException $e) {
throw new NonUniqueResultException($e);
}
}

public function getSingleScalarResult() {
return $this->query->getSingleScalarResult();
try {
return $this->query->getSingleScalarResult();
} catch (\Doctrine\ORM\NoResultException $e) {
throw new NoResultException($e);
} catch (\Doctrine\ORM\NonUniqueResultException $e) {
throw new NonUniqueResultException($e);
}
}

public function getSql(): string {
return $this->query->getSQL();
}

/**
* Get all defined parameters.
*
* @return ArrayCollection The defined query parameters.
* @psalm-return ArrayCollection<int, Query\Parameter>
*/
public function getParameters(): ArrayCollection
{
return $this->query->getParameters()
->map(fn (Query\Parameter $parameter) => new Parameter($parameter));
}
}
16 changes: 7 additions & 9 deletions lib/public/DB/ORM/IEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*
* @template-covariant T of object
*/
interface IEntityRepository
{
interface IEntityRepository {
/**
* Finds an object by its primary key / identifier.
*
Expand All @@ -17,15 +16,15 @@ interface IEntityRepository
* @return object|null The object.
* @psalm-return T|null
*/
public function find($id);
public function find($id): ?object;

/**
* Finds all objects in the repository.
*
* @return array<int, object> The objects.
* @psalm-return T[]
*/
public function findAll();
public function findAll(): array;

/**
* Finds objects by a set of criteria.
Expand All @@ -48,22 +47,21 @@ public function findBy(
?array $orderBy = null,
?int $limit = null,
?int $offset = null
);
): array;

/**
* Finds a single object by a set of criteria.
*
* @param array<string, mixed> $criteria The criteria.
*
* @return object|null The object.
* @psalm-return T|null
* @return T|null
*/
public function findOneBy(array $criteria);
public function findOneBy(array $criteria): ?object;

/**
* Returns the class name of the object managed by the repository.
*
* @psalm-return class-string<T>
*/
public function getClassName();
public function getClassName(): string;
}
4 changes: 2 additions & 2 deletions lib/public/DB/ORM/IParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface IParameter {
/**
* Retrieves the Parameter name.
*/
public function getName(): string
public function getName(): string;

/**
* Retrieves the Parameter value.
Expand All @@ -31,7 +31,7 @@ public function getType();
*
* @return void
*/
public function setValue($value, $type = null): void
public function setValue($value, $type = null): void;

public function typeWasSpecified(): bool;
}
11 changes: 11 additions & 0 deletions lib/public/DB/ORM/IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OCP\DB\ORM;

use Doctrine\Common\Collections\ArrayCollection;

interface IQuery {
/**
* Enable/disable second level query (result) caching for this query.
Expand Down Expand Up @@ -79,4 +81,13 @@ public function getSingleResult();

public function getSingleScalarResult();

public function getSql(): string;

/**
* Get all defined parameters.
*
* @return ArrayCollection The defined query parameters.
* @psalm-return ArrayCollection<int, \Doctrine\ORM\Query\Parameter>
*/
public function getParameters(): ArrayCollection;
}
1 change: 0 additions & 1 deletion lib/public/IContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
* IContainer is the basic interface to be used for any internal dependency injection mechanism
*
* @since 6.0.0
* @deprecated 20.0.0 use \Psr\Container\ContainerInterface
*/
interface IContainer extends ContainerInterface {

Expand Down

0 comments on commit b0d3b32

Please sign in to comment.