LeechCraft  0.6.70-18450-gabe19ee3b0
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 <QDir>
11 #include <QFile>
12 #include <QSqlError>
13 #include <QSqlQuery>
14 #include <QThread>
15 #include <QRandomGenerator>
16 #include <util/sys/paths.h>
17 #include <util/sll/qtutil.h>
18 #include "dblock.h"
19 
20 namespace LC::Util
21 {
22  QSqlQuery RunTextQuery (const QSqlDatabase& db, const QString& text)
23  {
24  QSqlQuery query { db };
25  if (!query.exec (text))
26  {
27  qDebug () << "unable to execute query";
28  DBLock::DumpError (query);
29  throw std::runtime_error { "unable to execute query" };
30  }
31  return query;
32  }
33 
34  QString LoadQuery (const QString& pluginName, const QString& filename)
35  {
36  QFile file { ":/" + pluginName + "/resources/sql/" + filename + ".sql" };
37  if (!file.open (QIODevice::ReadOnly))
38  {
39  qWarning () << Q_FUNC_INFO
40  << file.fileName ()
41  << file.errorString ();
42  throw std::runtime_error { "Cannot open query file" };
43  }
44 
45  return QString::fromUtf8 (file.readAll ());
46  }
47 
48  void RunQuery (const QSqlDatabase& db, const QString& pluginName, const QString& filename)
49  {
50  QSqlQuery query { db };
51  query.prepare (LoadQuery (pluginName, filename));
52  Util::DBLock::Execute (query);
53  }
54 
55  QString GenConnectionName (const QString& base)
56  {
57  return (base + ".%1_%2")
58  .arg (QRandomGenerator::global ()->generate ())
59  .arg (std::bit_cast<uintptr_t> (QThread::currentThread ()));
60  }
61 
62  QSqlDatabase OpenSqliteDatabase (const SqliteDatabaseConfig& config)
63  {
64  auto db = QSqlDatabase::addDatabase ("QSQLITE", Util::GenConnectionName (config.Connection_));
65  db.setDatabaseName (Util::GetUserDir (config.DirKind_, config.Dir_).filePath (config.Filename_));
66  if (!db.open ())
67  {
68  qWarning () << "cannot open the database";
69  Util::DBLock::DumpError (db.lastError ());
70  throw std::runtime_error { "cannot create database" };
71  }
72 
73  RunTextQuery (db, "PRAGMA synchronous = NORMAL;"_qs);
74  RunTextQuery (db, "PRAGMA journal_mode = WAL;"_qs);
75 
76  return db;
77  }
78 }
static UTIL_DB_API void DumpError(const QSqlError &error)
Dumps the error to the qWarning() stream.
Definition: dblock.cpp:68
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition: paths.cpp:97
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:48
static UTIL_DB_API void Execute(QSqlQuery &query)
Tries to execute the given query.
Definition: dblock.cpp:84
QString LoadQuery(const QString &pluginName, const QString &filename)
Loads the query text from the given resource file.
Definition: util.cpp:34
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition: util.cpp:22
QSqlDatabase OpenSqliteDatabase(const SqliteDatabaseConfig &config)
Definition: util.cpp:62
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition: util.cpp:55