Skip to content

Commit

Permalink
refactor(dav-backend): Since we're in a transaction, use QB properly …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
tcitworld committed Mar 16, 2023
1 parent bedf6d2 commit 01cd399
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
21 changes: 11 additions & 10 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ public function getMultipleCalendarObjects($calendarId, array $uris, $calendarTy
public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$extraData = $this->getDenormalizedData($calendarData);

$this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) {
return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) {
// Try to detect duplicates
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('*'))
Expand Down Expand Up @@ -1308,7 +1308,7 @@ public function createCalendarObject($calendarId, $objectUri, $calendarData, $ca
public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$extraData = $this->getDenormalizedData($calendarData);

$this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) {
return $this->atomic(function () use ($calendarId, $objectUri, $calendarData, $extraData, $calendarType) {
$query = $this->db->getQueryBuilder();
$query->update('calendarobjects')
->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB))
Expand Down Expand Up @@ -2274,7 +2274,7 @@ public function getCalendarObjectById(string $principalUri, int $id): ?array {
* @param int $syncLevel
* @param int|null $limit
* @param int $calendarType
* @return array
* @return array|null
*/
public function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
return $this->atomic(function () use ($calendarId, $syncToken, $syncLevel, $limit, $calendarType) {
Expand Down Expand Up @@ -2686,10 +2686,10 @@ public function createSchedulingObject($principalUri, $objectUri, $objectData) {
* @param int $calendarType
* @return void
*/
protected function addChange($calendarId, $objectUri, $operation, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->atomic(function () use ($calendarId, $objectUri, $operation, $calendarType) {
$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
protected function addChange(int $calendarId, string $objectUri, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void {
$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';

$this->atomic(function () use ($calendarId, $objectUri, $operation, $calendarType, $table) {
$query = $this->db->getQueryBuilder();
$query->select('synctoken')
->from($table)
Expand All @@ -2709,10 +2709,11 @@ protected function addChange($calendarId, $objectUri, $operation, $calendarType
])
->executeStatement();

$stmt = $this->db->prepare("UPDATE `*PREFIX*$table` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?");
$stmt->execute([
$calendarId
]);
$query = $this->db->getQueryBuilder();
$query->update($table)
->set('synctoken', $query->createNamedParameter($syncToken + 1, IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)))
->executeStatement();
}, $this->db);
}

Expand Down
37 changes: 24 additions & 13 deletions apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,20 +923,31 @@ public function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel,
* @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);
}

Expand Down

0 comments on commit 01cd399

Please sign in to comment.