mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
add new class to interface with postgres database in an experiment to
simplify things by using a db to survive a crash/reboot. So far (with next checkin) manages to update a db. Next need to do queries instead of searches through in-memory data.
This commit is contained in:
parent
0b46455044
commit
87de53cf96
3 changed files with 133 additions and 7 deletions
|
@ -18,17 +18,19 @@
|
||||||
|
|
||||||
CXX = g++
|
CXX = g++
|
||||||
CC=$(CXX)
|
CC=$(CXX)
|
||||||
SRC = xwrelay.cpp \
|
SRC = \
|
||||||
|
configs.cpp \
|
||||||
cref.cpp \
|
cref.cpp \
|
||||||
|
crefmgr.cpp \
|
||||||
ctrl.cpp \
|
ctrl.cpp \
|
||||||
|
dbmgr.cpp \
|
||||||
http.cpp \
|
http.cpp \
|
||||||
tpool.cpp \
|
lstnrmgr.cpp \
|
||||||
|
permid.cpp \
|
||||||
states.cpp \
|
states.cpp \
|
||||||
timermgr.cpp \
|
timermgr.cpp \
|
||||||
configs.cpp \
|
tpool.cpp \
|
||||||
crefmgr.cpp \
|
xwrelay.cpp \
|
||||||
permid.cpp \
|
|
||||||
lstnrmgr.cpp \
|
|
||||||
|
|
||||||
# STATIC ?= -static
|
# STATIC ?= -static
|
||||||
GITINFO = gitversion.txt
|
GITINFO = gitversion.txt
|
||||||
|
@ -36,8 +38,11 @@ HASH=$(shell git describe)
|
||||||
|
|
||||||
OBJ = $(patsubst %.cpp,%.o,$(SRC))
|
OBJ = $(patsubst %.cpp,%.o,$(SRC))
|
||||||
#LDFLAGS += -pthread -g -lmcheck $(STATIC)
|
#LDFLAGS += -pthread -g -lmcheck $(STATIC)
|
||||||
LDFLAGS += -pthread -g $(STATIC)
|
LDFLAGS += -pthread -g $(STATIC) \
|
||||||
|
-L$(shell pg_config --libdir) -lpq
|
||||||
|
|
||||||
CPPFLAGS += -DSPAWN_SELF -DDO_HTTP -g -Wall \
|
CPPFLAGS += -DSPAWN_SELF -DDO_HTTP -g -Wall \
|
||||||
|
-I $(shell pg_config --includedir) \
|
||||||
-DSVN_REV=\"$(shell cat $(GITINFO) 2>/dev/null || echo -n $(HASH) )\"
|
-DSVN_REV=\"$(shell cat $(GITINFO) 2>/dev/null || echo -n $(HASH) )\"
|
||||||
|
|
||||||
# turn on semaphore debugging
|
# turn on semaphore debugging
|
||||||
|
|
81
xwords4/relay/dbmgr.cpp
Normal file
81
xwords4/relay/dbmgr.cpp
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2010 by Eric House (xwords@eehouse.org). All rights reserved.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "dbmgr.h"
|
||||||
|
#include "xwrelay_priv.h"
|
||||||
|
|
||||||
|
#define DB_NAME "xwgames"
|
||||||
|
|
||||||
|
DBMgr::DBMgr()
|
||||||
|
{
|
||||||
|
logf( XW_LOGINFO, "%s called", __func__ );
|
||||||
|
m_pgconn = PQconnectdb( "dbname = " DB_NAME );
|
||||||
|
logf( XW_LOGINFO, "%s:, m_pgconn: %p", __func__, m_pgconn );
|
||||||
|
|
||||||
|
ConnStatusType status = PQstatus( m_pgconn );
|
||||||
|
assert( status == CONNECTION_OK );
|
||||||
|
}
|
||||||
|
|
||||||
|
DBMgr::~DBMgr()
|
||||||
|
{
|
||||||
|
logf( XW_LOGINFO, "%s called", __func__ );
|
||||||
|
PQfinish( m_pgconn );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DBMgr::AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||||
|
int langCode, int nPlayersT, int nPlayersH )
|
||||||
|
{
|
||||||
|
|
||||||
|
#if 1
|
||||||
|
if ( !cookie ) cookie = "";
|
||||||
|
if ( !connName ) connName = "";
|
||||||
|
|
||||||
|
const char* fmt = "INSERT INTO games (cookie, connName, nTotal, nHere, lang) "
|
||||||
|
"VALUES( '%s', '%s', %d, %d, %d )";
|
||||||
|
char buf[256];
|
||||||
|
snprintf( buf, sizeof(buf), fmt, cookie, connName, nPlayersT, nPlayersH, langCode );
|
||||||
|
logf( XW_LOGINFO, "passing %s", buf );
|
||||||
|
PGresult* result = PQexec( m_pgconn, buf );
|
||||||
|
#else
|
||||||
|
const char* command = "INSERT INTO games (cookie, connName, ntotal, nhere, lang) "
|
||||||
|
"VALUES( $1, $2, $3, $4, $5 )";
|
||||||
|
char nPlayersHBuf[4];
|
||||||
|
char nPlayersTBuf[4];
|
||||||
|
char langBuf[4];
|
||||||
|
|
||||||
|
snprintf( nPlayersHBuf, sizeof(nPlayersHBuf), "%d", nPlayersH );
|
||||||
|
snprintf( nPlayersTBuf, sizeof(nPlayersTBuf), "%d", nPlayersT );
|
||||||
|
snprintf( langBuf, sizeof(langBuf), "%d", langCode );
|
||||||
|
|
||||||
|
const char * const paramValues[] = { cookie, connName, nPlayersTBuf, nPlayersHBuf, langBuf };
|
||||||
|
|
||||||
|
PGresult* result = PQexecParams( m_pgconn, command,
|
||||||
|
sizeof(paramValues)/sizeof(paramValues[0]),
|
||||||
|
NULL, /*const Oid *paramTypes,*/
|
||||||
|
paramValues,
|
||||||
|
NULL, /*const int *paramLengths,*/
|
||||||
|
NULL, /*const int *paramFormats,*/
|
||||||
|
0 /*int resultFormat*/ );
|
||||||
|
#endif
|
||||||
|
logf( XW_LOGINFO, "PQexecParams=>%d", result );
|
||||||
|
}
|
40
xwords4/relay/dbmgr.h
Normal file
40
xwords4/relay/dbmgr.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright 2010 by Eric House (xwords@eehouse.org). All rights reserved.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _DBMGR_H_
|
||||||
|
#define _DBMGR_H_
|
||||||
|
|
||||||
|
#include "xwrelay.h"
|
||||||
|
#include <libpq-fe.h>
|
||||||
|
|
||||||
|
class DBMgr {
|
||||||
|
public:
|
||||||
|
DBMgr();
|
||||||
|
~DBMgr();
|
||||||
|
|
||||||
|
void AddNew( const char* cookie, const char* connName, CookieID cid,
|
||||||
|
int langCode, int nPlayersT, int nPlayersH );
|
||||||
|
|
||||||
|
private:
|
||||||
|
PGconn* m_pgconn;
|
||||||
|
}; /* DBMgr */
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Reference in a new issue