diff options
author | Thomas Citharel <tcit@tcit.fr> | 2023-03-20 17:45:50 +0100 |
---|---|---|
committer | Simon L. (Rebase PR Action) <szaimen@e.mail.de> | 2023-04-17 16:08:44 +0000 |
commit | ff3b69b21dad865973dfabde34c8d4cd6f951380 (patch) | |
tree | 4c500be76b4f6d994e5a110549d4d20ec1c0d114 /apps/dav/lib/CardDAV/CardDavBackend.php | |
parent | c9a3129cb4e33efab6b65af37a2e7de117153788 (diff) | |
download | nextcloud-server-ff3b69b21dad865973dfabde34c8d4cd6f951380.tar.gz nextcloud-server-ff3b69b21dad865973dfabde34c8d4cd6f951380.zip |
refactor(dav): Since we're in a transaction, use QB properly when incrementing synctoken
Now that we're in a transaction, we can reuse the sync token's previous value without trouble, and rewrite the increment UPDATE query without dirty direct SQL.
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'apps/dav/lib/CardDAV/CardDavBackend.php')
-rw-r--r-- | apps/dav/lib/CardDAV/CardDavBackend.php | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/apps/dav/lib/CardDAV/CardDavBackend.php b/apps/dav/lib/CardDAV/CardDavBackend.php index 524a1ee117a..577d7282eae 100644 --- a/apps/dav/lib/CardDAV/CardDavBackend.php +++ b/apps/dav/lib/CardDAV/CardDavBackend.php @@ -923,20 +923,31 @@ class CardDavBackend implements BackendInterface, SyncSupport { * @param int $operation 1 = add, 2 = modify, 3 = delete * @return void */ - protected function addChange($addressBookId, $objectUri, $operation) { + protected function addChange(int $addressBookId, string $objectUri, int $operation): void { $this->atomic(function () use ($addressBookId, $objectUri, $operation) { - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; - $stmt = $this->db->prepare($sql); - $stmt->execute([ - $objectUri, - $addressBookId, - $operation, - $addressBookId - ]); - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); - $stmt->execute([ - $addressBookId - ]); + $query = $this->db->getQueryBuilder(); + $query->select('synctoken') + ->from('addressbooks') + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))); + $result = $query->executeQuery(); + $syncToken = (int)$result->fetchOne(); + $result->closeCursor(); + + $query = $this->db->getQueryBuilder(); + $query->insert('addressbookchanges') + ->values([ + 'uri' => $query->createNamedParameter($objectUri), + 'synctoken' => $query->createNamedParameter($syncToken), + 'addressbookid' => $query->createNamedParameter($addressBookId), + 'operation' => $query->createNamedParameter($operation), + ]) + ->executeStatement(); + + $query = $this->db->getQueryBuilder(); + $query->update('addressbooks') + ->set('synctoken', $query->createNamedParameter($syncToken + 1, IQueryBuilder::PARAM_INT)) + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) + ->executeStatement(); }, $this->db); } |