MythTV  master
mythplugins/mythgallery/mythgallery/dbcheck.cpp
Go to the documentation of this file.
1 #include <iostream>
2 using namespace std;
3 
4 // qt
5 #include <QString>
6 #include <QSqlError>
7 
8 // myth
9 #include "mythcontext.h"
10 #include "mythdb.h"
11 
12 // mythgallery
13 #include "dbcheck.h"
14 
15 const QString currentDatabaseVersion = "1003";
16 
17 static bool UpdateDBVersionNumber(const QString &newnumber)
18 {
19 
20  if (!gCoreContext->SaveSettingOnHost("GalleryDBSchemaVer",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 MythGallery 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("GalleryDBSchemaVer");
71 
72  if (dbver == currentDatabaseVersion)
73  return true;
74 
75  if (dbver == "")
76  {
77  LOG(VB_GENERAL, LOG_NOTICE,
78  "Inserting MythGallery initial database information.");
79 
80  const QString updates[] = {
81 "CREATE TABLE IF NOT EXISTS gallerymetadata ("
82 " image VARCHAR(255) NOT NULL PRIMARY KEY,"
83 " angle INTEGER NOT NULL"
84 ");",
85 "INSERT INTO settings VALUES ('GalleryDBSchemaVer', 1000, NULL);",
86 ""
87 };
88  if (!performActualUpdate(updates, "1000", dbver))
89  return false;
90  }
91 
92 
93 
94  if (dbver == "1000")
95  {
96  const QString updates[] = {
97 QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
99 "ALTER TABLE gallerymetadata"
100 " MODIFY image varbinary(255) NOT NULL;",
101 ""
102 };
103 
104  if (!performActualUpdate(updates, "1001", dbver))
105  return false;
106  }
107 
108 
109  if (dbver == "1001")
110  {
111  const QString updates[] = {
112 QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
114 "ALTER TABLE gallerymetadata"
115 " DEFAULT CHARACTER SET default,"
116 " MODIFY image varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL;",
117 ""
118 };
119 
120  if (!performActualUpdate(updates, "1002", dbver))
121  return false;
122  }
123 
124  if (dbver == "1002")
125  {
126  const QString updates[] = {
127 "DELETE FROM keybindings "
128 " WHERE action = 'DELETE' AND context = 'Gallery';",
129 ""
130 };
131 
132  if (!performActualUpdate(updates, "1003", dbver))
133  return false;
134  }
135 
136  return true;
137 }
QString dbName
database name
Definition: mythdbparams.h:26
static bool performActualUpdate(const QString updates[], const QString &version, QString &dbver)
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
const QString currentDatabaseVersion
DatabaseParams GetDatabaseParams(void)
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:63
QSqlError lastError(void) const
Definition: mythdbcon.h:202
QString GetSetting(const QString &key, const QString &defaultval="")
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
#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)
static bool UpdateDBVersionNumber(const QString &newnumber)