LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <QVariant>
12#include <QSqlQuery>
13#include <QtDebug>
14#include "dbconfig.h"
15
16class QSqlDatabase;
17class QString;
18
19namespace LC::Util
20{
36 UTIL_DB_API QSqlQuery RunTextQuery (const QSqlDatabase& db, const QString& text);
37
58 UTIL_DB_API QString LoadQuery (const QString& plugin, const QString& filename);
59
81 UTIL_DB_API void RunQuery (const QSqlDatabase& db, const QString& plugin, const QString& filename);
82
94 template<typename T = int>
95 T GetLastId (const QSqlQuery& query)
96 {
97 const auto& lastVar = query.lastInsertId ();
98 if (lastVar.isNull ())
99 throw std::runtime_error { "No last ID has been reported." };
100
101 if (!lastVar.canConvert<T> ())
102 {
103 qWarning () << Q_FUNC_INFO
104 << "cannot convert"
105 << lastVar;
106 throw std::runtime_error { "Cannot convert last ID." };
107 }
108
109 return lastVar.value<T> ();
110 }
111
123 UTIL_DB_API QString GenConnectionName (const QString& base);
124}
#define UTIL_DB_API
Definition dbconfig.h:16
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition util.cpp:18
QString LoadQuery(const QString &pluginName, const QString &filename)
Loads the query text from the given resource file.
Definition util.cpp:30
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition util.cpp:51
void RunQuery(const QSqlDatabase &db, const QString &pluginName, const QString &filename)
Loads the query from the given resource file and runs it.
Definition util.cpp:44
T GetLastId(const QSqlQuery &query)
Gets the last insert ID for the given query.
Definition util.h:95