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