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 <QRegExp>
10 #include <QDateTime>
11 #include <QMutex>
12 #include <QList>
13 
14 #include "mythbaseexp.h"
15 #include "mythdbparams.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(const QString &name);
32  ~MSqlDatabase(void);
33 
34  bool OpenDatabase(bool skipdb = false);
35  void SetDBParams(const DatabaseParams &params) { m_dbparms = params; };
36 
37  private:
38  bool isOpen(void);
39  bool KickDatabase(void);
40  QString GetConnectionName(void) const { return m_name; }
41  QSqlDatabase db(void) const { return m_db; }
42  bool Reconnect(void);
43  void InitSessionVars(void);
44 
45  private:
46  QString m_name;
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  typedef QList<MSqlDatabase*> DBList;
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_inuse_count; // 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_static_pool;
87 };
88 
90 typedef struct _MSqlQueryInfo
91 {
93  QSqlDatabase qsqldb;
96 
98 typedef QMap<QString, QVariant> MSqlBindings;
99 
102 
104  MBASE_PUBLIC void MSqlEscapeAsAQuery(QString &query, MSqlBindings &bindings);
105 
125 class MBASE_PUBLIC MSqlQuery : private QSqlQuery
126 {
127  MBASE_PUBLIC friend void MSqlEscapeAsAQuery(QString&, MSqlBindings&);
128  public:
130  explicit MSqlQuery(const MSqlQueryInfo &qi);
132  ~MSqlQuery();
133 
135  bool isConnected(void) { 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, 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 
197  // Thunks that allow us to make QSqlQuery private
198  QVariant value(int i) const { return QSqlQuery::value(i); }
199  QString executedQuery(void) const { return QSqlQuery::executedQuery(); }
200  QMap<QString, QVariant> boundValues(void) const
201  { return QSqlQuery::boundValues(); }
202  QSqlError lastError(void) const { return QSqlQuery::lastError(); }
203  int size(void) const { return QSqlQuery::size();}
204  bool isActive(void) const { return QSqlQuery::isActive(); }
205  QSqlRecord record(void) const { return QSqlQuery::record(); }
206  int numRowsAffected() const { return QSqlQuery::numRowsAffected(); }
207  void setForwardOnly(bool f) { QSqlQuery::setForwardOnly(f); }
208  bool isNull(int field) const { return QSqlQuery::isNull(field); }
209  const QSqlDriver *driver(void) const { return QSqlQuery::driver(); }
210  int at(void) const { return QSqlQuery::at(); }
211 
213  static bool testDBConnection();
214 
215  typedef enum
216  {
219  } ConnectionReuse;
221  static MSqlQueryInfo InitCon(ConnectionReuse = kNormalConnection);
222 
224  static MSqlQueryInfo SchedCon();
225 
227  static MSqlQueryInfo ChannelCon();
228 
229  private:
230  // Only QSql::In is supported as a param type and only named params...
231  void bindValue(const QString&, const QVariant&, QSql::ParamType);
232  void bindValue(int, const QVariant&, QSql::ParamType);
233  void addBindValue(const QVariant&, QSql::ParamType = QSql::In);
234 
235  bool seekDebug(const char *type, bool result,
236  int where, bool relative) const;
237 
238  MSqlDatabase *m_db {nullptr};
239  bool m_isConnected {false};
240  bool m_returnConnection {false};
241  QString m_last_prepared_query; // holds a copy of the last prepared query
242 };
243 
244 #endif
void InitSessionVars(void)
Definition: mythdbcon.cpp:268
QSqlDatabase wrapper, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:26
QSqlDatabase m_db
Definition: mythdbcon.h:47
DatabaseParams m_dbparms
Definition: mythdbcon.h:49
struct exc__state * last
Definition: pxsup2dast.c:98
struct _MSqlQueryInfo MSqlQueryInfo
MSqlDatabase Info, used by MSqlQuery. Do not use directly.
bool OpenDatabase(bool skipdb=false)
Definition: mythdbcon.cpp:122
QHash< QThread *, MSqlDatabase * > m_inuse
Definition: mythdbcon.h:77
QList< MSqlDatabase * > DBList
Definition: mythdbcon.h:74
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
QHash< QThread *, DBList > m_static_pool
Definition: mythdbcon.h:86
int size(void) const
Definition: mythdbcon.h:203
bool isConnected(void)
Only updated once during object creation.
Definition: mythdbcon.h:135
MSqlDatabase * db
Definition: mythdbcon.h:92
QSqlDatabase db(void) const
Definition: mythdbcon.h:41
QHash< QThread *, int > m_inuse_count
Definition: mythdbcon.h:78
QString GetConnectionName(void) const
Definition: mythdbcon.h:40
MBASE_PUBLIC void MSqlAddMoreBindings(MSqlBindings &output, MSqlBindings &addfrom)
Add the entries in addfrom to the map in output.
Definition: mythdbcon.cpp:906
MBASE_PUBLIC void MSqlEscapeAsAQuery(QString &query, MSqlBindings &bindings)
Given a partial query string and a bindings object, escape the string.
Definition: mythdbcon.cpp:927
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
QVariant value(int i) const
Definition: mythdbcon.h:198
QMutex m_lock
Definition: mythdbcon.h:73
QString m_name
Definition: mythdbcon.h:46
~MSqlDatabase(void)
Definition: mythdbcon.cpp:99
QSqlError lastError(void) const
Definition: mythdbcon.h:202
bool returnConnection
Definition: mythdbcon.h:94
QSqlRecord record(void) const
Definition: mythdbcon.h:205
bool isActive(void) const
Definition: mythdbcon.h:204
bool Reconnect(void)
Definition: mythdbcon.cpp:253
const char * name
Definition: ParseText.cpp:328
QString m_last_prepared_query
Definition: mythdbcon.h:241
MSqlDatabase(const QString &name)
Definition: mythdbcon.cpp:76
QMap< QString, QVariant > boundValues(void) const
Definition: mythdbcon.h:200
MSqlDatabase Info, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:90
PictureAttribute next(PictureAttributeSupported supported, PictureAttribute attribute)
Structure containing the basic Database parameters.
Definition: mythdbparams.h:9
DB connection pool, used by MSqlQuery. Do not use directly.
Definition: mythdbcon.h:53
bool KickDatabase(void)
Definition: mythdbcon.cpp:243
QDateTime m_lastDBKick
Definition: mythdbcon.h:48
int numRowsAffected() const
Definition: mythdbcon.h:206
const QSqlDriver * driver(void) const
Definition: mythdbcon.h:209
QString executedQuery(void) const
Definition: mythdbcon.h:199
void SetDBParams(const DatabaseParams &params)
Definition: mythdbcon.h:35
MBASE_PUBLIC bool TestDatabase(const QString &dbHostName, const QString &dbUserName, QString dbPassword, QString dbName="mythconverg", int dbPort=3306)
Definition: mythdbcon.cpp:37
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:98
QHash< QThread *, DBList > m_pool
Definition: mythdbcon.h:75
bool isNull(int field) const
Definition: mythdbcon.h:208
QSqlDatabase qsqldb
Definition: mythdbcon.h:93
bool isOpen(void)
Definition: mythdbcon.cpp:112
int at(void) const
Definition: mythdbcon.h:210
#define output
void setForwardOnly(bool f)
Definition: mythdbcon.h:207