// ==========================================================================
// Author: Yee Hsu
// Date: 8/3/2008
// File: MySQL.cpp
//
// Desc: My own attempt to write a string class. String class is an
// object of char* arrays and performs similarities to the "string"
// type. Class overloads several operators to perfrom things such
// as concatenations, copies, and comparisons.
// ==========================================================================
#include "stdafx.h"
#include "CMySQL.h"
// ==========================================================================
// Identifier: MySQL()
//
// Description: Node Constructor
// ==========================================================================
MySQL::MySQL()
{
this->mysql = NULL;
this->result = NULL;
this->field = NULL;
this->row = NULL;
}
// ==========================================================================
// Identifier: MySQL()
//
// Description: Node Destructor
// ==========================================================================
MySQL::~MySQL()
{
if (this->mysql)
{
this->Disconnect();
}
this->mysql = NULL;
}
// ==========================================================================
// Identifier: Disconnect()
//
// Description: Disconnect a MySQL session
// ==========================================================================
void MySQL::Disconnect()
{
mysql_close(this->mysql);
this->mysql = NULL;
}
// ==========================================================================
// Identifier: Connect()
//
// Description: Connect to a MySQL session
// ==========================================================================
bool MySQL::Connect(const MySQLConnect ConnectInfo)
{
this->ConnectInfo = ConnectInfo;
return this->Connect();
}
// ==========================================================================
// Identifier: Connect()
//
// Description: Connect to a MySQL session
// ==========================================================================
bool MySQL::Connect()
{
try
{
// init mysql
if ((this->mysql = mysql_init((MYSQL *)0)) == NULL)
{
return false;
}
// connect to mysql server
if (mysql_real_connect(this->mysql, // MySQL Handle
this->ConnectInfo.sHostName.c_str(), // Hostname to connect to
this->ConnectInfo.sUserName.c_str(), // Username to connect with
this->ConnectInfo.sPassword.c_str(), // Password to connect with
this->ConnectInfo.sDatabase.c_str(), // Database to connect to
MYSQL_PORT, NULL, 0) == NULL)
{
this->Disconnect();
return false;
}
// connect to database
this->mysql->reconnect = 1;
if (mysql_select_db(this->mysql, this->ConnectInfo.sDatabase.c_str()) < 0)
{
this->Disconnect();
return false;
}
}
catch (...)
{
return false;
}
return true;
}
// ==========================================================================
// Identifier: ExecuteQuery()
//
// Description: Execute MySQL statement and return in result set
// ==========================================================================
bool MySQL::ExecuteQuery(const std::string sSQL, int* nRowsReturned)
{
try
{
if (!this->mysql)
return false;
if (mysql_query(this->mysql, sSQL.c_str()) == 0)
{
if (nRowsReturned != NULL)
{
MYSQL_RES* res = mysql_store_result(this->mysql) ;
*nRowsReturned = (int) mysql_num_rows(res) ;
mysql_free_result(res) ;
}
return true;
}
}
catch (...)
{
return false;
}
return false;
}