From 663551fcb5513c8e4fa5359334607e0e3b7f592b Mon Sep 17 00:00:00 2001 From: Andy2 Date: Sun, 12 Sep 2010 03:57:23 -0700 Subject: [PATCH] improve db, adding uniqueness contraints. Pass in connName so can meet that constraint. Figure out next cid using a query at startup. --- xwords4/relay/crefmgr.cpp | 3 ++- xwords4/relay/dbmgr.cpp | 38 +++++++++++++++++++++++++++++++++++--- xwords4/relay/dbmgr.h | 1 + 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/xwords4/relay/crefmgr.cpp b/xwords4/relay/crefmgr.cpp index b052dedb1..7dbe4a27f 100644 --- a/xwords4/relay/crefmgr.cpp +++ b/xwords4/relay/crefmgr.cpp @@ -344,7 +344,8 @@ CRefMgr::getMakeCookieRef_locked( const char* cookie, const char* connName, if ( cref == NULL && !alreadyHere ) { CookieID cid = nextCID( NULL ); 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; diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 911a22feb..b76143b47 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -19,6 +19,7 @@ */ #include +#include #include "dbmgr.h" #include "xwrelay_priv.h" @@ -33,6 +34,18 @@ DBMgr::DBMgr() ConnStatusType status = PQstatus( m_pgconn ); 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() @@ -50,10 +63,12 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid, if ( !cookie ) cookie = ""; if ( !connName ) connName = ""; - const char* fmt = "INSERT INTO games (cookie, connName, nTotal, nHere, lang) " - "VALUES( '%s', '%s', %d, %d, %d )"; + const char* fmt = "INSERT INTO games " + "(cid, cookie, connName, nTotal, nHere, lang) " + "VALUES( %d, '%s', '%s', %d, %d, %d )"; 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 ); PGresult* result = PQexec( m_pgconn, buf ); #else @@ -79,3 +94,20 @@ DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid, #endif 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 +); + */ + + diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index c7322b846..b1e74e4d0 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -34,6 +34,7 @@ class DBMgr { private: PGconn* m_pgconn; + int m_nextCID; }; /* DBMgr */