LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
util.cpp
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#include "util.h"
10#include <QFile>
11#include <QSqlQuery>
12#include <QThread>
13#include "dblock.h"
14
15namespace LC::Util
16{
17 QSqlQuery RunTextQuery (const QSqlDatabase& db, const QString& text)
18 {
19 QSqlQuery query { db };
20 if (!query.exec (text))
21 {
22 qDebug () << "unable to execute query";
23 DBLock::DumpError (query);
24 throw std::runtime_error { "unable to execute query" };
25 }
26 return query;
27 }
28
29 QString LoadQuery (const QString& pluginName, const QString& filename)
30 {
31 QFile file { ":/" + pluginName + "/resources/sql/" + filename + ".sql" };
32 if (!file.open (QIODevice::ReadOnly))
33 {
34 qWarning () << Q_FUNC_INFO
35 << file.fileName ()
36 << file.errorString ();
37 throw std::runtime_error { "Cannot open query file" };
38 }
39
40 return QString::fromUtf8 (file.readAll ());
41 }
42
43 void RunQuery (const QSqlDatabase& db, const QString& pluginName, const QString& filename)
44 {
45 QSqlQuery query { db };
46 query.prepare (LoadQuery (pluginName, filename));
48 }
49
50 QString GenConnectionName (const QString& base)
51 {
52 return (base + ".%1_%2")
53 .arg (qrand ())
54 .arg (reinterpret_cast<uintptr_t> (QThread::currentThread ()));
55 }
56}
static UTIL_DB_API void Execute(QSqlQuery &query)
Tries to execute the given query.
Definition: dblock.cpp:81
static UTIL_DB_API void DumpError(const QSqlError &error)
Dumps the error to the qWarning() stream.
Definition: dblock.cpp:68
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition: util.cpp:17
QString LoadQuery(const QString &pluginName, const QString &filename)
Loads the query text from the given resource file.
Definition: util.cpp:29
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition: util.cpp:50
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:43