File indexing completed on 2025-01-19 03:53:29
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2010-10-12 0007 * Description : Core database convenience object for grouping operations 0008 * 0009 * SPDX-FileCopyrightText: 2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "coredboperationgroup.h" 0016 0017 // Qt includes 0018 0019 #include <QElapsedTimer> 0020 0021 // Local includes 0022 0023 #include "digikam_debug.h" 0024 #include "coredb.h" 0025 #include "coredbaccess.h" 0026 #include "coredbbackend.h" 0027 0028 namespace Digikam 0029 { 0030 0031 class Q_DECL_HIDDEN CoreDbOperationGroup::Private 0032 { 0033 public: 0034 0035 explicit Private() 0036 { 0037 access = nullptr; 0038 acquired = false; 0039 maxTime = 0; 0040 } 0041 0042 public: 0043 0044 CoreDbAccess* access; 0045 bool acquired; 0046 QElapsedTimer timeAcquired; 0047 int maxTime; 0048 0049 public: 0050 0051 bool needsTransaction() const 0052 { 0053 return CoreDbAccess::parameters().isSQLite(); 0054 } 0055 0056 void acquire() 0057 { 0058 if (access) 0059 { 0060 acquired = access->backend()->beginTransaction(); 0061 } 0062 else 0063 { 0064 CoreDbAccess access; 0065 acquired = access.backend()->beginTransaction(); 0066 } 0067 0068 timeAcquired.start(); 0069 } 0070 0071 void release() 0072 { 0073 if (acquired) 0074 { 0075 if (access) 0076 { 0077 access->backend()->commitTransaction(); 0078 } 0079 else 0080 { 0081 CoreDbAccess access; 0082 access.backend()->commitTransaction(); 0083 } 0084 } 0085 } 0086 }; 0087 0088 CoreDbOperationGroup::CoreDbOperationGroup() 0089 : d(new Private) 0090 { 0091 if (d->needsTransaction()) 0092 { 0093 d->acquire(); 0094 } 0095 } 0096 0097 CoreDbOperationGroup::CoreDbOperationGroup(CoreDbAccess* const access) 0098 : d(new Private) 0099 { 0100 d->access = access; 0101 0102 if (d->needsTransaction()) 0103 { 0104 d->acquire(); 0105 } 0106 } 0107 0108 CoreDbOperationGroup::~CoreDbOperationGroup() 0109 { 0110 d->release(); 0111 delete d; 0112 } 0113 0114 void CoreDbOperationGroup::lift() 0115 { 0116 if (d->acquired) 0117 { 0118 d->release(); 0119 0120 if (d->access) 0121 { 0122 CoreDbAccessUnlock unlock(d->access); 0123 } 0124 0125 d->acquire(); 0126 } 0127 } 0128 0129 void CoreDbOperationGroup::setMaximumTime(int msecs) 0130 { 0131 d->maxTime = msecs; 0132 } 0133 0134 void CoreDbOperationGroup::resetTime() 0135 { 0136 d->timeAcquired.start(); 0137 } 0138 0139 void CoreDbOperationGroup::allowLift() 0140 { 0141 if (d->maxTime && d->timeAcquired.elapsed() > d->maxTime) 0142 { 0143 lift(); 0144 } 0145 } 0146 0147 } // namespace Digikam