improve db, adding uniqueness contraints. Pass in connName so can

meet that constraint.  Figure out next cid using a query at startup.
This commit is contained in:
Andy2 2010-09-12 03:57:23 -07:00
parent 86a1e943ca
commit 663551fcb5
3 changed files with 38 additions and 4 deletions

View file

@ -344,7 +344,8 @@ CRefMgr::getMakeCookieRef_locked( const char* cookie, const char* connName,
if ( cref == NULL && !alreadyHere ) { if ( cref == NULL && !alreadyHere ) {
CookieID cid = nextCID( NULL ); CookieID cid = nextCID( NULL );
cref = AddNew( cookie, connName, cid, langCode ); cref = AddNew( cookie, connName, cid, langCode );
m_db->AddNew( cookie, connName, cid, langCode, nPlayersT, nPlayersH ); m_db->AddNew( cookie, cref->ConnName(), cid, langCode,
nPlayersT, nPlayersH );
} }
return cref; return cref;

View file

@ -19,6 +19,7 @@
*/ */
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include "dbmgr.h" #include "dbmgr.h"
#include "xwrelay_priv.h" #include "xwrelay_priv.h"
@ -33,6 +34,18 @@ DBMgr::DBMgr()
ConnStatusType status = PQstatus( m_pgconn ); ConnStatusType status = PQstatus( m_pgconn );
assert( status == CONNECTION_OK ); assert( status == CONNECTION_OK );
/* Now figure out what the largest cid currently is. There must be a way
to get postgres to do this for me.... */
const char* query = "SELECT cid FROM games ORDER BY - cid LIMIT 1";
PGresult* result = PQexec( m_pgconn, query );
if ( 0 == PQntuples( result ) ) {
m_nextCID = 1;
} else {
char* value = PQgetvalue( result, 0, 0 );
m_nextCID = 1 + atoi( value );
}
logf( XW_LOGINFO, "%s: m_nextCID=%d", __func__, m_nextCID );
} }
DBMgr::~DBMgr() DBMgr::~DBMgr()
@ -50,10 +63,12 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
if ( !cookie ) cookie = ""; if ( !cookie ) cookie = "";
if ( !connName ) connName = ""; if ( !connName ) connName = "";
const char* fmt = "INSERT INTO games (cookie, connName, nTotal, nHere, lang) " const char* fmt = "INSERT INTO games "
"VALUES( '%s', '%s', %d, %d, %d )"; "(cid, cookie, connName, nTotal, nHere, lang) "
"VALUES( %d, '%s', '%s', %d, %d, %d )";
char buf[256]; char buf[256];
snprintf( buf, sizeof(buf), fmt, cookie, connName, nPlayersT, nPlayersH, langCode ); snprintf( buf, sizeof(buf), fmt, m_nextCID++, cookie, connName,
nPlayersT, nPlayersH, langCode );
logf( XW_LOGINFO, "passing %s", buf ); logf( XW_LOGINFO, "passing %s", buf );
PGresult* result = PQexec( m_pgconn, buf ); PGresult* result = PQexec( m_pgconn, buf );
#else #else
@ -79,3 +94,20 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
#endif #endif
logf( XW_LOGINFO, "PQexecParams=>%d", result ); logf( XW_LOGINFO, "PQexecParams=>%d", result );
} }
/*
Schema:
CREATE TABLE games (
cid integer PRIMARY KEY,
cookie VARCHAR(32),
connName VARCHAR(64) UNIQUE,
nTotal INTEGER,
nHere INTEGER,
lang INTEGER,
ctime TIMESTAMP,
mtime TIMESTAMP
);
*/

View file

@ -34,6 +34,7 @@ class DBMgr {
private: private:
PGconn* m_pgconn; PGconn* m_pgconn;
int m_nextCID;
}; /* DBMgr */ }; /* DBMgr */