-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathDocumentMapper.php
More file actions
88 lines (78 loc) · 2.16 KB
/
DocumentMapper.php
File metadata and controls
88 lines (78 loc) · 2.16 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
<?php
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Text\Db;
use Generator;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/** @extends QBMapper<Document> */
class DocumentMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'text_documents', Document::class);
}
/**
* @param $documentId
* @return Document
* @throws DoesNotExistException
*/
public function find(int $documentId): Document {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($documentId)))
->executeQuery();
$data = $result->fetch();
$result->closeCursor();
if ($data === false) {
throw new DoesNotExistException('Document doesn\'t exist');
}
return Document::fromRow($data);
}
public function findAll(): Generator {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('*')
->from($this->getTableName())
->executeQuery();
try {
while ($row = $result->fetch()) {
yield $this->mapRowToEntity($row);
}
} finally {
$result->closeCursor();
}
}
public function findAllWithNoActiveSessions(): Generator {
$qb = $this->db->getQueryBuilder();
$result = $qb->select('d.*')
->from($this->getTableName(), 'd')
->leftJoin('d', 'text_sessions', 's', $qb->expr()->eq('s.document_id', 'd.id'))
->where($qb->expr()->isNull('s.id'))
->executeQuery();
try {
while ($row = $result->fetch()) {
yield $this->mapRowToEntity($row);
}
} finally {
$result->closeCursor();
}
}
public function countAll(): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('id'))
->from($this->getTableName());
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}
public function clearAll(): void {
$qb = $this->db->getQueryBuilder();
$qb->delete($this->getTableName())
->executeStatement();
}
}