#include "CCMySQL.h"
#define __DEBUG 1
#define BREAKALLOC NULL
#if __DEBUG == 1
#include <crtdbg.h>
int __cdecl MyAllocHook(
int nAllocType,
void * pvData,
size_t nSize,
int nBlockUse,
long lRequest,
const unsigned char * szFileName,
int nLine
)
{
char *operation[] = { "", "allocating", "re-allocating", "freeing" };
char *blockType[] = { "Free", "Normal", "CRT", "Ignore", "Client" };
if ( nBlockUse == _CRT_BLOCK ) { // Ignore internal C runtime library allocations
return( TRUE );
}
_ASSERT( ( nAllocType > 0 ) && ( nAllocType < 4 ) );
_ASSERT( ( nBlockUse >= 0 ) && ( nBlockUse < 5 ) );
#if printMemAllocReport == 1
_CrtDbgReport(0,0,0,0,"Memory operation in %s, line %d: %s a %d-byte '%s' block (# %ld)",
szFileName, nLine, operation[nAllocType], nSize,
blockType[nBlockUse], lRequest );
if ( pvData != NULL )
_CrtDbgReport(0,0,0,0," at %X", pvData );
_CrtDbgReport(0,0,0,0,"\n");
#endif
if(BREAKALLOC) __asm int 3
return( TRUE ); // Allow the memory operation to proceed
}
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch( ul_reason_for_call ) {
case DLL_PROCESS_ATTACH:
_CrtSetAllocHook( MyAllocHook );
break;
case DLL_PROCESS_DETACH:
_CrtDumpMemoryLeaks();
}
return TRUE;
}
#endif
///////////////////////////
// Ctor
CCMySQL::CCMySQL() {
this->connected = false;
this->entry = 0;
this->result = NULL;
this->connection = ::mysql_init(NULL);
this->data = new char***[1];
this->data[0] = NULL;
this->datasize = new int[1];
this->datasize[0] = 1;
}
///////////////////////////
// Dtor
CCMySQL::~CCMySQL() {
this->entry = 0;
if(this->result != NULL) {
::mysql_free_result(this->result);
}
if(this->data[0] != NULL) {
int i = 0, j = 0;
for(j = 0; this->data[j] != NULL; j++) {
for(i = 0; this->data[j][i] != NULL; i++) {
delete this->data[j][i][0];
delete this->data[j][i][1];
delete this->data[j][i];
}
delete this->data[j];
}
}
delete this->data;
delete this->datasize;
this->data = NULL;
this->disconnect();
mysql_shutdown(this->connection);
}
///////////////////////////
// Ctor & connect
CCMySQL::CCMySQL(char* server, char* user, char* pass, char* db) {
this->connected = false;
this->result = NULL;
this->data = new char***[1];
this->data[0] = NULL;
this->datasize = new int[1];
this->datasize[0] = 1;
this->connection = ::mysql_init(NULL);
this->connect(server, user, pass, db);
}
////////////////////////////
// connect
bool CCMySQL::connect(char* server, char* user, char* pass, char* db) {
void* res = mysql_connect(this->connection, server, user, pass);
if(::strcmp(db, "") != 0) {
this->SelectDB(db);
}
if(res == NULL) {
this->connected = false;
return false;
}
else {
this->connected = true;
return true;
}
}
/////////////////////////////
// disconnect
void CCMySQL::disconnect() {
STARTSEH()
this->connected = false;
::mysql_close(this->connection);
ENDSEH((void)0;)
}
/////////////////////////////
// Select Database
void CCMySQL::SelectDB(char* db) {
if(::mysql_select_db(this->connection, db) == 0) {
::printf("%s", ::mysql_error(this->connection));
}
}
/////////////////////////////
// Send Query
bool CCMySQL::query(char* query) {
int i = 0;
if(this->result != NULL) {
::mysql_free_result(this->result);
this->result = NULL;
}
if(::mysql_query(this->connection, query) == 0) {
}
if(::mysql_error(this->connection)[0] != '\0') {
return false;
}
this->result = ::mysql_store_result(this->connection);
if(this->data[0] != NULL) {
int i = 0, j = 0;
for(j = 0; this->data[j] != NULL; j++) {
for(i = 0; this->data[j][i] != NULL; i++) {
delete this->data[j][i][0];
delete this->data[j][i][1];
delete this->data[j][i];
}
delete this->data[j];
}
::realloc(this->datasize, sizeof(int));
this->datasize[0] = 1;
this->data = (char****)::realloc(this->data, this->datasize[0] * sizeof(char***));
this->data[0] = NULL;
}
if(this->result == NULL) {
return true;
}
for(int j = 0;(this->mysql_row=::mysql_fetch_row(this->result))!=NULL; j++) {
this->datasize[0]++;
this->data = (char****)::realloc(this->data, this->datasize[0] * sizeof(char***));
this->datasize = (int*)::realloc(this->datasize, (this->datasize[0]+1) * sizeof(int));
this->data[j] = new char**[1];
this->datasize[j+1] = 1;
this->data[j+1] = NULL;
::mysql_field_seek(this->result, 0);
for (this->mysql_field=::mysql_fetch_field(result), i=0; this->mysql_field; this->mysql_field=::mysql_fetch_field(this->result), i++) {
if(this->mysql_row[i]) {
this->datasize[j+1]++;
this->data[j] = (char***)::realloc(this->data[j], this->datasize[j+1] * sizeof(char**));
this->data[j][i] = new char*[2];
this->data[j][i][0] = new char[::strlen(this->mysql_field->name)+1];
this->data[j][i][1] = new char[::strlen(this->mysql_row[i])+1];
this->data[j][i+1] = NULL;
::strcpy(this->data[j][i][1], this->mysql_row[i]);
::strcpy(this->data[j][i][0], this->mysql_field->name);
}
}
}
this->entry = 0;
if(this->result != NULL) {
::mysql_free_result(this->result);
this->result = NULL;
}
return true;
}
///////////////////////////
// Load next DataRow
bool CCMySQL::NextRow() {
if(this->data[this->entry] == NULL) {
return false;
}
if(this->data[this->entry+1] != NULL) {
this->entry++;
return true;
}
return false;
}
////////////////////////////
// Load Previous DataRow
bool CCMySQL::PrevRow() {
if(this->entry > 0) {
this->entry--;
return true;
}
return false;
}
///////////////////////////////////
// GetDataEntry as char[] using ID
void* CCMySQL::GetEntry(int nr) {
if(this->data[this->entry] != NULL) {
return this->data[this->entry][nr][1];
}
else return false;
}
///////////////////////////////////////
// Get DataEntry as char[] using Name
void* CCMySQL::GetEntry(char* name) {
if(this->data[this->entry] == NULL) {
return false;
}
for(int i = 0; this->data[this->entry][i] != NULL; i++) {
if(::_stricmp(this->data[this->entry][i][0], name) == 0) {
return this->data[this->entry][i][1];
}
}
return false;
}
//////////////////////////////////////////
// DLL Functions for explizit DLL Binding
CCDatabase* createDatabaseConnection(char* server, char* user, char* pass, char* db) {
return (CCDatabase*) new CCMySQL(server, user, pass, db);
}
void freeDatabaseConnection(CCDatabase* connection) {
CCMySQL* con = (CCMySQL*)(void*)connection;
delete con;
}
void DBEscapeString(char* out, char* in, size_t len) {
::mysql_escape_string(out, in, len);
}