Skip to content
Merged
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
59 changes: 25 additions & 34 deletions lib/Db/Row2Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\Tables\Db;

use DateTime;
use DateTimeImmutable;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Helper\ColumnsHelper;
Expand All @@ -29,7 +30,7 @@ class Row2Mapper {
use TTransactional;

private RowSleeveMapper $rowSleeveMapper;
private ?string $userId = null;
private ?string $userId;
private IDBConnection $db;
private LoggerInterface $logger;
protected UserHelper $userHelper;
Expand Down Expand Up @@ -79,17 +80,20 @@ public function delete(Row2 $row): Row2 {
public function find(int $id, array $columns): Row2 {
$columnIdsArray = array_map(fn (Column $column) => $column->getId(), $columns);
$rows = $this->getRows([$id], $columnIdsArray);

if (count($rows) === 1) {
return $rows[0];
} elseif (count($rows) === 0) {
}

if (count($rows) === 0) {
$e = new Exception('Wanted row not found.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new NotFoundError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
} else {
$e = new Exception('Too many results for one wanted row.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

$e = new Exception('Too many results for one wanted row.');
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

/**
Expand All @@ -101,7 +105,7 @@ public function findNextId(int $offsetId = -1): ?int {
} catch (MultipleObjectsReturnedException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
} catch (DoesNotExistException $e) {
} catch (DoesNotExistException) {
return null;
}
return $rowSleeve->getId();
Expand All @@ -118,11 +122,6 @@ public function getTableIdForRow(int $rowId): ?int {
}

/**
* @param string $userId
* @param int $tableId
* @param array|null $filter
* @param int|null $limit
* @param int|null $offset
* @return int[]
* @throws InternalError
*/
Expand Down Expand Up @@ -230,20 +229,13 @@ private function getRows(array $rowIds, array $columnIds, array $sort = []): arr
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

try {
$columnTypes = $this->columnMapper->getColumnTypes($columnIds);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

return $this->parseEntities($result, $sleeves, $columnTypes);
return $this->parseEntities($result, $sleeves);
}

/**
* @throws InternalError
*/
private function addFilterToQuery(IQueryBuilder &$qb, array $filters, string $userId): void {
private function addFilterToQuery(IQueryBuilder $qb, array $filters, string $userId): void {
// TODO move this into service
$this->replacePlaceholderValues($filters, $userId);

Expand All @@ -265,6 +257,9 @@ private function addFilterToQuery(IQueryBuilder &$qb, array $filters, string $us
private function addSortQueryForMultipleSleeveFinder(IQueryBuilder $qb, string $sleevesAlias, array $sort): void {
$i = 1;
foreach ($sort as $sortData) {
if (!in_array($sortData['mode'], ['ASC', 'DESC'])) {
continue;
}
$column = $sortData['columnId'] > 0 ? $this->columnMapper->find($sortData['columnId']) : null;
if ($column === null && $sortData['columnId'] > 0) {
throw new InternalError('No column found to build filter with for id ' . $sortData['columnId']);
Expand Down Expand Up @@ -312,7 +307,7 @@ private function addSortQueryForMultipleSleeveFinder(IQueryBuilder $qb, string $
private function replacePlaceholderValues(array &$filters, string $userId): void {
foreach ($filters as &$filterGroup) {
foreach ($filterGroup as &$filter) {
if (substr($filter['value'], 0, 1) === '@') {
if (str_starts_with($filter['value'], '@')) {
$filter['value'] = $this->columnsHelper->resolveSearchValue($filter['value'], $userId);
}
}
Expand All @@ -322,7 +317,7 @@ private function replacePlaceholderValues(array &$filters, string $userId): void
/**
* @throws InternalError
*/
private function getFilterGroups(IQueryBuilder &$qb, array $filters): array {
private function getFilterGroups(IQueryBuilder $qb, array $filters): array {
$filterGroups = [];
foreach ($filters as $filterGroup) {
$filterGroups[] = $qb->expr()->andX(...$this->getFilter($qb, $filterGroup));
Expand All @@ -333,7 +328,7 @@ private function getFilterGroups(IQueryBuilder &$qb, array $filters): array {
/**
* @throws InternalError
*/
private function getFilter(IQueryBuilder &$qb, array $filterGroup): array {
private function getFilter(IQueryBuilder $qb, array $filterGroup): array {
$filterExpressions = [];
foreach ($filterGroup as $filter) {
$columnId = $filter['columnId'];
Expand Down Expand Up @@ -377,7 +372,6 @@ private function getFilterExpression(IQueryBuilder $qb, Column $column, string $

// We try to match the requested value against the default before building the query
// so we know if we shall include rows that have no entry in the column_TYPE tables upfront
$includeDefault = false;
$defaultValue = $this->getFormattedDefaultValue($column);

$qb2 = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -473,15 +467,13 @@ private function getMetaFilterExpression(IQueryBuilder $qb, int $columnId, strin
$qb2->where($this->getSqlOperator($operator, $qb, 'created_by', $value, IQueryBuilder::PARAM_STR));
break;
case Column::TYPE_META_CREATED_AT:
$value = new \DateTimeImmutable($value);
$qb2->where($this->getSqlOperator($operator, $qb, 'created_at', $value, IQueryBuilder::PARAM_DATE));
$qb2->where($this->getSqlOperator($operator, $qb, 'created_at', new DateTimeImmutable($value), IQueryBuilder::PARAM_DATE));
break;
case Column::TYPE_META_UPDATED_BY:
$qb2->where($this->getSqlOperator($operator, $qb, 'last_edit_by', $value, IQueryBuilder::PARAM_STR));
break;
case Column::TYPE_META_UPDATED_AT:
$value = new \DateTimeImmutable($value);
$qb2->where($this->getSqlOperator($operator, $qb, 'last_edit_at', $value, IQueryBuilder::PARAM_DATE));
$qb2->where($this->getSqlOperator($operator, $qb, 'last_edit_at', new DateTimeImmutable($value), IQueryBuilder::PARAM_DATE));
break;
}
return $qb2;
Expand Down Expand Up @@ -527,7 +519,7 @@ private function getSqlOperator(string $operator, IQueryBuilder $qb, string $col
* @return Row2[]
* @throws InternalError
*/
private function parseEntities(IResult $result, array $sleeves, array $columnTypes): array {
private function parseEntities(IResult $result, array $sleeves): array {
$rows = [];
foreach ($sleeves as $sleeve) {
$rows[$sleeve->getId()] = new Row2();
Expand All @@ -545,7 +537,7 @@ private function parseEntities(IResult $result, array $sleeves, array $columnTyp
$cellMapperCache = [];

while ($rowData = $result->fetch()) {
if (!isset($rowData['row_id']) || !isset($rows[$rowData['row_id']])) {
if (!isset($rowData['row_id'], $rows[$rowData['row_id']])) {
break;
}

Expand Down Expand Up @@ -692,8 +684,7 @@ private function insertCell(int $rowId, int $columnId, $value, ?string $lastEdit
try {
$column = $this->columnMapper->find($columnId);
} catch (DoesNotExistException $e) {
$e = new Exception('Can not insert cell, because the given column-id is not known');
$this->logger->error($e->getMessage(), ['exception' => $e]);
$this->logger->error('Can not insert cell, because the given column-id is not known', ['exception' => $e]);
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
}

Expand Down Expand Up @@ -784,7 +775,7 @@ private function getCellMapperFromType(string $columnType): RowCellMapperSuper {
/**
* @throws InternalError
*/
private function getColumnDbParamType(Column $column) {
private function getColumnDbParamType(Column $column): int {
return $this->getCellMapper($column)->getDbParamType();
}

Expand Down
Loading