LeechCraft  0.6.70-10870-g558588d6ec
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "util.h"
31 #include <QFile>
32 #include <QSqlQuery>
33 #include <QThread>
34 #include "dblock.h"
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
40  QSqlQuery RunTextQuery (const QSqlDatabase& db, const QString& text)
41  {
42  QSqlQuery query { db };
43  if (!query.prepare (text))
44  {
45  qDebug () << "unable to prepare query";
46  DBLock::DumpError (query);
47  throw std::runtime_error { "unable to prepare query" };
48  }
49 
50  DBLock::Execute (query);
51 
52  return query;
53  }
54 
55  QString LoadQuery (const QString& pluginName, const QString& filename)
56  {
57  QFile file { ":/" + pluginName + "/resources/sql/" + filename + ".sql" };
58  if (!file.open (QIODevice::ReadOnly))
59  {
60  qWarning () << Q_FUNC_INFO
61  << file.fileName ()
62  << file.errorString ();
63  throw std::runtime_error { "Cannot open query file" };
64  }
65 
66  return QString::fromUtf8 (file.readAll ());
67  }
68 
69  void RunQuery (const QSqlDatabase& db, const QString& pluginName, const QString& filename)
70  {
71  QSqlQuery query { db };
72  query.prepare (LoadQuery (pluginName, filename));
73  Util::DBLock::Execute (query);
74  }
75 
76  namespace
77  {
78  template<typename To, typename From>
79  std::enable_if_t<std::is_same<From, To> {}, To> DumbCast (From from)
80  {
81  return from;
82  }
83 
84  template<typename To, typename From>
85  std::enable_if_t<!std::is_same<From, To> {} &&
86  std::is_integral<From> {} &&
87  std::is_integral<To> {}, To> DumbCast (From from)
88  {
89  return static_cast<To> (from);
90  }
91 
92  template<typename To, typename From>
93  std::enable_if_t<!std::is_same<From, To> {} &&
94  !(std::is_integral<From> {} &&
95  std::is_integral<To> {}), To> DumbCast (From from)
96  {
97  return reinterpret_cast<To> (from);
98  }
99 
100  uintptr_t Handle2Num (Qt::HANDLE handle)
101  {
102  return DumbCast<uintptr_t> (handle);
103  }
104  }
105 
106  QString GenConnectionName (const QString& base)
107  {
108  return (base + ".%1_%2")
109  .arg (qrand ())
110  .arg (Handle2Num (QThread::currentThreadId ()));
111  }
112 }
113 }
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition: util.cpp:106
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:69
QString LoadQuery(const QString &pluginName, const QString &filename)
Loads the query text from the given resource file.
Definition: util.cpp:55
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition: util.cpp:40
static UTIL_DB_API void DumpError(const QSqlError &error)
Dumps the error to the qWarning() stream.
Definition: dblock.cpp:84
static UTIL_DB_API void Execute(QSqlQuery &query)
Tries to execute the given query.
Definition: dblock.cpp:98