MythTV master
dbcheckcommon.cpp
Go to the documentation of this file.
1/*
2 * Common functions used by all the *dbcheck.cpp files.
3 */
4#include <QString>
5#include <QSqlError>
6#include "mythdb.h"
7#include "mythdbcheck.h"
8#include "mythlogging.h"
9
10
11
22bool UpdateDBVersionNumber(const QString &component, const QString &versionkey,
23 const QString &newnumber, QString &dbver)
24{
25 // delete old schema version
27
28 QString thequery =
29 QString("DELETE FROM settings WHERE value='%1';").arg(versionkey);
30 query.prepare(thequery);
31
32 if (!query.exec())
33 {
34 QString msg =
35 QString("DB Error (Deleting old %1 DB version number): \n"
36 "Query was: %2 \nError was: %3 \nnew version: %4")
37 .arg(component,
38 thequery,
40 newnumber);
41 LOG(VB_GENERAL, LOG_ERR, msg);
42 return false;
43 }
44
45 // set new schema version
46 thequery = QString("INSERT INTO settings (value, data, hostname) "
47 "VALUES ('%1', %2, NULL);").arg(versionkey, newnumber);
48 query.prepare(thequery);
49
50 if (!query.exec())
51 {
52 QString msg =
53 QString("DB Error (Setting new %1 DB version number): \n"
54 "Query was: %2 \nError was: %3 \nnew version: %4")
55 .arg(component,
56 thequery,
58 newnumber);
59 LOG(VB_GENERAL, LOG_ERR, msg);
60 return false;
61 }
62
63 dbver = newnumber;
64
65 return true;
66}
67
74bool performUpdateSeries(const QString &component, const DBUpdates& updates)
75{
77
78 for (const auto & update : updates)
79 {
80 const char *thequery = update.c_str();
81 if ((strlen(thequery) != 0U) && !query.exec(thequery))
82 {
83 QString msg =
84 QString("DB Error (Performing %1 database upgrade): \n"
85 "Query was: %2 \nError was: %3")
86 .arg(component,
87 thequery,
89 LOG(VB_GENERAL, LOG_ERR, msg);
90 return false;
91 }
92 }
93
94 return true;
95}
96
108 const QString &component, const QString &versionkey,
109 const DBUpdates &updates, const QString &version, QString &dbver)
110{
112
113 LOG(VB_GENERAL, LOG_CRIT, QString("Upgrading to %1 schema version %2")
114 .arg(component, version));
115
116 if (!performUpdateSeries(component, updates))
117 return false;
118
119 if (!UpdateDBVersionNumber(component, versionkey, version, dbver))
120 return false;
121
122 return true;
123}
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:838
QSqlError lastError(void) const
Definition: mythdbcon.h:213
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:619
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:551
static QString DBErrorMessage(const QSqlError &err)
Definition: mythdb.cpp:230
bool performActualUpdate(const QString &component, const QString &versionkey, const DBUpdates &updates, const QString &version, QString &dbver)
bool UpdateDBVersionNumber(const QString &component, const QString &versionkey, const QString &newnumber, QString &dbver)
Updates the schema version stored in the database.
bool performUpdateSeries(const QString &component, const DBUpdates &updates)
std::vector< std::string > DBUpdates
Definition: mythdbcheck.h:9
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
string version
Definition: giantbomb.py:185