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