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 using namespace std;
10 
11 #include <QString>
12 #include <QSqlError>
13 #include "mythdb.h"
14 #include "mythdbcheck.h"
15 #include "mythlogging.h"
16 
17 
18 
29 bool UpdateDBVersionNumber(const QString &component, const QString &versionkey,
30  const QString &newnumber, QString &dbver)
31 {
32  // delete old schema version
34 
35  QString thequery =
36  QString("DELETE FROM settings WHERE value='%1';").arg(versionkey);
37  query.prepare(thequery);
38 
39  if (!query.exec())
40  {
41  QString msg =
42  QString("DB Error (Deleting old %1 DB version number): \n"
43  "Query was: %2 \nError was: %3 \nnew version: %4")
44  .arg(component)
45  .arg(thequery)
47  .arg(newnumber);
48  LOG(VB_GENERAL, LOG_ERR, msg);
49  return false;
50  }
51 
52  // set new schema version
53  thequery = QString("INSERT INTO settings (value, data, hostname) "
54  "VALUES ('%1', %2, NULL);").arg(versionkey).arg(newnumber);
55  query.prepare(thequery);
56 
57  if (!query.exec())
58  {
59  QString msg =
60  QString("DB Error (Setting new %1 DB version number): \n"
61  "Query was: %2 \nError was: %3 \nnew version: %4")
62  .arg(component)
63  .arg(thequery)
65  .arg(newnumber);
66  LOG(VB_GENERAL, LOG_ERR, msg);
67  return false;
68  }
69 
70  dbver = newnumber;
71 
72  return true;
73 }
74 
81 bool performUpdateSeries(const QString &component, const DBUpdates& updates)
82 {
84 
85  for (const auto & update : updates)
86  {
87  const char *thequery = update.c_str();
88  if ((strlen(thequery) != 0U) && !query.exec(thequery))
89  {
90  QString msg =
91  QString("DB Error (Performing %1 database upgrade): \n"
92  "Query was: %2 \nError was: %3")
93  .arg(component)
94  .arg(thequery)
96  LOG(VB_GENERAL, LOG_ERR, msg);
97  return false;
98  }
99  }
100 
101  return true;
102 }
103 
115  const QString &component, const QString &versionkey,
116  const DBUpdates &updates, const QString &version, QString &dbver)
117 {
119 
120  LOG(VB_GENERAL, LOG_CRIT, QString("Upgrading to %1 schema version %2")
121  .arg(component).arg(version));
122 
123  if (!performUpdateSeries(component, updates))
124  return false;
125 
126  if (!UpdateDBVersionNumber(component, versionkey, version, dbver))
127  return false;
128 
129  return true;
130 }
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
mythdb.h
MythDB::DBErrorMessage
static QString DBErrorMessage(const QSqlError &err)
Definition: mythdb.cpp:184
arg
arg(title).arg(filename).arg(doDelete))
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
mythlogging.h
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
mythdbcheck.h
performUpdateSeries
bool performUpdateSeries(const QString &component, const DBUpdates &updates)
Definition: dbcheckcommon.cpp:81
UpdateDBVersionNumber
bool UpdateDBVersionNumber(const QString &component, const QString &versionkey, const QString &newnumber, QString &dbver)
Updates the schema version stored in the database.
Definition: dbcheckcommon.cpp:29
MSqlQuery::lastError
QSqlError lastError(void) const
Definition: mythdbcon.h:202
performActualUpdate
bool performActualUpdate(const QString &component, const QString &versionkey, const DBUpdates &updates, const QString &version, QString &dbver)
Definition: dbcheckcommon.cpp:114
DBUpdates
std::vector< std::string > DBUpdates
Definition: mythdbcheck.h:9
query
MSqlQuery query(MSqlQuery::InitCon())
nv_python_libs.bbciplayer.bbciplayer_api.version
string version
Definition: bbciplayer_api.py:81
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808