// *************************************************************** // Function: GWLPDatabaseUpdate // *************************************************************** #include "GWLPDatabase.h" #define TI ((THREAD_INFO*)pParam) //************************************ // Name: QueryThread // // Query execution function for threads. // // Parameters: // pParam - THREAD_INFO* // // Return: // //************************************ unsigned long __stdcall QueryThread(void* pParam) { nlMutexLock(TI->pMutex); //Perform the query. Try again on first failure. After first failure exit thread with fail code. bool failed=false; int i=0; for (i; i<2; i++) { if (mysql_real_query(TI->pDb, TI->pQuery, TI->queryLength)!=0) { failed=true; } if (!failed) break; } TI->numAffected=(long)mysql_affected_rows(TI->pDb); //The query failed from some other reason if (i==2) { //Find out what our error is and make a copy and store it char* error=(char*)mysql_error(TI->pDb); TI->pErrorMessage=new char[strlen(error)+1]; memcpy(TI->pErrorMessage, error, strlen(error)+1); nlMutexUnlock(TI->pMutex); ExitThread(-1); } //Store the result set. Will be NULL if there is none. TI->pResult=mysql_store_result(TI->pDb); nlMutexUnlock(TI->pMutex); return 0; } //************************************ // Name: Update // // Processes the queues and adds results to the result queue when needed. This function should be called once per loop. // // Parameters: // // Return: // //************************************ void GWLPDatabase::Update() { if (true) { //Process the queue list::iterator itr; for (itr=mQueue.begin(); itr!=mQueue.end(); itr++) { //Figure out what db this query needs to go to for (unsigned int i=0; iboundRule) this_one=true; else if (mPipes[i].pipeRuleSet!=itr->boundRule && mPipes[i].threads.size()referenceId=itr->referenceId; thread->pResult=0; thread->pDb=mPipes[i].pDatabase; thread->pMutex=mPipes[i].pMutex; //Set the query and query size thread->pQuery=itr->pQuery; thread->queryLength=itr->queryLength; thread->pErrorMessage=0; thread->returnStatus=itr->returnStatus; thread->showStatus=itr->showStatus; //Create the thread unsigned long threadID2; #ifdef WIN32 thread->pThreadHandle=CreateThread(NULL, 0, QueryThread, thread, 0, &threadID2); #endif //If thread was not created. Log an error if (thread->pThreadHandle==NULL) { printf("Query Thread Creation Failed! Query: %s\n", thread->pQuery); itr->deleteMe=true; } else { itr->deleteMe=true; mPipes[i].threads.push_back(thread); } break; } } } whymustidothis: //Delete queries that are already in a thread for (itr=mQueue.begin(); itr!=mQueue.end(); itr++) { if (itr->deleteMe) { mQueue.erase(itr); goto whymustidothis; } } } //Process the thread list for (unsigned int i=0; i::iterator itr; for (itr=mPipes[i].threads.begin(); itr!=mPipes[i].threads.end(); itr++) { //Check if the thread is still running unsigned long exit_code; if (!GetExitCodeThread((*itr)->pThreadHandle, &exit_code)) { printf("Failed to retrieve exit code! Code #%d\n", GetLastError()); } else { if (exit_code==STILL_ACTIVE){} //There was no error else if (exit_code==0) { this->mQueriesProcessed++; if (this->mQueriesProcessed>4000000000){this->mQueriesProcessed=0;} //If no result was returned if ((*itr)->pResult==NULL) { //Check for possibly bad query if ((*itr)->numAffected==0 && (*itr)->showStatus) { printf("Query had no effect: %s\n", (*itr)->pQuery); printf("There is no error associated with this. This message indicates that the query affected 0 rows.\n"); } //If the calling application has specified it wants the status of this query, add the status to the result list if ((*itr)->returnStatus) { QUERY_RESULT g; g.referenceID=(*itr)->referenceId; g.pResult=0; g.affectedRows=(*itr)->numAffected; typedef pair gpair; mResults.insert(gpair(g.referenceID, g)); } if ((*itr)->pQuery!=NULL){delete [] (*itr)->pQuery;} CloseHandle((*itr)->pThreadHandle); delete (*itr); mPipes[i].threads.erase(itr); goto goagain; } //If a result was returned o_0 else { if ((*itr)->pQuery!=NULL){delete [] (*itr)->pQuery;} QUERY_RESULT g; g.referenceID=(*itr)->referenceId; g.pResult=(*itr)->pResult; g.affectedRows=-1; CloseHandle((*itr)->pThreadHandle); delete (*itr); mPipes[i].threads.erase(itr); typedef pair gpair; mResults.insert(gpair(g.referenceID, g)); goto goagain; } } //There was an error else { printf("Query Failed: %s\n", (*itr)->pQuery); printf("Error: %s\n", (*itr)->pErrorMessage); delete [] (*itr)->pErrorMessage; CloseHandle((*itr)->pThreadHandle); QUERY_RESULT g; g.referenceID=(*itr)->referenceId; g.pResult=NULL; g.affectedRows=-1; typedef pair gpair; mResults.insert(gpair(g.referenceID, g)); delete (*itr); mPipes[i].threads.erase(itr); goto goagain; } } } } }