MythTV  master
gamedbcheck.cpp
Go to the documentation of this file.
1 #include <iostream>
2 using namespace std;
3 
4 #include <QString>
5 #include <QSqlError>
6 
7 #include <mythcontext.h>
8 #include <mythdb.h>
9 #include <mythdbcheck.h>
10 
11 #include "gamedbcheck.h"
12 #include "gamesettings.h"
13 
14 const QString currentDatabaseVersion = "1019";
15 const QString MythGameVersionName = "GameDBSchemaVer";
16 
17 static bool InitializeDatabase(void)
18 {
19  LOG(VB_GENERAL, LOG_NOTICE,
20  "Inserting MythGame initial database information.");
21 
22  DBUpdates updates {
23 "CREATE TABLE gamemetadata ("
24 " `system` varchar(128) NOT NULL default '',"
25 " romname varchar(128) NOT NULL default '',"
26 " gamename varchar(128) NOT NULL default '',"
27 " genre varchar(128) NOT NULL default '',"
28 " year varchar(10) NOT NULL default '',"
29 " publisher varchar(128) NOT NULL default '',"
30 " favorite tinyint(1) default NULL,"
31 " rompath varchar(255) NOT NULL default '',"
32 " gametype varchar(64) NOT NULL default '',"
33 " diskcount tinyint(1) NOT NULL default '1',"
34 " country varchar(128) NOT NULL default '',"
35 " crc_value varchar(64) NOT NULL default '',"
36 " display tinyint(1) NOT NULL default '1',"
37 " version varchar(64) NOT NULL default '',"
38 " KEY `system` (`system`),"
39 " KEY year (year),"
40 " KEY romname (romname),"
41 " KEY gamename (gamename),"
42 " KEY genre (genre)"
43 ");",
44 "CREATE TABLE gameplayers ("
45 " gameplayerid int(10) unsigned NOT NULL auto_increment,"
46 " playername varchar(64) NOT NULL default '',"
47 " workingpath varchar(255) NOT NULL default '',"
48 " rompath varchar(255) NOT NULL default '',"
49 " screenshots varchar(255) NOT NULL default '',"
50 " commandline text NOT NULL,"
51 " gametype varchar(64) NOT NULL default '',"
52 " extensions varchar(128) NOT NULL default '',"
53 " spandisks tinyint(1) NOT NULL default '0',"
54 " PRIMARY KEY (gameplayerid),"
55 " UNIQUE KEY playername (playername)"
56 ");",
57 "CREATE TABLE romdb ("
58 " crc varchar(64) NOT NULL default '',"
59 " name varchar(128) NOT NULL default '',"
60 " description varchar(128) NOT NULL default '',"
61 " category varchar(128) NOT NULL default '',"
62 " year varchar(10) NOT NULL default '',"
63 " manufacturer varchar(128) NOT NULL default '',"
64 " country varchar(128) NOT NULL default '',"
65 " publisher varchar(128) NOT NULL default '',"
66 " platform varchar(64) NOT NULL default '',"
67 " filesize int(12) default NULL,"
68 " flags varchar(64) NOT NULL default '',"
69 " version varchar(64) NOT NULL default '',"
70 " KEY crc (crc),"
71 " KEY year (year),"
72 " KEY category (category),"
73 " KEY name (name),"
74 " KEY description (description),"
75 " KEY platform (platform)"
76 ");"
77 };
78  QString dbver = "";
79  return performActualUpdate("MythGame", MythGameVersionName,
80  updates, "1011", dbver);
81 }
82 
84 {
85  QString dbver = gCoreContext->GetSetting("GameDBSchemaVer");
87 
88  if (dbver == currentDatabaseVersion)
89  return true;
90 
91  if (dbver.isEmpty())
92  {
93  if (!InitializeDatabase())
94  return false;
95  dbver = "1011";
96  }
97 
98  if (dbver == "1000")
99  {
100  DBUpdates updates {
101 "ALTER TABLE gamemetadata ADD COLUMN favorite BOOL NULL;"
102 };
103  if (!performActualUpdate("MythGame", MythGameVersionName,
104  updates, "1001", dbver))
105  return false;
106  }
107 
108  if ((((dbver == "1004")
109  || (dbver == "1003"))
110  || (dbver == "1002"))
111  || (dbver == "1001"))
112  {
113  DBUpdates updates {
114 
115 "CREATE TABLE gameplayers ("
116 " gameplayerid int(10) unsigned NOT NULL auto_increment,"
117 " playername varchar(64) NOT NULL default '',"
118 " workingpath varchar(255) NOT NULL default '',"
119 " rompath varchar(255) NOT NULL default '',"
120 " screenshots varchar(255) NOT NULL default '',"
121 " commandline varchar(255) NOT NULL default '',"
122 " gametype varchar(64) NOT NULL default '',"
123 " extensions varchar(128) NOT NULL default '',"
124 " PRIMARY KEY (gameplayerid),"
125 " UNIQUE KEY playername (playername)"
126 ");",
127 "ALTER TABLE gamemetadata ADD COLUMN rompath varchar(255) NOT NULL default ''; ",
128 "ALTER TABLE gamemetadata ADD COLUMN gametype varchar(64) NOT NULL default ''; "
129 };
130  if (!performActualUpdate("MythGame", MythGameVersionName,
131  updates, "1005", dbver))
132  return false;
133  }
134 
135  if (dbver == "1005")
136  {
137  DBUpdates updates {
138 "ALTER TABLE gameplayers ADD COLUMN spandisks tinyint(1) NOT NULL default 0; ",
139 "ALTER TABLE gamemetadata ADD COLUMN diskcount tinyint(1) NOT NULL default 1; "
140 };
141  if (!performActualUpdate("MythGame", MythGameVersionName,
142  updates, "1006", dbver))
143  return false;
144  }
145 
146  if (dbver == "1006")
147  {
148 
149  if (!gCoreContext->GetSetting("GameAllTreeLevels").isEmpty())
150  {
151  if (!query.exec("UPDATE settings SET data = 'system gamename' "
152  "WHERE value = 'GameAllTreeLevels'; "))
153  MythDB::DBError("update GameAllTreeLevels", query);
154  }
155 
156  DBUpdates updates = {
157 "ALTER TABLE gamemetadata ADD COLUMN country varchar(128) NOT NULL default ''; ",
158 "ALTER TABLE gamemetadata ADD COLUMN crc_value varchar(64) NOT NULL default ''; ",
159 "ALTER TABLE gamemetadata ADD COLUMN display tinyint(1) NOT NULL default 1; "
160 };
161 
162  if (!performActualUpdate("MythGame", MythGameVersionName,
163  updates, "1007", dbver))
164  return false;
165  }
166 
167  if (dbver == "1007")
168  {
169  DBUpdates updates {
170 "ALTER TABLE gameplayers MODIFY commandline TEXT NOT NULL default ''; "
171 };
172 
173  if (!performActualUpdate("MythGame", MythGameVersionName,
174  updates, "1008", dbver))
175  return false;
176  }
177 
178  if (dbver == "1008")
179  {
180  DBUpdates updates {
181 "CREATE TABLE romdb ("
182 " crc varchar(64) NOT NULL default '',"
183 " name varchar(128) NOT NULL default '',"
184 " description varchar(128) NOT NULL default '',"
185 " category varchar(128) NOT NULL default '',"
186 " year varchar(10) NOT NULL default '',"
187 " manufacturer varchar(128) NOT NULL default '',"
188 " country varchar(128) NOT NULL default '',"
189 " publisher varchar(128) NOT NULL default '',"
190 " platform varchar(64) NOT NULL default '',"
191 " filesize int(12) default NULL,"
192 " flags varchar(64) NOT NULL default '',"
193 " version varchar(64) NOT NULL default '',"
194 " KEY crc (crc),"
195 " KEY year (year),"
196 " KEY category (category),"
197 " KEY name (name),"
198 " KEY description (description),"
199 " KEY platform (platform)"
200 ");"
201 };
202 
203  if (!performActualUpdate("MythGame", MythGameVersionName,
204  updates, "1009", dbver))
205  return false;
206  }
207 
208  if (dbver == "1009")
209  {
210  DBUpdates updates {
211 "ALTER TABLE gamemetadata MODIFY year varchar(10) not null default '';"
212 };
213 
214  if (!performActualUpdate("MythGame", MythGameVersionName,
215  updates, "1010", dbver))
216  return false;
217  }
218 
219  if (dbver == "1010")
220  {
221  DBUpdates updates {
222 
223 "ALTER TABLE gamemetadata ADD COLUMN version varchar(64) NOT NULL default '';",
224 "ALTER TABLE gamemetadata ADD COLUMN publisher varchar(128) NOT NULL default '';"
225 };
226 
227  if (!performActualUpdate("MythGame", MythGameVersionName,
228  updates, "1011", dbver))
229  return false;
230  }
231 
232 
233  if (dbver == "1011")
234  {
235  DBUpdates updates {
236 "ALTER TABLE romdb ADD COLUMN binfile varchar(64) NOT NULL default ''; "
237 };
238 
239  if (!performActualUpdate("MythGame", MythGameVersionName,
240  updates, "1012", dbver))
241  return false;
242  }
243 
244 
245  if (dbver == "1012")
246  {
247  DBUpdates updates {
248 qPrintable(QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
250 "ALTER TABLE gamemetadata"
251 " MODIFY `system` varbinary(128) NOT NULL default '',"
252 " MODIFY romname varbinary(128) NOT NULL default '',"
253 " MODIFY gamename varbinary(128) NOT NULL default '',"
254 " MODIFY genre varbinary(128) NOT NULL default '',"
255 " MODIFY year varbinary(10) NOT NULL default '',"
256 " MODIFY publisher varbinary(128) NOT NULL default '',"
257 " MODIFY rompath varbinary(255) NOT NULL default '',"
258 " MODIFY gametype varbinary(64) NOT NULL default '',"
259 " MODIFY country varbinary(128) NOT NULL default '',"
260 " MODIFY crc_value varbinary(64) NOT NULL default '',"
261 " MODIFY version varbinary(64) NOT NULL default '';",
262 "ALTER TABLE gameplayers"
263 " MODIFY playername varbinary(64) NOT NULL default '',"
264 " MODIFY workingpath varbinary(255) NOT NULL default '',"
265 " MODIFY rompath varbinary(255) NOT NULL default '',"
266 " MODIFY screenshots varbinary(255) NOT NULL default '',"
267 " MODIFY commandline blob NOT NULL,"
268 " MODIFY gametype varbinary(64) NOT NULL default '',"
269 " MODIFY extensions varbinary(128) NOT NULL default '';",
270 "ALTER TABLE romdb"
271 " MODIFY crc varbinary(64) NOT NULL default '',"
272 " MODIFY name varbinary(128) NOT NULL default '',"
273 " MODIFY description varbinary(128) NOT NULL default '',"
274 " MODIFY category varbinary(128) NOT NULL default '',"
275 " MODIFY year varbinary(10) NOT NULL default '',"
276 " MODIFY manufacturer varbinary(128) NOT NULL default '',"
277 " MODIFY country varbinary(128) NOT NULL default '',"
278 " MODIFY publisher varbinary(128) NOT NULL default '',"
279 " MODIFY platform varbinary(64) NOT NULL default '',"
280 " MODIFY flags varbinary(64) NOT NULL default '',"
281 " MODIFY version varbinary(64) NOT NULL default '',"
282 " MODIFY binfile varbinary(64) NOT NULL default '';"
283 };
284 
285  if (!performActualUpdate("MythGame", MythGameVersionName,
286  updates, "1013", dbver))
287  return false;
288  }
289 
290 
291  if (dbver == "1013")
292  {
293  DBUpdates updates {
294 qPrintable(QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
296 "ALTER TABLE gamemetadata"
297 " DEFAULT CHARACTER SET default,"
298 " MODIFY `system` varchar(128) CHARACTER SET utf8 NOT NULL default '',"
299 " MODIFY romname varchar(128) CHARACTER SET utf8 NOT NULL default '',"
300 " MODIFY gamename varchar(128) CHARACTER SET utf8 NOT NULL default '',"
301 " MODIFY genre varchar(128) CHARACTER SET utf8 NOT NULL default '',"
302 " MODIFY year varchar(10) CHARACTER SET utf8 NOT NULL default '',"
303 " MODIFY publisher varchar(128) CHARACTER SET utf8 NOT NULL default '',"
304 " MODIFY rompath varchar(255) CHARACTER SET utf8 NOT NULL default '',"
305 " MODIFY gametype varchar(64) CHARACTER SET utf8 NOT NULL default '',"
306 " MODIFY country varchar(128) CHARACTER SET utf8 NOT NULL default '',"
307 " MODIFY crc_value varchar(64) CHARACTER SET utf8 NOT NULL default '',"
308 " MODIFY version varchar(64) CHARACTER SET utf8 NOT NULL default '';",
309 "ALTER TABLE gameplayers"
310 " DEFAULT CHARACTER SET default,"
311 " MODIFY playername varchar(64) CHARACTER SET utf8 NOT NULL default '',"
312 " MODIFY workingpath varchar(255) CHARACTER SET utf8 NOT NULL default '',"
313 " MODIFY rompath varchar(255) CHARACTER SET utf8 NOT NULL default '',"
314 " MODIFY screenshots varchar(255) CHARACTER SET utf8 NOT NULL default '',"
315 " MODIFY commandline text CHARACTER SET utf8 NOT NULL,"
316 " MODIFY gametype varchar(64) CHARACTER SET utf8 NOT NULL default '',"
317 " MODIFY extensions varchar(128) CHARACTER SET utf8 NOT NULL default '';",
318 "ALTER TABLE romdb"
319 " DEFAULT CHARACTER SET default,"
320 " MODIFY crc varchar(64) CHARACTER SET utf8 NOT NULL default '',"
321 " MODIFY name varchar(128) CHARACTER SET utf8 NOT NULL default '',"
322 " MODIFY description varchar(128) CHARACTER SET utf8 NOT NULL default '',"
323 " MODIFY category varchar(128) CHARACTER SET utf8 NOT NULL default '',"
324 " MODIFY year varchar(10) CHARACTER SET utf8 NOT NULL default '',"
325 " MODIFY manufacturer varchar(128) CHARACTER SET utf8 NOT NULL default '',"
326 " MODIFY country varchar(128) CHARACTER SET utf8 NOT NULL default '',"
327 " MODIFY publisher varchar(128) CHARACTER SET utf8 NOT NULL default '',"
328 " MODIFY platform varchar(64) CHARACTER SET utf8 NOT NULL default '',"
329 " MODIFY flags varchar(64) CHARACTER SET utf8 NOT NULL default '',"
330 " MODIFY version varchar(64) CHARACTER SET utf8 NOT NULL default '',"
331 " MODIFY binfile varchar(64) CHARACTER SET utf8 NOT NULL default '';"
332 };
333 
334  if (!performActualUpdate("MythGame", MythGameVersionName,
335  updates, "1014", dbver))
336  return false;
337  }
338 
339  if (dbver == "1014")
340  {
341  DBUpdates updates {
342 
343 "ALTER TABLE gamemetadata ADD fanart VARCHAR(255) NOT NULL AFTER rompath,"
344 "ADD boxart VARCHAR( 255 ) NOT NULL AFTER fanart;"
345 };
346 
347  if (!performActualUpdate("MythGame", MythGameVersionName,
348  updates, "1015", dbver))
349  return false;
350  }
351 
352  if (dbver == "1015")
353  {
354  DBUpdates updates {
355 
356 "ALTER TABLE gamemetadata ADD screenshot VARCHAR(255) NOT NULL AFTER rompath,"
357 "ADD plot TEXT NOT NULL AFTER fanart;"
358 };
359 
360  if (!performActualUpdate("MythGame", MythGameVersionName,
361  updates, "1016", dbver))
362  return false;
363  }
364 
365  if (dbver == "1016")
366  {
367  DBUpdates updates {
368 
369 "ALTER TABLE gamemetadata ADD inetref TEXT AFTER crc_value;"
370 };
371 
372  if (!performActualUpdate("MythGame", MythGameVersionName,
373  updates, "1017", dbver))
374  return false;
375  }
376 
377  if (dbver == "1017")
378  {
379  DBUpdates updates {
380 
381 "ALTER TABLE gamemetadata ADD intid int(11) NOT NULL AUTO_INCREMENT "
382 "PRIMARY KEY FIRST;"
383 };
384 
385  if (!performActualUpdate("MythGame", MythGameVersionName,
386  updates, "1018", dbver))
387  return false;
388  }
389 
390  if (dbver == "1018")
391  {
392  DBUpdates updates {
393 "ALTER TABLE romdb MODIFY description varchar(192) CHARACTER SET utf8 NOT NULL default '';",
394 "ALTER TABLE romdb MODIFY binfile varchar(128) CHARACTER SET utf8 NOT NULL default '';",
395 "ALTER TABLE romdb MODIFY filesize int(12) unsigned default NULL;"
396 };
397 
398  if (!performActualUpdate("MythGame", MythGameVersionName,
399  updates, "1019", dbver))
400  return false;
401  }
402 
403  return true;
404 }
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
mythdb.h
MythContext::GetDatabaseParams
DatabaseParams GetDatabaseParams(void)
Definition: mythcontext.cpp:1679
InitializeDatabase
static bool InitializeDatabase(void)
Definition: gamedbcheck.cpp:17
gamedbcheck.h
UpgradeGameDatabaseSchema
bool UpgradeGameDatabaseSchema(void)
Definition: gamedbcheck.cpp:83
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
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
mythdbcheck.h
DatabaseParams::m_dbName
QString m_dbName
database name
Definition: mythdbparams.h:26
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
MythGameVersionName
const QString MythGameVersionName
Definition: gamedbcheck.cpp:15
gamesettings.h
performActualUpdate
bool performActualUpdate(const QString &component, const QString &versionkey, const DBUpdates &updates, const QString &version, QString &dbver)
Definition: dbcheckcommon.cpp:114
currentDatabaseVersion
const QString currentDatabaseVersion
Definition: gamedbcheck.cpp:14
mythcontext.h
DBUpdates
std::vector< std::string > DBUpdates
Definition: mythdbcheck.h:9
query
MSqlQuery query(MSqlQuery::InitCon())
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:61
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:916