2018-05-10 15:35:26 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-05-10 15:35:26 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-05-10 15:35:26 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Marius David Wieschollek <git.public@mdns.eu>
|
2018-05-10 15:35:26 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-10 15:35:26 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCP\AppFramework\Db;
|
|
|
|
|
2018-09-06 09:46:18 +03:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2018-05-10 15:35:26 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple parent class for inheriting your data access layer from. This class
|
|
|
|
* may be subject to change in the future
|
|
|
|
*
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
abstract class QBMapper {
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $tableName;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $entityClass;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $db;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDBConnection $db Instance of the Db abstraction layer
|
|
|
|
* @param string $tableName the name of the table. set this to allow entity
|
|
|
|
* @param string $entityClass the name of the entity that the sql should be
|
|
|
|
* mapped to queries without using sql
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function __construct(IDBConnection $db, string $tableName, string $entityClass=null) {
|
2018-05-10 15:35:26 +03:00
|
|
|
$this->db = $db;
|
|
|
|
$this->tableName = $tableName;
|
|
|
|
|
|
|
|
// if not given set the entity name to the class without the mapper part
|
|
|
|
// cache it here for later use since reflection is slow
|
|
|
|
if($entityClass === null) {
|
|
|
|
$this->entityClass = str_replace('Mapper', '', \get_class($this));
|
|
|
|
} else {
|
|
|
|
$this->entityClass = $entityClass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string the table name
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function getTableName(): string {
|
|
|
|
return $this->tableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an entity from the table
|
|
|
|
* @param Entity $entity the entity that should be deleted
|
|
|
|
* @return Entity the deleted entity
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function delete(Entity $entity): Entity {
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
2020-02-26 16:44:45 +03:00
|
|
|
$idType = $this->getParameterTypeForProperty($entity, 'id');
|
|
|
|
|
2018-05-10 15:35:26 +03:00
|
|
|
$qb->delete($this->tableName)
|
|
|
|
->where(
|
2020-02-26 16:44:45 +03:00
|
|
|
$qb->expr()->eq('id', $qb->createNamedParameter($entity->getId(), $idType))
|
2018-05-10 15:35:26 +03:00
|
|
|
);
|
|
|
|
$qb->execute();
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new entry in the db from an entity
|
|
|
|
* @param Entity $entity the entity that should be created
|
|
|
|
* @return Entity the saved entity with the set id
|
|
|
|
* @since 14.0.0
|
2018-05-14 15:46:33 +03:00
|
|
|
* @suppress SqlInjectionChecker
|
2018-05-10 15:35:26 +03:00
|
|
|
*/
|
|
|
|
public function insert(Entity $entity): Entity {
|
|
|
|
// get updated fields to save, fields have to be set using a setter to
|
|
|
|
// be saved
|
|
|
|
$properties = $entity->getUpdatedFields();
|
|
|
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->insert($this->tableName);
|
|
|
|
|
|
|
|
// build the fields
|
|
|
|
foreach($properties as $property => $updated) {
|
|
|
|
$column = $entity->propertyToColumn($property);
|
|
|
|
$getter = 'get' . ucfirst($property);
|
|
|
|
$value = $entity->$getter();
|
|
|
|
|
2019-03-25 00:35:23 +03:00
|
|
|
$type = $this->getParameterTypeForProperty($entity, $property);
|
|
|
|
$qb->setValue($column, $qb->createNamedParameter($value, $type));
|
2018-05-10 15:35:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
|
2018-12-15 16:22:54 +03:00
|
|
|
if($entity->id === null) {
|
2020-02-26 16:44:45 +03:00
|
|
|
// When autoincrement is used id is always an int
|
2018-12-15 16:05:11 +03:00
|
|
|
$entity->setId((int)$qb->getLastInsertId());
|
|
|
|
}
|
2018-05-10 15:35:26 +03:00
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
2018-09-06 09:46:18 +03:00
|
|
|
/**
|
|
|
|
* Tries to creates a new entry in the db from an entity and
|
|
|
|
* updates an existing entry if duplicate keys are detected
|
|
|
|
* by the database
|
|
|
|
*
|
|
|
|
* @param Entity $entity the entity that should be created/updated
|
|
|
|
* @return Entity the saved entity with the (new) id
|
2018-09-14 16:50:55 +03:00
|
|
|
* @throws \InvalidArgumentException if entity has no id
|
2018-09-06 09:46:18 +03:00
|
|
|
* @since 15.0.0
|
|
|
|
* @suppress SqlInjectionChecker
|
|
|
|
*/
|
|
|
|
public function insertOrUpdate(Entity $entity): Entity {
|
|
|
|
try {
|
|
|
|
return $this->insert($entity);
|
|
|
|
} catch (UniqueConstraintViolationException $ex) {
|
|
|
|
return $this->update($entity);
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 15:35:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an entry in the db from an entity
|
|
|
|
* @throws \InvalidArgumentException if entity has no id
|
|
|
|
* @param Entity $entity the entity that should be created
|
|
|
|
* @return Entity the saved entity with the set id
|
|
|
|
* @since 14.0.0
|
2018-05-14 15:46:33 +03:00
|
|
|
* @suppress SqlInjectionChecker
|
2018-05-10 15:35:26 +03:00
|
|
|
*/
|
|
|
|
public function update(Entity $entity): Entity {
|
|
|
|
// if entity wasn't changed it makes no sense to run a db query
|
|
|
|
$properties = $entity->getUpdatedFields();
|
|
|
|
if(\count($properties) === 0) {
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// entity needs an id
|
|
|
|
$id = $entity->getId();
|
|
|
|
if($id === null){
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Entity which should be updated has no id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// get updated fields to save, fields have to be set using a setter to
|
|
|
|
// be saved
|
|
|
|
// do not update the id field
|
|
|
|
unset($properties['id']);
|
|
|
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->update($this->tableName);
|
|
|
|
|
|
|
|
// build the fields
|
|
|
|
foreach($properties as $property => $updated) {
|
|
|
|
$column = $entity->propertyToColumn($property);
|
|
|
|
$getter = 'get' . ucfirst($property);
|
|
|
|
$value = $entity->$getter();
|
|
|
|
|
2019-03-25 00:35:23 +03:00
|
|
|
$type = $this->getParameterTypeForProperty($entity, $property);
|
|
|
|
$qb->set($column, $qb->createNamedParameter($value, $type));
|
2018-05-10 15:35:26 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:44:45 +03:00
|
|
|
$idType = $this->getParameterTypeForProperty($entity, 'id');
|
|
|
|
|
2018-05-10 15:35:26 +03:00
|
|
|
$qb->where(
|
2020-02-26 16:44:45 +03:00
|
|
|
$qb->expr()->eq('id', $qb->createNamedParameter($id, $idType))
|
2018-05-10 15:35:26 +03:00
|
|
|
);
|
|
|
|
$qb->execute();
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
2019-03-25 00:35:23 +03:00
|
|
|
/**
|
|
|
|
* Returns the type parameter for the QueryBuilder for a specific property
|
|
|
|
* of the $entity
|
|
|
|
*
|
|
|
|
* @param Entity $entity The entity to get the types from
|
|
|
|
* @param string $property The property of $entity to get the type for
|
|
|
|
* @return int
|
|
|
|
* @since 16.0.0
|
|
|
|
*/
|
|
|
|
protected function getParameterTypeForProperty(Entity $entity, string $property): int {
|
|
|
|
$types = $entity->getFieldTypes();
|
|
|
|
|
|
|
|
if(!isset($types[ $property ])) {
|
|
|
|
return IQueryBuilder::PARAM_STR;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($types[ $property ]) {
|
|
|
|
case 'int':
|
|
|
|
case 'integer':
|
|
|
|
return IQueryBuilder::PARAM_INT;
|
|
|
|
case 'string':
|
|
|
|
return IQueryBuilder::PARAM_STR;
|
|
|
|
case 'bool':
|
|
|
|
case 'boolean':
|
|
|
|
return IQueryBuilder::PARAM_BOOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IQueryBuilder::PARAM_STR;
|
|
|
|
}
|
|
|
|
|
2018-05-10 15:35:26 +03:00
|
|
|
/**
|
|
|
|
* Returns an db result and throws exceptions when there are more or less
|
|
|
|
* results
|
|
|
|
*
|
|
|
|
* @see findEntity
|
|
|
|
*
|
|
|
|
* @param IQueryBuilder $query
|
|
|
|
* @throws DoesNotExistException if the item does not exist
|
|
|
|
* @throws MultipleObjectsReturnedException if more than one item exist
|
|
|
|
* @return array the result as row
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
protected function findOneQuery(IQueryBuilder $query): array {
|
|
|
|
$cursor = $query->execute();
|
|
|
|
|
|
|
|
$row = $cursor->fetch();
|
|
|
|
if($row === false) {
|
|
|
|
$cursor->closeCursor();
|
|
|
|
$msg = $this->buildDebugMessage(
|
|
|
|
'Did expect one result but found none when executing', $query
|
|
|
|
);
|
|
|
|
throw new DoesNotExistException($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
$row2 = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
2020-04-09 17:07:47 +03:00
|
|
|
if($row2 !== false) {
|
2018-05-10 15:35:26 +03:00
|
|
|
$msg = $this->buildDebugMessage(
|
|
|
|
'Did not expect more than one result when executing', $query
|
|
|
|
);
|
|
|
|
throw new MultipleObjectsReturnedException($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $msg
|
|
|
|
* @param IQueryBuilder $sql
|
|
|
|
* @return string
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
private function buildDebugMessage(string $msg, IQueryBuilder $sql): string {
|
|
|
|
return $msg .
|
|
|
|
': query "' . $sql->getSQL() . '"; ';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an entity from a row. Automatically determines the entity class
|
|
|
|
* from the current mapper name (MyEntityMapper -> MyEntity)
|
|
|
|
*
|
|
|
|
* @param array $row the row which should be converted to an entity
|
|
|
|
* @return Entity the entity
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
protected function mapRowToEntity(array $row): Entity {
|
|
|
|
return \call_user_func($this->entityClass .'::fromRow', $row);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs a sql query and returns an array of entities
|
|
|
|
*
|
|
|
|
* @param IQueryBuilder $query
|
|
|
|
* @return Entity[] all fetched entities
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
protected function findEntities(IQueryBuilder $query): array {
|
|
|
|
$cursor = $query->execute();
|
|
|
|
|
|
|
|
$entities = [];
|
|
|
|
|
|
|
|
while($row = $cursor->fetch()){
|
|
|
|
$entities[] = $this->mapRowToEntity($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cursor->closeCursor();
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an db result and throws exceptions when there are more or less
|
|
|
|
* results
|
|
|
|
*
|
|
|
|
* @param IQueryBuilder $query
|
|
|
|
* @throws DoesNotExistException if the item does not exist
|
|
|
|
* @throws MultipleObjectsReturnedException if more than one item exist
|
|
|
|
* @return Entity the entity
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
protected function findEntity(IQueryBuilder $query): Entity {
|
|
|
|
return $this->mapRowToEntity($this->findOneQuery($query));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|