00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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