MythTV  master
mythdbcon.h
Go to the documentation of this file.
1 #ifndef MYTHDBCON_H_
2 #define MYTHDBCON_H_
3 
4 #include <QSqlDatabase>
5 #include <QSqlRecord>
6 #include <QSqlError>
7 #include <QVariant>
8 #include <QSqlQuery>
9 #include <QDateTime>
10 #include <QMutex>
11 #include <QList>
12 
13 #include "mythbaseexp.h"
14 #include "mythdbparams.h"
15 #include "qtuplift.h"
16 
17 #define REUSE_CONNECTION 1
18 
19 MBASE_PUBLIC bool TestDatabase(const QString& dbHostName,
20  const QString& dbUserName,
21  QString dbPassword,
22  QString dbName = "mythconverg",
23  int dbPort = 3306);
24 
27 {
28  friend class MDBManager;
29  friend class MSqlQuery;
30  public:
31  explicit MSqlDatabase(QString name, QString driver = "QMYSQL");
32  ~MSqlDatabase(void);
33 
34  bool OpenDatabase(bool skipdb = false);
35  void SetDBParams(const DatabaseParams &params) { m_dbparms = params; };
36 
37  private:
38  Q_DISABLE_COPY_MOVE(MSqlDatabase)
39  bool isOpen(void);
40  bool KickDatabase(void);
41  QString GetConnectionName(void) const { return m_name; }
42  QSqlDatabase db(void) const { return m_db; }
43  bool Reconnect(void);
44  void InitSessionVars(void);
45 
46  private:
47  QString m_name;
48  QString m_driver;
49  QSqlDatabase m_db;
50  QDateTime m_lastDBKick;
52 };
53 
56 {
57  friend class MSqlQuery;
58  public:
59  MDBManager(void) = default;
60  ~MDBManager(void);
61 
62  void CloseDatabases(void);
63  void PurgeIdleConnections(bool leaveOne = false);
64 
65  protected:
66  MSqlDatabase *popConnection(bool reuse);
67  void pushConnection(MSqlDatabase *db);
68 
69  MSqlDatabase *getSchedCon(void);
70  MSqlDatabase *getChannelCon(void);
71 
72  private:
73  Q_DISABLE_COPY_MOVE(MDBManager)
74  MSqlDatabase *getStaticCon(MSqlDatabase **dbcon, const QString& name);
75 
76  QMutex m_lock;
77  using DBList = QList<MSqlDatabase*>;
78  QHash<QThread*, DBList> m_pool; // protected by m_lock
79 #if REUSE_CONNECTION
80  QHash<QThread*, MSqlDatabase*> m_inuse; // protected by m_lock
81  QHash<QThread*, int> m_inuseCount; // protected by m_lock
82 #endif
83 
84  int m_nextConnID {0};
85  int m_connCount {0};
86 
87  MSqlDatabase *m_schedCon {nullptr};
88  MSqlDatabase *m_channelCon {nullptr};
89  QHash<QThread*, DBList> m_staticPool;
90 };
91 
94 {
95  MSqlDatabase *db {nullptr};
96  QSqlDatabase qsqldb;
97  bool returnConnection {false};
98 };
99 
101 using MSqlBindings = QMap<QString, QVariant>;
102 
105 
107  MBASE_PUBLIC void MSqlEscapeAsAQuery(QString &query, const MSqlBindings &bindings);
108 
128 class MBASE_PUBLIC MSqlQuery : private QSqlQuery
129 {
130  MBASE_PUBLIC friend void MSqlEscapeAsAQuery(QString& query, MSqlBindings& bindings);
131  public:
133  explicit MSqlQuery(const MSqlQueryInfo &qi);
135  ~MSqlQuery();
136 
138  bool isConnected(void) const { return m_isConnected; }
139 
141  bool exec(void);
142 
144  bool next(void);
145 
147  bool previous(void);
148 
150  bool first(void);
151 
153  bool last(void);
154 
156  // so we can display the query results
157  bool seek(int where, bool relative = false);
158 
160  bool exec(const QString &query);
161 
163  bool prepare(const QString &query);
164 
166  void bindValue(const QString &placeholder, const QVariant &val);
167 
181  void bindValueNoNull(const QString &placeholder, const QVariant &val);
182 
184  void bindValues(const MSqlBindings &bindings);
185 
194  QVariant lastInsertId();
195 
198  bool Reconnect(void);
199 
202  bool lostConnectionCheck(void);
203 
204  // Thunks that allow us to make QSqlQuery private
205  QVariant value(int i) const { return QSqlQuery::value(i); }
206  QString executedQuery(void) const { return QSqlQuery::executedQuery(); }
207 
208 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
209  QMap<QString, QVariant> boundValues(void) const
210  { return QSqlQuery::boundValues(); }
211 #else
212  QVariantList boundValues(void) const { return QSqlQuery::boundValues(); }
213 #endif
214  QSqlError lastError(void) const { return QSqlQuery::lastError(); }
215  int size(void) const { return QSqlQuery::size();}
216  bool isActive(void) const { return QSqlQuery::isActive(); }
217  QSqlRecord record(void) const { return QSqlQuery::record(); }
218  int numRowsAffected() const { return QSqlQuery::numRowsAffected(); }
219  void setForwardOnly(bool f) { QSqlQuery::setForwardOnly(f); }
220  bool isNull(int field) const { return QSqlQuery::isNull(field); }
221  const QSqlDriver *driver(void) const { return QSqlQuery::driver(); }
222  int at(void) const { return QSqlQuery::at(); }
223 
225  static bool testDBConnection();
226 
228  {
231  };
233  static MSqlQueryInfo InitCon(ConnectionReuse _reuse = kNormalConnection);
234 
236  static MSqlQueryInfo SchedCon();
237 
239  static MSqlQueryInfo ChannelCon();
240 
241  private:
242  Q_DISABLE_COPY_MOVE(MSqlQuery)
243 
244  // Only QSql::In is supported as a param type and only named params...
245  void bindValue(const QString&, const QVariant&, QSql::ParamType);
246  void bindValue(int, const QVariant&, QSql::ParamType);
247  void addBindValue(const QVariant&, QSql::ParamType = QSql::In);
248 
249  bool seekDebug(const char *type, bool result,
250  int where, bool relative) const;
251 
252  MSqlDatabase *m_db {nullptr};
253  bool m_isConnected {false};
254  bool m_returnConnection {false};
255  QString m_lastPreparedQuery; // holds a copy of the last prepared query
256 };
257 
258 #endif
MSqlBindings
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:101
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:216
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:215
MDBManager::m_lock
QMutex m_lock
Definition: mythdbcon.h:76
MSqlQuery::isNull
bool isNull(int field) const
Definition: mythdbcon.h:220
MSqlDatabase::InitSessionVars
void InitSessionVars(void)
Definition: mythdbcon.cpp:270
DatabaseParams
Structure containing the basic Database parameters.
Definition: mythdbparams.h:10
mythbaseexp.h
MDBManager::m_inuseCount
QHash< QThread *, int > m_inuseCount
Definition: mythdbcon.h:81
MSqlQuery::record
QSqlRecord record(void) const
Definition: mythdbcon.h:217
MDBManager
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:55
MSqlDatabase::KickDatabase
bool KickDatabase(void)
Definition: mythdbcon.cpp:245
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:205
MSqlDatabase::db
QSqlDatabase db(void) const
Definition: mythdbcon.h:42
qtuplift.h
TestDatabase
MBASE_PUBLIC bool TestDatabase(const QString &dbHostName, const QString &dbUserName, QString dbPassword, QString dbName="mythconverg", int dbPort=3306)
Definition: mythdbcon.cpp:39
MSqlEscapeAsAQuery
MBASE_PUBLIC void MSqlEscapeAsAQuery(QString &query, const MSqlBindings &bindings)
Given a partial query string and a bindings object, escape the string.
Definition: mythdbcon.cpp:981
MSqlDatabase::isOpen
bool isOpen(void)
Definition: mythdbcon.cpp:115
MDBManager::DBList
QList< MSqlDatabase * > DBList
Definition: mythdbcon.h:77
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MSqlDatabase::MSqlDatabase
MSqlDatabase(QString name, QString driver="QMYSQL")
Definition: mythdbcon.cpp:77
MSqlQueryInfo
MSqlDatabase Info, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:93
MSqlQuery::kDedicatedConnection
@ kDedicatedConnection
Definition: mythdbcon.h:229
MDBManager::m_pool
QHash< QThread *, DBList > m_pool
Definition: mythdbcon.h:78
MSqlDatabase::m_driver
QString m_driver
Definition: mythdbcon.h:48
MSqlDatabase::Reconnect
bool Reconnect(void)
Definition: mythdbcon.cpp:255
MDBManager::m_inuse
QHash< QThread *, MSqlDatabase * > m_inuse
Definition: mythdbcon.h:80
MDBManager::m_staticPool
QHash< QThread *, DBList > m_staticPool
Definition: mythdbcon.h:89
MSqlQueryInfo::db
MSqlDatabase * db
Definition: mythdbcon.h:95
MSqlQuery::boundValues
QVariantList boundValues(void) const
Definition: mythdbcon.h:212
MSqlDatabase::m_db
QSqlDatabase m_db
Definition: mythdbcon.h:49
MSqlQuery::setForwardOnly
void setForwardOnly(bool f)
Definition: mythdbcon.h:219
MSqlQuery::kNormalConnection
@ kNormalConnection
Definition: mythdbcon.h:230
MSqlDatabase::~MSqlDatabase
~MSqlDatabase(void)
Definition: mythdbcon.cpp:102
MSqlQuery::isConnected
bool isConnected(void) const
Only updated once during object creation.
Definition: mythdbcon.h:138
MSqlAddMoreBindings
MBASE_PUBLIC void MSqlAddMoreBindings(MSqlBindings &output, MSqlBindings &addfrom)
Add the entries in addfrom to the map in output.
Definition: mythdbcon.cpp:960
MSqlQuery::m_lastPreparedQuery
QString m_lastPreparedQuery
Definition: mythdbcon.h:255
MSqlDatabase
QSqlDatabase wrapper, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:26
MSqlQuery::lastError
QSqlError lastError(void) const
Definition: mythdbcon.h:214
MSqlQueryInfo::returnConnection
bool returnConnection
Definition: mythdbcon.h:97
MSqlDatabase::m_lastDBKick
QDateTime m_lastDBKick
Definition: mythdbcon.h:50
MSqlQuery::executedQuery
QString executedQuery(void) const
Definition: mythdbcon.h:206
MSqlDatabase::GetConnectionName
QString GetConnectionName(void) const
Definition: mythdbcon.h:41
Reconnect
Definition: backendconnectionmanager.cpp:28
MSqlQuery::numRowsAffected
int numRowsAffected() const
Definition: mythdbcon.h:218
MSqlDatabase::m_name
QString m_name
Definition: mythdbcon.h:47
MSqlQueryInfo::qsqldb
QSqlDatabase qsqldb
Definition: mythdbcon.h:96
MSqlQuery::ConnectionReuse
ConnectionReuse
Definition: mythdbcon.h:227
output
#define output
Definition: synaesthesia.cpp:218
MSqlDatabase::SetDBParams
void SetDBParams(const DatabaseParams &params)
Definition: mythdbcon.h:35
MSqlDatabase::OpenDatabase
bool OpenDatabase(bool skipdb=false)
Definition: mythdbcon.cpp:125
MSqlQuery::at
int at(void) const
Definition: mythdbcon.h:222
mythdbparams.h
MSqlQuery::driver
const QSqlDriver * driver(void) const
Definition: mythdbcon.h:221
MSqlDatabase::m_dbparms
DatabaseParams m_dbparms
Definition: mythdbcon.h:51