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
125
126
127
128
129
130
131
132
133
134
|
<?php
/**
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Mail\Db;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use function array_map;
/**
* @template-extends QBMapper<CollectedAddress>
*/
class CollectedAddressMapper extends QBMapper {
/**
* @param IDBConnection $db
*/
public function __construct(IDBConnection $db) {
parent::__construct($db, 'mail_coll_addresses');
}
/**
* Find email addresses by query string
*
* @param string $userId
* @param string $query
*
* @return CollectedAddress[]
*/
public function findMatching($userId, $query) {
$qb = $this->db->getQueryBuilder();
$dbQuery = $qb
->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->orX(
$qb->expr()->iLike('email', $qb->createNamedParameter("%$query%")),
$qb->expr()->iLike('display_name', $qb->createNamedParameter("%$query%"))
));
return $this->findEntities($dbQuery);
}
/**
* @param null|string $email
* @return bool
*/
public function exists(string $userId, ?string $email) {
$qb = $this->db->getQueryBuilder();
$dbQuery = $qb
->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->iLike('email', $qb->createNamedParameter($email)));
return count($this->findEntities($dbQuery)) > 0;
}
public function getTotal(): int {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count())
->from($this->getTableName());
$result = $qb->execute();
$count = (int)$result->fetchColumn();
$result->closeCursor();
return $count;
}
/**
* @param int|null $minId
*
* @return CollectedAddress[]
*
* @psalm-return array<CollectedAddress>
*/
public function getChunk($minId = null): array {
/* @var $qb IQueryBuilder */
$qb = $this->db->getQueryBuilder();
$query = $qb->select('*')
->from($this->getTableName())
->orderBy('id')
->setMaxResults(100);
if ($minId !== null) {
$query = $query->where($qb->expr()->gte('id',
$qb->createNamedParameter($minId)));
}
return $this->findEntities($query);
}
public function deleteOrphans(): void {
$qb1 = $this->db->getQueryBuilder();
$idsQuery = $qb1->selectDistinct('c.id')
->from($this->getTableName(), 'c')
->leftJoin('c', 'mail_accounts', 'a', $qb1->expr()->eq('c.user_id', 'a.user_id'))
->where($qb1->expr()->isNull('a.id'));
$result = $idsQuery->execute();
$ids = array_map(function (array $row) {
return (int)$row['id'];
}, $result->fetchAll());
$result->closeCursor();
$qb2 = $this->db->getQueryBuilder();
$query = $qb2
->delete($this->getTableName())
->where($qb2->expr()->in('id', $qb2->createParameter('ids')));
foreach (array_chunk($ids, 1000) as $chunk) {
$query->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
$query->execute();
}
}
}
|