LeechCraft  0.6.70-13729-g7046a9d2a7
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 LC
37 {
38 namespace Util
39 {
40  QSqlQuery RunTextQuery (const QSqlDatabase& db, const QString& text)
41  {
42  QSqlQuery query { db };
43  if (!query.exec (text))
44  {
45  qDebug () << "unable to execute query";
46  DBLock::DumpError (query);
47  throw std::runtime_error { "unable to execute query" };
48  }
49  return query;
50  }
51 
52  QString LoadQuery (const QString& pluginName, const QString& filename)
53  {
54  QFile file { ":/" + pluginName + "/resources/sql/" + filename + ".sql" };
55  if (!file.open (QIODevice::ReadOnly))
56  {
57  qWarning () << Q_FUNC_INFO
58  << file.fileName ()
59  << file.errorString ();
60  throw std::runtime_error { "Cannot open query file" };
61  }
62 
63  return QString::fromUtf8 (file.readAll ());
64  }
65 
66  void RunQuery (const QSqlDatabase& db, const QString& pluginName, const QString& filename)
67  {
68  QSqlQuery query { db };
69  query.prepare (LoadQuery (pluginName, filename));
70  Util::DBLock::Execute (query);
71  }
72 
73  namespace
74  {
75  template<typename To, typename From>
76  std::enable_if_t<std::is_same<From, To> {}, To> DumbCast (From from)
77  {
78  return from;
79  }
80 
81  template<typename To, typename From>
82  std::enable_if_t<!std::is_same<From, To> {} &&
83  std::is_integral<From> {} &&
84  std::is_integral<To> {}, To> DumbCast (From from)
85  {
86  return static_cast<To> (from);
87  }
88 
89  template<typename To, typename From>
90  std::enable_if_t<!std::is_same<From, To> {} &&
91  !(std::is_integral<From> {} &&
92  std::is_integral<To> {}), To> DumbCast (From from)
93  {
94  return reinterpret_cast<To> (from);
95  }
96 
97  uintptr_t Handle2Num (Qt::HANDLE handle)
98  {
99  return DumbCast<uintptr_t> (handle);
100  }
101  }
102 
103  QString GenConnectionName (const QString& base)
104  {
105  return (base + ".%1_%2")
106  .arg (qrand ())
107  .arg (Handle2Num (QThread::currentThreadId ()));
108  }
109 }
110 }
static UTIL_DB_API void DumpError(const QSqlError &error)
Dumps the error to the qWarning() stream.
Definition: dblock.cpp:84
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:66
static UTIL_DB_API void Execute(QSqlQuery &query)
Tries to execute the given query.
Definition: dblock.cpp:97
QString LoadQuery(const QString &pluginName, const QString &filename)
Loads the query text from the given resource file.
Definition: util.cpp:52
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition: util.cpp:40
Definition: constants.h:35
QString GenConnectionName(const QString &base)
Generates an unique thread-safe connection name.
Definition: util.cpp:103