File indexing completed on 2025-01-05 04:47:01

0001 /*
0002     SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QString>
0010 
0011 namespace Akonadi
0012 {
0013 namespace Server
0014 {
0015 class DataStore;
0016 
0017 /**
0018   Helper class for DataStore transaction handling.
0019   Works similar to QMutexLocker.
0020   Supports command-local and session-global transactions.
0021 */
0022 class Transaction
0023 {
0024 public:
0025     /**
0026       Starts a new transaction. The transaction will automatically rolled back
0027       on destruction if it hasn't been committed explicitly before.
0028       If there is already a global transaction in progress, this one will be used
0029       instead of creating a new one.
0030       @param db The corresponding DataStore. You must not delete @p db during
0031       the lifetime of a Transaction object.
0032       @param name A name of the transaction. Used for debugging.
0033       @param beginTransaction if false, the transaction won't be started, until begin is explicitly called. The default is to begin the transaction right away.
0034     */
0035     explicit Transaction(DataStore *db, const QString &name, bool beginTransaction = true);
0036 
0037     /**
0038       Rolls back the transaction if it hasn't been committed explicitly.
0039       This also happens if a global transaction is used.
0040     */
0041     ~Transaction();
0042 
0043     /**
0044       Commits the transaction. Returns true on success.
0045       If a global transaction is used, nothing happens, global transactions have
0046       to be committed explicitly.
0047     */
0048     bool commit();
0049 
0050     void begin();
0051 
0052 private:
0053     Q_DISABLE_COPY(Transaction)
0054     DataStore *mDb;
0055     QString mName;
0056     bool mCommitted;
0057 };
0058 
0059 } // namespace Server
0060 } // namespace Akonadi