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