save game before summary (summary does UPDATE only, not INSERT)

This commit is contained in:
Eric House 2022-09-30 15:55:26 -07:00
parent 4cfc781cd7
commit 0ed20391c3
2 changed files with 13 additions and 14 deletions

View file

@ -207,20 +207,18 @@ writeBlobColumnData( const XP_U8* data, gsize len, XP_U16 strVersion, sqlite3* p
{ {
XP_LOGFF( "(col=%s)", column ); XP_LOGFF( "(col=%s)", column );
int result; int result;
char buf[256]; char query[256];
char* query;
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
XP_Bool newGame = -1 == curRow; XP_Bool newGame = -1 == curRow;
if ( newGame ) { /* new row; need to insert blob first */ if ( newGame ) { /* new row; need to insert blob first */
const char* fmt = "INSERT INTO games (%s) VALUES (?)"; const char* fmt = "INSERT INTO games (%s) VALUES (?)";
snprintf( buf, sizeof(buf), fmt, column ); snprintf( query, sizeof(query), fmt, column );
query = buf;
} else { } else {
const char* fmt = "UPDATE games SET %s=? where rowid=%lld"; const char* fmt = "UPDATE games SET %s=? where rowid=%lld";
snprintf( buf, sizeof(buf), fmt, column, curRow ); snprintf( query, sizeof(query), fmt, column, curRow );
query = buf;
} }
XP_LOGFF( "query: %s", query );
result = sqlite3_prepare_v2( pDb, query, -1, &stmt, NULL ); result = sqlite3_prepare_v2( pDb, query, -1, &stmt, NULL );
assertPrintResult( pDb, result, SQLITE_OK ); assertPrintResult( pDb, result, SQLITE_OK );
@ -287,7 +285,9 @@ gdb_write( XWStreamCtxt* stream, XWEnv XP_UNUSED(xwe), void* closure )
if ( newGame ) { /* new row; need to insert blob first */ if ( newGame ) { /* new row; need to insert blob first */
cGlobals->rowid = selRow; cGlobals->rowid = selRow;
XP_LOGFF( "new game at row %lld", selRow ); const CurGameInfo* gi = cGlobals->gi;
XP_U32 gameID = gi->gameID;
XP_LOGFF( "new game for id %d at row %lld", gameID, selRow );
} else { } else {
assert( selRow == cGlobals->rowid ); assert( selRow == cGlobals->rowid );
} }
@ -434,6 +434,7 @@ gdb_summarize( CommonGlobals* cGlobals )
for ( int ii = 0; !!pairs[ii]; ++ii ) { for ( int ii = 0; !!pairs[ii]; ++ii ) {
g_free( pairs[ii] ); g_free( pairs[ii] );
} }
XP_ASSERT( -1 != cGlobals->rowid );
gchar* query = g_strdup_printf( "UPDATE games SET %s WHERE rowid=%lld", gchar* query = g_strdup_printf( "UPDATE games SET %s WHERE rowid=%lld",
vals, cGlobals->rowid ); vals, cGlobals->rowid );
g_free( vals ); g_free( vals );

View file

@ -458,13 +458,8 @@ linuxSaveGame( CommonGlobals* cGlobals )
} }
if ( doSave ) { if ( doSave ) {
if ( !!pDb ) {
gdb_summarize( cGlobals );
}
XWStreamCtxt* outStream;
MemStreamCloseCallback onClose = !!pDb? gdb_write : writeToFile; MemStreamCloseCallback onClose = !!pDb? gdb_write : writeToFile;
outStream = XWStreamCtxt* outStream =
mem_stream_make_sized( MPPARM(cGlobals->util->mpool) mem_stream_make_sized( MPPARM(cGlobals->util->mpool)
cGlobals->params->vtMgr, cGlobals->params->vtMgr,
cGlobals->lastStreamSize, cGlobals->lastStreamSize,
@ -477,8 +472,11 @@ linuxSaveGame( CommonGlobals* cGlobals )
stream_destroy( outStream, NULL_XWE ); stream_destroy( outStream, NULL_XWE );
game_saveSucceeded( &cGlobals->game, NULL_XWE, cGlobals->curSaveToken ); game_saveSucceeded( &cGlobals->game, NULL_XWE, cGlobals->curSaveToken );
XP_LOGF( "%s: saved", __func__ );
if ( !!pDb ) {
gdb_summarize( cGlobals );
}
XP_LOGF( "%s: saved", __func__ );
} else { } else {
XP_LOGF( "%s: simulating save failure", __func__ ); XP_LOGF( "%s: simulating save failure", __func__ );
} }