File indexing completed on 2025-01-26 05:00:55
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Ivan Cukic <ivan.cukic(at)kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include <KCrash> 0010 #include <QSqlQuery> 0011 #include <common/database/schema/ResourcesDatabaseSchema.h> 0012 #include <memory> 0013 0014 #include "DebugResources.h" 0015 0016 namespace Utils 0017 { 0018 static unsigned int errorCount = 0; 0019 0020 inline bool prepare(Common::Database &database, QSqlQuery &query, const QString &queryString) 0021 { 0022 Q_UNUSED(database); 0023 0024 return query.prepare(queryString); 0025 } 0026 0027 inline bool prepare(Common::Database &database, std::unique_ptr<QSqlQuery> &query, const QString &queryString) 0028 { 0029 if (query) { 0030 return true; 0031 } 0032 0033 query.reset(new QSqlQuery(database.createQuery())); 0034 0035 return prepare(database, *query, queryString); 0036 } 0037 0038 enum ErrorHandling { 0039 IgnoreError, 0040 FailOnError, 0041 }; 0042 0043 inline bool exec(Common::Database &database, ErrorHandling eh, QSqlQuery &query) 0044 { 0045 bool success = query.exec(); 0046 0047 if (eh == FailOnError) { 0048 if ((!success) && (errorCount++ < 2)) { 0049 qCWarning(KAMD_LOG_RESOURCES) << query.lastQuery(); 0050 qCWarning(KAMD_LOG_RESOURCES) << query.lastError(); 0051 KCrash::setErrorMessage(query.lastError().text()); 0052 } 0053 Q_ASSERT_X(success, "Uils::exec", qPrintable(QStringLiteral("Query failed:") + query.lastError().text())); 0054 0055 if (!success) { 0056 database.reportError(query.lastError()); 0057 } 0058 } 0059 0060 return success; 0061 } 0062 0063 template<typename T1, typename T2, typename... Ts> 0064 inline bool exec(Common::Database &database, ErrorHandling eh, QSqlQuery &query, const T1 &variable, const T2 &value, Ts... ts) 0065 { 0066 query.bindValue(variable, value); 0067 0068 return exec(database, eh, query, ts...); 0069 } 0070 0071 } // namespace Utils