mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
factor dbmgr code; add and call method to zero CIDs on boot.
This commit is contained in:
parent
821e4d9c39
commit
3b5b5f0241
3 changed files with 25 additions and 8 deletions
|
@ -25,6 +25,7 @@
|
||||||
#include "xwrelay_priv.h"
|
#include "xwrelay_priv.h"
|
||||||
|
|
||||||
#define DB_NAME "xwgames"
|
#define DB_NAME "xwgames"
|
||||||
|
#define TABLE_NAME "games"
|
||||||
|
|
||||||
static DBMgr* s_instance = NULL;
|
static DBMgr* s_instance = NULL;
|
||||||
|
|
||||||
|
@ -73,20 +74,18 @@ void
|
||||||
DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
|
DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||||
int langCode, int nPlayersT )
|
int langCode, int nPlayersT )
|
||||||
{
|
{
|
||||||
|
|
||||||
#if 1
|
#if 1
|
||||||
if ( !cookie ) cookie = "";
|
if ( !cookie ) cookie = "";
|
||||||
if ( !connName ) connName = "";
|
if ( !connName ) connName = "";
|
||||||
|
|
||||||
const char* fmt = "INSERT INTO games "
|
const char* fmt = "INSERT INTO " DB_NAME
|
||||||
"(cid, cookie, connName, nTotal, nHere, lang, ctime) "
|
" (cid, cookie, connName, nTotal, nHere, lang, ctime) "
|
||||||
"VALUES( %d, '%s', '%s', %d, %d, %d, 'now' )";
|
"VALUES( %d, '%s', '%s', %d, %d, %d, 'now' )";
|
||||||
char buf[256];
|
char buf[256];
|
||||||
snprintf( buf, sizeof(buf), fmt, cid/*m_nextCID++*/, cookie, connName,
|
snprintf( buf, sizeof(buf), fmt, cid/*m_nextCID++*/, cookie, connName,
|
||||||
nPlayersT, 0, langCode );
|
nPlayersT, 0, langCode );
|
||||||
logf( XW_LOGINFO, "passing %s", buf );
|
logf( XW_LOGINFO, "passing %s", buf );
|
||||||
PGresult* result = PQexec( m_pgconn, buf );
|
execSql( buf );
|
||||||
PQclear( result );
|
|
||||||
#else
|
#else
|
||||||
const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) "
|
const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) "
|
||||||
"VALUES( $1, $2, $3, $4, $5 )";
|
"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 *paramLengths,*/
|
||||||
NULL, /*const int *paramFormats,*/
|
NULL, /*const int *paramFormats,*/
|
||||||
0 /*int resultFormat*/ );
|
0 /*int resultFormat*/ );
|
||||||
#endif
|
|
||||||
logf( XW_LOGINFO, "PQexecParams=>%d", result );
|
logf( XW_LOGINFO, "PQexecParams=>%d", result );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
CookieID
|
CookieID
|
||||||
|
@ -115,7 +114,7 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH )
|
||||||
{
|
{
|
||||||
CookieID cid = 0;
|
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 lang = %d "
|
||||||
"AND nTotal = %d "
|
"AND nTotal = %d "
|
||||||
"AND %d <= nTotal-nHere "
|
"AND %d <= nTotal-nHere "
|
||||||
|
@ -138,14 +137,27 @@ DBMgr::FindOpen( const char* cookie, int lang, int nPlayersT, int nPlayersH )
|
||||||
void
|
void
|
||||||
DBMgr::AddPlayers( const char* connName, int nToAdd )
|
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'";
|
"WHERE connName = '%s'";
|
||||||
char query[256];
|
char query[256];
|
||||||
snprintf( query, sizeof(query), fmt, nToAdd, connName );
|
snprintf( query, sizeof(query), fmt, nToAdd, connName );
|
||||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query );
|
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 );
|
PGresult* result = PQexec( m_pgconn, query );
|
||||||
PQclear( result );
|
PQclear( result );
|
||||||
|
logf( XW_LOGINFO, "PQexecParams=>%d", result );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -30,6 +30,8 @@ class DBMgr {
|
||||||
|
|
||||||
~DBMgr();
|
~DBMgr();
|
||||||
|
|
||||||
|
void ClearCIDs( void );
|
||||||
|
|
||||||
void AddNew( const char* cookie, const char* connName, CookieID cid,
|
void AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||||
int langCode, int nPlayersT );
|
int langCode, int nPlayersT );
|
||||||
|
|
||||||
|
@ -40,6 +42,7 @@ class DBMgr {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DBMgr();
|
DBMgr();
|
||||||
|
void execSql( const char* query ); /* no-results query */
|
||||||
PGconn* m_pgconn;
|
PGconn* m_pgconn;
|
||||||
//int m_nextCID;
|
//int m_nextCID;
|
||||||
}; /* DBMgr */
|
}; /* DBMgr */
|
||||||
|
|
|
@ -916,6 +916,8 @@ main( int argc, char** argv )
|
||||||
exit( 1 );
|
exit( 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DBMgr::Get()->ClearCIDs(); /* get prev boot's state in db */
|
||||||
|
|
||||||
vector<int>::const_iterator iter_game;
|
vector<int>::const_iterator iter_game;
|
||||||
for ( iter_game = ints_game.begin(); iter_game != ints_game.end();
|
for ( iter_game = ints_game.begin(); iter_game != ints_game.end();
|
||||||
++iter_game ) {
|
++iter_game ) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue