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