MythTV  master
mythplugins/mythnews/mythnews/dbcheck.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <iostream>
3 
4 // QT headers
5 #include <QString>
6 #include <QSqlError>
7 
8 // Myth headers
9 #include <mythcontext.h>
10 #include <mythdb.h>
11 
12 // MythNews headers
13 #include "dbcheck.h"
14 
15 const QString currentDatabaseVersion = "1001";
16 
17 static bool UpdateDBVersionNumber(const QString &newnumber)
18 {
19 
20  if (!gCoreContext->SaveSettingOnHost("NewsDBSchemaVer", newnumber, nullptr))
21  {
22  LOG(VB_GENERAL, LOG_ERR,
23  QString("DB Error (Setting new DB version number): %1\n")
24  .arg(newnumber));
25 
26  return false;
27  }
28 
29  return true;
30 }
31 
32 static bool performActualUpdate(const QString updates[], const QString& version,
33  QString &dbver)
34 {
36 
37  LOG(VB_GENERAL, LOG_NOTICE,
38  "Upgrading to MythNews schema version " + version);
39 
40  int counter = 0;
41  QString thequery = updates[counter];
42 
43  while (thequery != "")
44  {
45  if (!query.exec(thequery))
46  {
47  QString msg =
48  QString("DB Error (Performing database upgrade): \n"
49  "Query was: %1 \nError was: %2 \nnew version: %3")
50  .arg(thequery)
51  .arg(MythDB::DBErrorMessage(query.lastError()))
52  .arg(version);
53  LOG(VB_GENERAL, LOG_ERR, msg);
54  return false;
55  }
56 
57  counter++;
58  thequery = updates[counter];
59  }
60 
62  return false;
63 
64  dbver = version;
65  return true;
66 }
67 
69 {
70  QString dbver = gCoreContext->GetSetting("NewsDBSchemaVer");
71 
72  if (dbver == currentDatabaseVersion)
73  return true;
74 
75  if (dbver.isEmpty())
76  {
77  LOG(VB_GENERAL, LOG_NOTICE,
78  "Inserting MythNews initial database information.");
79 
80  const QString updates[] =
81  {
82  "CREATE TABLE IF NOT EXISTS newssites "
83  "( name VARCHAR(100) NOT NULL PRIMARY KEY,"
84  " category VARCHAR(255) NOT NULL,"
85  " url VARCHAR(255) NOT NULL,"
86  " ico VARCHAR(255),"
87  " updated INT UNSIGNED);",
88  ""
89  };
90  if (!performActualUpdate(updates, "1000", dbver))
91  return false;
92  }
93 
94  if (dbver == "1000")
95  {
96  const QString updates[] =
97  {
98  "ALTER TABLE `newssites` ADD `podcast` BOOL NOT NULL DEFAULT '0';",
99  ""
100  };
101 
102  if (!performActualUpdate(updates, "1001", dbver))
103  return false;
104  }
105 
106  return true;
107 }
108 
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
static bool UpdateDBVersionNumber(const QString &newnumber)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
const QString currentDatabaseVersion
QSqlError lastError(void) const
Definition: mythdbcon.h:202
QString GetSetting(const QString &key, const QString &defaultval="")
static bool performActualUpdate(const QString updates[], const QString &version, QString &dbver)
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
bool UpgradeNewsDatabaseSchema(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static QString DBErrorMessage(const QSqlError &err)
Definition: mythdb.cpp:184
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
bool SaveSettingOnHost(const QString &key, const QString &newValue, const QString &host)