factor dbmgr code; add and call method to zero CIDs on boot.

This commit is contained in:
Andy2 2010-09-15 01:14:28 -07:00
parent 821e4d9c39
commit 3b5b5f0241
3 changed files with 25 additions and 8 deletions

View file

@ -25,6 +25,7 @@
#include "xwrelay_priv.h"
#define DB_NAME "xwgames"
#define TABLE_NAME "games"
static DBMgr* s_instance = NULL;
@ -73,20 +74,18 @@ void
DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
int langCode, int nPlayersT )
{
#if 1
if ( !cookie ) cookie = "";
if ( !connName ) connName = "";
const char* fmt = "INSERT INTO games "
"(cid, cookie, connName, nTotal, nHere, lang, ctime) "
const char* fmt = "INSERT INTO " DB_NAME
" (cid, cookie, connName, nTotal, nHere, lang, ctime) "
"VALUES( %d, '%s', '%s', %d, %d, %d, 'now' )";
char buf[256];
snprintf( buf, sizeof(buf), fmt, cid/*m_nextCID++*/, cookie, connName,
nPlayersT, 0, langCode );
logf( XW_LOGINFO, "passing %s", buf );
PGresult* result = PQexec( m_pgconn, buf );
PQclear( result );
execSql( buf );
#else
const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) "
"VALUES( $1, $2, $3, $4, $5 )";
@ -106,8 +105,8 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
NULL, /*const int *paramLengths,*/
NULL, /*const int *paramFormats,*/
0 /*int resultFormat*/ );
#endif
logf( XW_LOGINFO, "PQexecParams=>%d", result );
#endif
}
CookieID
@ -115,7 +114,7 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH )
{
CookieID cid = 0;
const char* fmt = "SELECT cid from games where cookie = '%s' "
const char* fmt = "SELECT cid from " DB_NAME " where cookie = '%s' "
"AND lang = %d "
"AND nTotal = %d "
"AND %d <= nTotal-nHere "
@ -138,14 +137,27 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH )
void
DBMgr::AddPlayers( const char* connName, int nToAdd )
{
const char* fmt = "UPDATE games SET nHere = nHere+%d "
const char* fmt = "UPDATE " DB_NAME " SET nHere = nHere+%d "
"WHERE connName = '%s'";
char query[256];
snprintf( query, sizeof(query), fmt, nToAdd, connName );
logf( XW_LOGINFO, "%s: query: %s", __func__, query );
execSql( query );
}
void
DBMgr::ClearCIDs( void )
{
execSql( "UPDATE " TABLE_NAME " set cid = 0" );
}
void
DBMgr::execSql( const char* query )
{
PGresult* result = PQexec( m_pgconn, query );
PQclear( result );
logf( XW_LOGINFO, "PQexecParams=>%d", result );
}
/*

View file

@ -30,6 +30,8 @@ class DBMgr {
~DBMgr();
void ClearCIDs( void );
void AddNew( const char* cookie, const char* connName, CookieID cid,
int langCode, int nPlayersT );
@ -40,6 +42,7 @@ class DBMgr {
private:
DBMgr();
void execSql( const char* query ); /* no-results query */
PGconn* m_pgconn;
//int m_nextCID;
}; /* DBMgr */

View file

@ -916,6 +916,8 @@ main( int argc, char** argv )
exit( 1 );
}
DBMgr::Get()->ClearCIDs(); /* get prev boot's state in db */
vector<int>::const_iterator iter_game;
for ( iter_game = ints_game.begin(); iter_game != ints_game.end();
++iter_game ) {