mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-28 07:58:08 +01:00
add new RecordSent to take list of msg ids -- and use stringstream
instead of static buffer and snprintf in one case. May make sense elsewhere too.
This commit is contained in:
parent
8148c7699a
commit
b511b4c455
2 changed files with 56 additions and 18 deletions
|
@ -22,6 +22,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#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
|
||||
|
|
|
@ -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 );
|
||||
|
|
Loading…
Add table
Reference in a new issue