StatsLoggerPGSQL.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #ifdef HAVE_PGSQL
00022 /* Code contains several string format bugs, and at least one buffer overflow */
00023 
00024 #include <common/StatsLoggerPGSQL.h>
00025 
00026 StatsLoggerPGSQL::StatsLoggerPGSQL() : pgsql_(NULL), lastresult_ (NULL)
00027 {
00028 
00029 }
00030 
00031 StatsLoggerPGSQL::~StatsLoggerPGSQL()
00032 {
00033 }
00034 
00035 #define SQL_BUFFER_SIZE 8192
00036 virtual bool StatsLoggerPGSQL::runQuery(const char *fmt, ...)
00037 {
00038     if (!success_) return false;
00039 
00040     if(lastresult_) {
00041         PQclear(lastresult_);
00042         lastresult_ = NULL;
00043     }
00044 
00045     static char text[SQL_BUFFER_SIZE];
00046     va_list ap;
00047     va_start(ap, fmt);
00048     int sqlLen = vsnprintf(text, SQL_BUFFER_SIZE, fmt, ap);
00049     va_end(ap);
00050 
00051     if(sqlLen >= SQL_BUFFER_SIZE) {
00052         Logger::log(0, "pgsql: Query failed, too long.\n");
00053         return false;
00054     }
00055 
00056     lastresult_ = PQexec(pgsql_, text);
00057     
00058     return lastresult_ && (
00059             PQresultStatus(lastresult_) == PGRES_COMMAND_OK ||
00060             PQresultStatus(lastresult_) == PGRES_TUPLES_OK
00061             );
00062 }
00063 
00064 bool StatsLoggerPGSQL::connectDatabase(const char *host, const char *user, 
00065         const char *passwd, const char *db)
00066 {
00067       pgsql_ = PQsetdbLogin(
00068                 host,
00069                 NULL,
00070                 NULL,
00071                 NULL,
00072                 db,
00073                 user,
00074                 passwd);
00075     if (pgsql_ && PQstatus(pgsql_) == CONNECTION_OK)
00076     {
00077         Logger::log(0, "pgsql stats logger started");
00078     }
00079     else
00080     {
00081         Logger::log(0, "pgsql stats logger failed to start. "
00082                 "Error: %s",
00083                 PQerrorMessage(pgsql_));
00084         Logger::log(0, "pgsql params : host %s, user %s, passwd %s, db %s",
00085                 host.c_str(), user.c_str(),
00086                 passwd.c_str(), db.c_str());
00087         if(pgsql_) {
00088             PQfinish(pgsql_);
00089             pgsql_ = NULL;
00090         }
00091                 return false;
00092     }
00093 
00094         return true;
00095 }
00096 
00097 #endif // HAVE_PGSQL

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3