StatsLoggerMySQL.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_MYSQL
00022 
00023 #include <common/StatsLoggerMySQL.h>
00024 #include <common/Logger.h>
00025 
00026 StatsLoggerMySQL::StatsLoggerMySQL() : mysql_(0)
00027 {
00028 
00029 }
00030 
00031 StatsLoggerMySQL::~StatsLoggerMySQL()
00032 {
00033 }
00034 
00035 bool StatsLoggerMySQL::runQuery(const char *format, ...)
00036 {
00037         if (!success_) return false;
00038 
00039         va_list ap; 
00040         va_start(ap, format); 
00041         std::string text = S3D::formatStringList(format, ap);
00042         va_end(ap); 
00043 
00044         return (mysql_real_query(mysql_, text.c_str(), (int) text.size()) == 0);
00045 }
00046 
00047 std::list<StatsLoggerDatabase::RowResult> StatsLoggerMySQL::runSelectQuery(const char *format, ...)
00048 {
00049         std::list<StatsLoggerDatabase::RowResult> results;
00050         if (!success_) return results;
00051 
00052         va_list ap; 
00053         va_start(ap, format); 
00054         std::string text = S3D::formatStringList(format, ap);
00055         va_end(ap); 
00056 
00057         if (mysql_real_query(mysql_, text.c_str(), (int) text.size()) != 0) return results;
00058 
00059         MYSQL_RES *result = mysql_store_result(mysql_);
00060         if (result)
00061         {
00062                 int rows = (int) mysql_num_rows(result);
00063                 int cols = (int) mysql_num_fields(result);
00064                 MYSQL_FIELD *fields = mysql_fetch_fields(result);
00065                 for (int r=0; r<rows; r++)
00066                 {
00067                         StatsLoggerDatabase::RowResult rowResult;
00068                         MYSQL_ROW row = mysql_fetch_row(result);
00069 
00070                         for (int c=0; c<cols; c++)
00071                         {
00072                                 const char *value = (row[c]?row[c]:"");
00073                                 const char *name = fields[c].name;
00074                                 rowResult.columns.push_back(value);
00075                                 rowResult.names[name] = c;
00076                         }
00077 
00078                         results.push_back(rowResult);
00079                 }
00080                 mysql_free_result(result);
00081         }
00082 
00083         return results;
00084 }
00085 
00086 bool StatsLoggerMySQL::connectDatabase(const char *host, const char *port,
00087         const char *user, const char *passwd, 
00088         const char *db)
00089 {
00090     mysql_ = mysql_init(0);
00091         if (!mysql_)
00092         {
00093                 Logger::log( "Failed to init mysql");
00094                 return false;
00095         }
00096         mysql_->reconnect = 1;
00097 
00098         int connectPort = 0;
00099         const char *connectSocket = 0;
00100         if (0 == stricmp(host, "localhost"))
00101         {
00102                 connectSocket = port;
00103         }
00104         else
00105         {
00106                 connectPort = atoi(port);
00107         }
00108 
00109         if (!mysql_real_connect(
00110                 mysql_,
00111                 host,
00112                 user,
00113                 passwd,
00114                 db,
00115                 connectPort, 
00116                 connectSocket, 0))
00117         {
00118                 Logger::log(S3D::formatStringBuffer("mysql stats logger failed to start. "
00119                         "Error: %s",
00120                         mysql_error(mysql_)));
00121                 Logger::log(S3D::formatStringBuffer("mysql params : host %s, user %s, passwd %s, db %s",
00122                         host, user, passwd, db));
00123                 return false;
00124         }
00125 
00126         return true;
00127 }
00128 
00129 int StatsLoggerMySQL::getLastInsertId()
00130 {
00131         return (int) mysql_insert_id(mysql_);
00132 }
00133 
00134 void StatsLoggerMySQL::escapeString(char *to, const char *from, unsigned long length)
00135 {
00136         mysql_real_escape_string(mysql_, to, from, length);
00137 }
00138 
00139 #endif // HAVE_MYSQL

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