MythTV master
dbcheckcommon.cpp
Go to the documentation of this file.
1/*
2 * Common functions used by all the *dbcheck.cpp files.
3 */
4
5#include "mythconfig.h"
6
7#include <cstdio>
8#include <iostream>
9
10#include <QString>
11#include <QSqlError>
12#include "mythdb.h"
13#include "mythdbcheck.h"
14#include "mythlogging.h"
15
16
17
28bool UpdateDBVersionNumber(const QString &component, const QString &versionkey,
29 const QString &newnumber, QString &dbver)
30{
31 // delete old schema version
33
34 QString thequery =
35 QString("DELETE FROM settings WHERE value='%1';").arg(versionkey);
36 query.prepare(thequery);
37
38 if (!query.exec())
39 {
40 QString msg =
41 QString("DB Error (Deleting old %1 DB version number): \n"
42 "Query was: %2 \nError was: %3 \nnew version: %4")
43 .arg(component,
44 thequery,
46 newnumber);
47 LOG(VB_GENERAL, LOG_ERR, msg);
48 return false;
49 }
50
51 // set new schema version
52 thequery = QString("INSERT INTO settings (value, data, hostname) "
53 "VALUES ('%1', %2, NULL);").arg(versionkey, newnumber);
54 query.prepare(thequery);
55
56 if (!query.exec())
57 {
58 QString msg =
59 QString("DB Error (Setting new %1 DB version number): \n"
60 "Query was: %2 \nError was: %3 \nnew version: %4")
61 .arg(component,
62 thequery,
64 newnumber);
65 LOG(VB_GENERAL, LOG_ERR, msg);
66 return false;
67 }
68
69 dbver = newnumber;
70
71 return true;
72}
73
80bool performUpdateSeries(const QString &component, const DBUpdates& updates)
81{
83
84 for (const auto & update : updates)
85 {
86 const char *thequery = update.c_str();
87 if ((strlen(thequery) != 0U) && !query.exec(thequery))
88 {
89 QString msg =
90 QString("DB Error (Performing %1 database upgrade): \n"
91 "Query was: %2 \nError was: %3")
92 .arg(component,
93 thequery,
95 LOG(VB_GENERAL, LOG_ERR, msg);
96 return false;
97 }
98 }
99
100 return true;
101}
102
114 const QString &component, const QString &versionkey,
115 const DBUpdates &updates, const QString &version, QString &dbver)
116{
118
119 LOG(VB_GENERAL, LOG_CRIT, QString("Upgrading to %1 schema version %2")
120 .arg(component, version));
121
122 if (!performUpdateSeries(component, updates))
123 return false;
124
125 if (!UpdateDBVersionNumber(component, versionkey, version, dbver))
126 return false;
127
128 return true;
129}
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:837
QSqlError lastError(void) const
Definition: mythdbcon.h:213
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:618
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:550
static QString DBErrorMessage(const QSqlError &err)
Definition: mythdb.cpp:231
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