Move getChangesForAddressBook to QueryBuilder

Makes psalm happy, and makes me happy.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2021-02-15 22:13:03 +01:00
parent 644e6df02e
commit ff1c5c4e25
1 changed files with 32 additions and 12 deletions

View File

@ -857,14 +857,20 @@ class CardDavBackend implements BackendInterface, SyncSupport {
* @param string $addressBookId * @param string $addressBookId
* @param string $syncToken * @param string $syncToken
* @param int $syncLevel * @param int $syncLevel
* @param int $limit * @param int|null $limit
* @return array * @return array
*/ */
public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) {
// Current synctoken // Current synctoken
$stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); $qb = $this->db->getQueryBuilder();
$stmt->execute([$addressBookId]); $qb->select('synctoken')
->from('addressbooks')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($addressBookId))
);
$stmt = $qb->execute();
$currentToken = $stmt->fetchOne(); $currentToken = $stmt->fetchOne();
$stmt->closeCursor();
if (is_null($currentToken)) { if (is_null($currentToken)) {
return null; return null;
@ -878,14 +884,23 @@ class CardDavBackend implements BackendInterface, SyncSupport {
]; ];
if ($syncToken) { if ($syncToken) {
$query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; $qb = $this->db->getQueryBuilder();
if ($limit > 0) { $qb->select('uri', 'operation')
$query .= " LIMIT " . (int)$limit; ->from('addressbookchanges')
->where(
$qb->expr()->andX(
$qb->expr()->gte('synctoken', $qb->createNamedParameter($syncToken)),
$qb->expr()->lt('synctoken', $qb->createNamedParameter($currentToken)),
$qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId))
)
)->orderBy('synctoken');
if (is_int($limit) && $limit > 0) {
$qb->setMaxResults($limit);
} }
// Fetching all changes // Fetching all changes
$stmt = $this->db->prepare($query); $stmt = $qb->execute();
$stmt->execute([$syncToken, $currentToken, $addressBookId]);
$changes = []; $changes = [];
@ -894,6 +909,7 @@ class CardDavBackend implements BackendInterface, SyncSupport {
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$changes[$row['uri']] = $row['operation']; $changes[$row['uri']] = $row['operation'];
} }
$stmt->closeCursor();
foreach ($changes as $uri => $operation) { foreach ($changes as $uri => $operation) {
switch ($operation) { switch ($operation) {
@ -909,12 +925,16 @@ class CardDavBackend implements BackendInterface, SyncSupport {
} }
} }
} else { } else {
$qb = $this->db->getQueryBuilder();
$qb->select('uri')
->from('cards')
->where(
$qb->expr()->eq('addressbookid', $qb->createNamedParameter($addressBookId))
);
// No synctoken supplied, this is the initial sync. // No synctoken supplied, this is the initial sync.
$query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; $stmt = $qb->execute();
$stmt = $this->db->prepare($query);
$stmt->execute([$addressBookId]);
$result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN);
$stmt->closeCursor();
} }
return $result; return $result;
} }