MythTV  master
mythplugins/mytharchive/mytharchive/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 // MythTV
9 #include <mythcontext.h>
10 #include <mythdb.h>
11 
12 // mytharchive
13 #include "dbcheck.h"
14 
15 const QString currentDatabaseVersion = "1005";
16 
17 static bool UpdateDBVersionNumber(const QString &newnumber)
18 {
19 
20  if (!gCoreContext->SaveSettingOnHost("ArchiveDBSchemaVer",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 MythArchive 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("ArchiveDBSchemaVer");
71 
72  if (dbver == currentDatabaseVersion)
73  return true;
74 
75  if (dbver == "")
76  {
77  LOG(VB_GENERAL, LOG_INFO,
78  "Inserting MythArchive initial database information.");
79 
80  const QString updates[] =
81  {
82  "DROP TABLE IF EXISTS archiveitems;",
83 
84  "CREATE TABLE IF NOT EXISTS archiveitems ("
85  " intid INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,"
86  " type set ('Recording','Video','File'),"
87  " title VARCHAR(128),"
88  " subtitle VARCHAR(128),"
89  " description TEXT,"
90  " startdate VARCHAR(30),"
91  " starttime VARCHAR(30),"
92  " size INT UNSIGNED NOT NULL,"
93  " filename TEXT NOT NULL,"
94  " hascutlist BOOL NOT NULL DEFAULT 0,"
95  " cutlist TEXT,"
96  " INDEX (title)"
97  ");",
98  ""
99  };
100  if (!performActualUpdate(updates, "1000", dbver))
101  return false;
102  }
103 
104  if (dbver == "1000")
105  {
106  const QString updates[] =
107  {
108  "ALTER TABLE archiveitems MODIFY size BIGINT UNSIGNED NOT NULL;",
109  ""
110  };
111 
112  if (!performActualUpdate(updates, "1001", dbver))
113  return false;
114  }
115 
116 
117  if (dbver == "1001")
118  {
119  const QString updates[] =
120  {
121  QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
123  "ALTER TABLE archiveitems"
124  " MODIFY title varbinary(128) default NULL,"
125  " MODIFY subtitle varbinary(128) default NULL,"
126  " MODIFY description blob,"
127  " MODIFY startdate varbinary(30) default NULL,"
128  " MODIFY starttime varbinary(30) default NULL,"
129  " MODIFY filename blob,"
130  " MODIFY cutlist blob;",
131  ""
132  };
133 
134  if (!performActualUpdate(updates, "1002", dbver))
135  return false;
136  }
137 
138 
139  if (dbver == "1002")
140  {
141  const QString updates[] =
142  {
143  QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
145  "ALTER TABLE archiveitems"
146  " DEFAULT CHARACTER SET default,"
147  " MODIFY title varchar(128) CHARACTER SET utf8 default NULL,"
148  " MODIFY subtitle varchar(128) CHARACTER SET utf8 default NULL,"
149  " MODIFY description text CHARACTER SET utf8,"
150  " MODIFY startdate varchar(30) CHARACTER SET utf8 default NULL,"
151  " MODIFY starttime varchar(30) CHARACTER SET utf8 default NULL,"
152  " MODIFY filename text CHARACTER SET utf8 NOT NULL,"
153  " MODIFY cutlist text CHARACTER SET utf8;",
154  ""
155  };
156 
157  if (!performActualUpdate(updates, "1003", dbver))
158  return false;
159  }
160 
161  if (dbver == "1003")
162  {
163  const QString updates[] =
164  {
165  "ALTER TABLE `archiveitems` "
166  "ADD duration INT UNSIGNED NOT NULL DEFAULT 0, "
167  "ADD cutduration INT UNSIGNED NOT NULL DEFAULT 0, "
168  "ADD videowidth INT UNSIGNED NOT NULL DEFAULT 0, "
169  "ADD videoheight INT UNSIGNED NOT NULL DEFAULT 0, "
170  "ADD filecodec VARCHAR(50) NOT NULL DEFAULT '', "
171  "ADD videocodec VARCHAR(50) NOT NULL DEFAULT '', "
172  "ADD encoderprofile VARCHAR(50) NOT NULL DEFAULT 'NONE';",
173  ""
174  };
175 
176  if (!performActualUpdate(updates, "1004", dbver))
177  return false;
178  }
179 
180  if (dbver == "1004")
181  {
182  const QString updates[] =
183  {
184  "DELETE FROM keybindings "
185  " WHERE action = 'DELETEITEM' AND context = 'Archive';",
186  ""
187  };
188 
189  if (!performActualUpdate(updates, "1005", dbver))
190  return false;
191  }
192 
193  return true;
194 }
195 
QString dbName
database name
Definition: mythdbparams.h:26
static bool UpdateDBVersionNumber(const QString &newnumber)
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.
DatabaseParams GetDatabaseParams(void)
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:63
static bool performActualUpdate(const QString updates[], const QString &version, QString &dbver)
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)
const QString currentDatabaseVersion