diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 33d3829fe..91bd6ebeb 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -22,6 +22,9 @@ #include #include #include +#include +#include +#include #include "dbmgr.h" #include "mlock.h" @@ -393,6 +396,39 @@ DBMgr::RecordSent( const char* const connName, HostID hid, int nBytes ) execSql( query ); } +void +DBMgr::RecordSent( const int* msgIDs, int nMsgIDs ) +{ + if ( nMsgIDs > 0 ) { + stringstream buf; + buf << "SELECT connname, hid, sum(msglen) FROM " MSGS_TABLE + " WHERE id IN ("; + + for ( int ii = 0; ; ) { + buf << msgIDs[ii]; + if ( ++ii == nMsgIDs ) { + break; + } + buf << ','; + } + buf << ") GROUP BY connname,hid"; + + const char* query = buf.str().c_str(); + logf( XW_LOGINFO, "%s: query: %s", __func__, query ); + + PGresult* result = PQexec( getThreadConn(), query ); + if ( PGRES_COMMAND_OK == PQresultStatus( result ) ) { + int ntuples = PQntuples( result ); + for ( int ii = 0; ii < ntuples; ++ii ) { + RecordSent( PQgetvalue( result, ii, 0 ), + atoi( PQgetvalue( result, ii, 1 ) ), + atoi( PQgetvalue( result, ii, 2 ) ) ); + } + } + PQclear( result ); + } +} + void DBMgr::GetPlayerCounts( const char* const connName, int* nTotal, int* nHere ) { @@ -475,7 +511,7 @@ DBMgr::PendingMsgCount( const char* connName, int hid ) } bool -DBMgr::execSql( const char* query ) +DBMgr::execSql( const char* const query ) { PGresult* result = PQexec( getThreadConn(), query ); bool ok = PGRES_COMMAND_OK == PQresultStatus(result); @@ -606,23 +642,24 @@ DBMgr::GetStoredMessage( const char* const connName, int hid, void DBMgr::RemoveStoredMessages( const int* msgIDs, int nMsgIDs ) { - char ids[1024]; - int len = 0; - int ii; - assert( nMsgIDs > 0 ); - for ( ii = 0; ; ) { - len += snprintf( ids + len, sizeof(ids) - len, "%d,", msgIDs[ii] ); - if ( ++ii == nMsgIDs ) { - ids[len-1] = '\0'; /* overwrite last comma */ - break; - } - } - const char* fmt = "DELETE from " MSGS_TABLE " WHERE id in (%s)"; - char query[1024]; - snprintf( query, sizeof(query), fmt, ids ); - logf( XW_LOGINFO, "%s: query: %s", __func__, query ); + if ( nMsgIDs > 0 ) { + stringstream buf; - execSql( query ); + buf << "DELETE FROM " MSGS_TABLE " WHERE id IN ("; + + for ( int ii = 0; ; ) { + buf << msgIDs[ii]; + if ( ++ii == nMsgIDs ) { + break; + } + buf << ','; + } + buf << ')'; + + const char* query = buf.str().c_str(); + logf( XW_LOGINFO, "%s: query: %s", __func__, query ); + execSql( query ); + } } static void diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 964360ded..34f013beb 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -64,6 +64,7 @@ class DBMgr { bool AddCID( const char* const connName, CookieID cid ); void ClearCID( const char* connName ); void RecordSent( const char* const connName, HostID hid, int nBytes ); + void RecordSent( const int* msgID, int nMsgIDs ); void GetPlayerCounts( const char* const connName, int* nTotal, int* nHere ); @@ -91,7 +92,7 @@ class DBMgr { private: DBMgr(); - bool execSql( const char* query ); /* no-results query */ + bool execSql( const char* const query ); /* no-results query */ void readArray( const char* const connName, int arr[] ); PGconn* getThreadConn( void );