Skip to content

Commit 02df775

Browse files
committed
feat: add relation lookup column type
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent ddc1bd1 commit 02df775

File tree

29 files changed

+960
-103
lines changed

29 files changed

+960
-103
lines changed

lib/Controller/Api1Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ public function indexViewRelations(int $viewId): DataResponse {
877877
* @param int|null $tableId Table ID
878878
* @param int|null $viewId View ID
879879
* @param string $title Title
880-
* @param 'text'|'number'|'datetime'|'select'|'usergroup'|'relation' $type Column main type
880+
* @param 'text'|'number'|'datetime'|'select'|'usergroup'|'relation'|'relation_lookup' $type Column main type
881881
* @param string|null $subtype Column sub type
882882
* @param bool $mandatory Is the column mandatory
883883
* @param string|null $description Description
@@ -1640,7 +1640,7 @@ public function createTableShare(int $tableId, string $receiver, string $receive
16401640
*
16411641
* @param int $tableId Table ID
16421642
* @param string $title Title
1643-
* @param 'text'|'number'|'datetime'|'select'|'usergroup'|'relation' $type Column main type
1643+
* @param 'text'|'number'|'datetime'|'select'|'usergroup'|'relation'|'relation_lookup' $type Column main type
16441644
* @param string|null $subtype Column sub type
16451645
* @param bool $mandatory Is the column mandatory
16461646
* @param string|null $description Description

lib/Db/Column.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class Column extends EntitySuper implements JsonSerializable {
101101
public const TYPE_DATETIME = 'datetime';
102102
public const TYPE_USERGROUP = 'usergroup';
103103
public const TYPE_RELATION = 'relation';
104+
public const TYPE_RELATION_LOOKUP = 'relation_lookup';
104105

105106
public const SUBTYPE_DATETIME_DATE = 'date';
106107
public const SUBTYPE_DATETIME_TIME = 'time';

lib/Db/Row2Mapper.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function delete(Row2 $row): Row2 {
5858
$this->db->beginTransaction();
5959
try {
6060
foreach ($this->columnsHelper->columns as $columnType) {
61+
if ($this->isVirtualColumn($columnType)) {
62+
continue;
63+
}
6164
$this->getCellMapperFromType($columnType)->deleteAllForRow($row->getId());
6265
}
6366
$this->rowSleeveMapper->deleteById($row->getId());
@@ -195,6 +198,8 @@ public function findAll(array $showColumnIds, int $tableId, ?int $limit = null,
195198
private function getRows(array $rowIds, array $columnIds): array {
196199
$qb = $this->db->getQueryBuilder();
197200

201+
$columnIds = $this->addRelationColumnIdsForSupplementColumns($columnIds);
202+
198203
$qbSqlForColumnTypes = null;
199204
foreach ($this->columnsHelper->columns as $columnType) {
200205
$qbTmp = $this->db->getQueryBuilder();
@@ -210,6 +215,9 @@ private function getRows(array $rowIds, array $columnIds): array {
210215
$qbTmp->selectAlias($qbTmp->createFunction('NULL'), 'value_type');
211216
}
212217

218+
if ($this->isVirtualColumn($columnType)) {
219+
continue;
220+
}
213221
$qbTmp
214222
->from('tables_row_cells_' . $columnType)
215223
->where($qb->expr()->in('column_id', $qb->createNamedParameter($columnIds, IQueryBuilder::PARAM_INT_ARRAY, ':columnIds')))
@@ -798,6 +806,10 @@ private function insertCell(int $rowId, int $columnId, $value, ?string $lastEdit
798806
throw new InternalError(get_class($this) . ' - ' . __FUNCTION__ . ': ' . $e->getMessage());
799807
}
800808

809+
if ($this->isVirtualColumn($column->getType())) {
810+
return;
811+
}
812+
801813
// insert new cell
802814
$cellMapper = $this->getCellMapper($column);
803815

@@ -836,6 +848,10 @@ private function insertCell(int $rowId, int $columnId, $value, ?string $lastEdit
836848
* @throws InternalError
837849
*/
838850
private function updateCell(RowCellSuper $cell, RowCellMapperSuper $mapper, $value, Column $column): void {
851+
if ($this->isVirtualColumn($column->getType())) {
852+
return;
853+
}
854+
839855
$this->getCellMapper($column)->applyDataToEntity($column, $cell, $value);
840856
$this->updateMetaData($cell);
841857
$mapper->updateWrapper($cell);
@@ -846,6 +862,9 @@ private function updateCell(RowCellSuper $cell, RowCellMapperSuper $mapper, $val
846862
*/
847863
private function insertOrUpdateCell(int $rowId, int $columnId, $value): void {
848864
$column = $this->columnMapper->find($columnId);
865+
if ($this->isVirtualColumn($column->getType())) {
866+
return;
867+
}
849868
$cellMapper = $this->getCellMapper($column);
850869
try {
851870
if ($cellMapper->hasMultipleValues()) {
@@ -872,6 +891,9 @@ private function getCellMapper(Column $column): RowCellMapperSuper {
872891
}
873892

874893
private function getCellMapperFromType(string $columnType): RowCellMapperSuper {
894+
if ($this->isVirtualColumn($columnType)) {
895+
throw new InternalError('Virtual columns do not have cell mappers');
896+
}
875897
$cellMapperClassName = 'OCA\Tables\Db\RowCell' . ucfirst($columnType) . 'Mapper';
876898
/** @var RowCellMapperSuper $cellMapper */
877899
try {
@@ -893,6 +915,9 @@ private function getColumnDbParamType(Column $column): int {
893915
* @throws InternalError
894916
*/
895917
public function deleteDataForColumn(Column $column): void {
918+
if ($this->isVirtualColumn($column->getType())) {
919+
return;
920+
}
896921
try {
897922
$this->getCellMapper($column)->deleteAllForColumn($column->getId());
898923
} catch (Exception $e) {
@@ -961,6 +986,9 @@ private function getFormattedDefaultValue(Column $column) {
961986
case Column::TYPE_USERGROUP:
962987
$defaultValue = $this->getCellMapper($column)->filterValueToQueryParam($column, $column->getUsergroupDefault());
963988
break;
989+
case Column::TYPE_RELATION_LOOKUP:
990+
$defaultValue = null;
991+
break;
964992
}
965993
return $defaultValue;
966994
}
@@ -988,4 +1016,29 @@ private function sortRowsByIds(array $rows, array $wantedRowIds): array {
9881016

9891017
return $sortedRows;
9901018
}
1019+
1020+
public function isVirtualColumn(string $columnType): bool {
1021+
return $columnType === Column::TYPE_RELATION_LOOKUP;
1022+
}
1023+
1024+
/**
1025+
* @param int[] $columnIds
1026+
* @return int[]
1027+
*/
1028+
public function addRelationColumnIdsForSupplementColumns(array $columnIds): array {
1029+
$allColumns = $this->columnMapper->findAll($columnIds);
1030+
foreach ($allColumns as $column) {
1031+
if ($column->getType() !== Column::TYPE_RELATION_LOOKUP) {
1032+
continue;
1033+
}
1034+
1035+
$customSettings = $column->getCustomSettingsArray();
1036+
$relationColumnId = $customSettings['relationColumnId'] ?? null;
1037+
if ($relationColumnId && !in_array($relationColumnId, $columnIds)) {
1038+
$columnIds[] = $relationColumnId;
1039+
}
1040+
}
1041+
return $columnIds;
1042+
}
1043+
9911044
}

lib/Helper/ColumnsHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ColumnsHelper {
1919
Column::TYPE_SELECTION,
2020
Column::TYPE_USERGROUP,
2121
Column::TYPE_RELATION,
22+
Column::TYPE_RELATION_LOOKUP,
2223
];
2324

2425
public function __construct(

lib/Service/ColumnService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ public function findOrCreateColumnsByTitleForTableAsArray(?int $tableId, ?int $v
477477
foreach ($titles as $title) {
478478
$i++;
479479
foreach ($allColumns as $column) {
480+
// Skip matching columns with type relation_lookup
481+
if ($column->getType() === Column::TYPE_RELATION_LOOKUP) {
482+
continue;
483+
}
484+
480485
if ($column->getTitle() === $title) {
481486
$result[$i] = $column;
482487
$countMatchingColumns++;

0 commit comments

Comments
 (0)