No-Intro: Prevent database from being downgraded

This commit is contained in:
Vicki Pfau 2024-08-12 16:08:36 -07:00
parent 17cf2e6237
commit dfab801277
1 changed files with 24 additions and 11 deletions

View File

@ -72,6 +72,7 @@ bool NoIntroDBLoadClrMamePro(struct NoIntroDB* db, struct VFile* vf) {
sqlite3_stmt* gamedbTable = NULL; sqlite3_stmt* gamedbTable = NULL;
sqlite3_stmt* gamedbDrop = NULL; sqlite3_stmt* gamedbDrop = NULL;
sqlite3_stmt* gamedbSelect = NULL;
sqlite3_stmt* gameTable = NULL; sqlite3_stmt* gameTable = NULL;
sqlite3_stmt* romTable = NULL; sqlite3_stmt* romTable = NULL;
char* fieldName = NULL; char* fieldName = NULL;
@ -91,6 +92,11 @@ bool NoIntroDBLoadClrMamePro(struct NoIntroDB* db, struct VFile* vf) {
return false; return false;
} }
static const char selectGamedb[] = "SELECT * FROM gamedb WHERE name = ? AND version >= ?;";
if (sqlite3_prepare_v2(db->db, selectGamedb, -1, &gamedbSelect, NULL)) {
return false;
}
static const char insertGame[] = "INSERT INTO games (dbid, name) VALUES (?, ?);"; static const char insertGame[] = "INSERT INTO games (dbid, name) VALUES (?, ?);";
if (sqlite3_prepare_v2(db->db, insertGame, -1, &gameTable, NULL)) { if (sqlite3_prepare_v2(db->db, insertGame, -1, &gameTable, NULL)) {
return false; return false;
@ -161,18 +167,24 @@ bool NoIntroDBLoadClrMamePro(struct NoIntroDB* db, struct VFile* vf) {
break; break;
case ')': case ')':
if (currentDb < 0 && dbType && dbVersion) { if (currentDb < 0 && dbType && dbVersion) {
sqlite3_clear_bindings(gamedbDrop); sqlite3_clear_bindings(gamedbSelect);
sqlite3_reset(gamedbDrop); sqlite3_reset(gamedbSelect);
sqlite3_bind_text(gamedbDrop, 1, dbType, -1, SQLITE_TRANSIENT); sqlite3_bind_text(gamedbSelect, 1, dbType, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(gamedbDrop, 2, dbVersion, -1, SQLITE_TRANSIENT); sqlite3_bind_text(gamedbSelect, 2, dbVersion, -1, SQLITE_TRANSIENT);
sqlite3_step(gamedbDrop); if (sqlite3_step(gamedbSelect) != SQLITE_ROW) {
sqlite3_clear_bindings(gamedbDrop);
sqlite3_reset(gamedbDrop);
sqlite3_bind_text(gamedbDrop, 1, dbType, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(gamedbDrop, 2, dbVersion, -1, SQLITE_TRANSIENT);
sqlite3_step(gamedbDrop);
sqlite3_clear_bindings(gamedbTable); sqlite3_clear_bindings(gamedbTable);
sqlite3_reset(gamedbTable); sqlite3_reset(gamedbTable);
sqlite3_bind_text(gamedbTable, 1, dbType, -1, SQLITE_TRANSIENT); sqlite3_bind_text(gamedbTable, 1, dbType, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(gamedbTable, 2, dbVersion, -1, SQLITE_TRANSIENT); sqlite3_bind_text(gamedbTable, 2, dbVersion, -1, SQLITE_TRANSIENT);
if (sqlite3_step(gamedbTable) == SQLITE_DONE) { if (sqlite3_step(gamedbTable) == SQLITE_DONE) {
currentDb = sqlite3_last_insert_rowid(db->db); currentDb = sqlite3_last_insert_rowid(db->db);
}
} }
free((void*) dbType); free((void*) dbType);
free((void*) dbVersion); free((void*) dbVersion);
@ -272,6 +284,7 @@ bool NoIntroDBLoadClrMamePro(struct NoIntroDB* db, struct VFile* vf) {
sqlite3_finalize(gamedbTable); sqlite3_finalize(gamedbTable);
sqlite3_finalize(gamedbDrop); sqlite3_finalize(gamedbDrop);
sqlite3_finalize(gamedbSelect);
sqlite3_finalize(gameTable); sqlite3_finalize(gameTable);
sqlite3_finalize(romTable); sqlite3_finalize(romTable);