-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathPageMapper.php
More file actions
124 lines (107 loc) · 3.08 KB
/
Copy pathPageMapper.php
File metadata and controls
124 lines (107 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Collectives\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/**
* @method Page insert(Page $page)
* @method Page update(Page $page)
* @method Page delete(Page $page)
* @method Page findEntity(IQueryBuilder $query)
* @template-extends QBMapper<Page>
*/
class PageMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'collectives_pages', Page::class);
}
public function updateOrInsert(Page $page): Page {
if ($page->getId() === null
&& null !== $oldPage = $this->findByFileId($page->getFileId())) {
$page->setId($oldPage->getId());
return $this->update($page);
}
return $this->insert($page);
}
public function trashByFileId(int $fileId): ?Page {
if (null !== $page = $this->findByFileId($fileId)) {
$page->setTrashTimestamp(time());
return $this->update($page);
}
return null;
}
public function restoreByFileId(int $fileId): ?Page {
if (null !== $page = $this->findByFileId($fileId, true)) {
$page->setTrashTimestamp(null);
return $this->update($page);
}
return null;
}
public function deleteByFileId(int $fileId): ?Page {
if (null !== $page = $this->findByFileId($fileId, true)) {
return $this->delete($page);
}
return null;
}
public function findByFileId(int $fileId, bool $trashed = false): ?Page {
$qb = $this->db->getQueryBuilder();
$andX = [
$qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)),
];
if ($trashed) {
$andX[] = $qb->expr()->isNotNull('trash_timestamp');
} else {
$andX[] = $qb->expr()->isNull('trash_timestamp');
}
$qb->select('*')
->from($this->tableName)
->where($qb->expr()->andX(...$andX));
try {
return $this->findEntity($qb);
} catch (DoesNotExistException|MultipleObjectsReturnedException) {
return null;
}
}
/**
* @param int[] $fileIds
* @return array<int, Page> Indexed by file_id
*/
public function findByFileIds(array $fileIds, bool $trashed = false): array {
if (empty($fileIds)) {
return [];
}
$pagesByFileId = [];
foreach (array_chunk($fileIds, 1000) as $chunk) {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where($qb->expr()->in('file_id',
$qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
if ($trashed) {
$qb->andWhere($qb->expr()->isNotNull('trash_timestamp'));
} else {
$qb->andWhere($qb->expr()->isNull('trash_timestamp'));
}
$pages = $this->findEntities($qb);
foreach ($pages as $page) {
$pagesByFileId[$page->getFileId()] = $page;
}
}
return $pagesByFileId;
}
/**
* @return Page[]
*/
public function getAll(): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName);
return $this->findEntities($qb);
}
}