bool DBcore::DoQuery_locked(DBerror &err, const char *query, int32 querylen, bool retry) { if (pStatus != Connected) Open_locked(); if (mysql_real_query(&mysql, query, querylen)) { int num = mysql_errno(&mysql); if (num == CR_SERVER_GONE_ERROR) pStatus = Error; if (retry && (num == CR_SERVER_LOST || num == CR_SERVER_GONE_ERROR)) { sLog.Error("DBCore", "Lost connection, attempting to recover...."); return DoQuery_locked(err, query, querylen, false); } pStatus = Error; err.SetError(num, mysql_error(&mysql)); sLog.Error("DBCore Query", "#%d in '%s': %s", err.GetErrNo(), query, err.c_str()); return false; } err.ClearError(); return true; }
bool DBcore::RunQuery(const char* query, int32 querylen, char* errbuf, MYSQL_RES** result, int32* affected_rows, int32* last_insert_id, int32* errnum, bool retry) { if (errnum) *errnum = 0; if (errbuf) errbuf[0] = 0; MutexLock lock(MDatabase); DBerror err; if(!DoQuery_locked(err, query, querylen, retry)) { sLog.Error("DBCore Query", "Query: %s failed", query); if(errnum != NULL) *errnum = err.GetErrNo(); /* @note possible buffer overflow because the size of 'errbuf' is unknown. * @todo check if this function is actualy used and of so... change the strcpy to strncpy. */ if(errbuf != NULL) strcpy(errbuf, err.c_str()); return false; } if (result) { if(mysql_field_count(&mysql)) { *result = mysql_store_result(&mysql); } else { *result = NULL; if (errnum) *errnum = UINT_MAX; /* @note possible buffer overflow because the size of 'errbuf' is unknown. * @todo check if this function is actualy used and of so... change the strcpy to strncpy. */ if (errbuf) strcpy(errbuf, "DBcore::RunQuery: No Result"); sLog.Error("DBCore Query", "Query: %s failed because it should return a result", query); return false; } } if (affected_rows) *affected_rows = (uint32)mysql_affected_rows(&mysql); if (last_insert_id) *last_insert_id = (uint32)mysql_insert_id(&mysql); return true; }