Beispiel #1
0
MySQLRequestResult DBcore::QueryDatabase(const char* query, uint32 querylen, bool retryOnFailureOnce)
{
	LockMutex lock(&MDatabase);

	// Reconnect if we are not connected before hand.
	if (pStatus != Connected)
	{
		Open();
	}

	// request query. != 0 indicates some kind of error.
	if (mysql_real_query(&mysql, query, querylen) != 0)
	{
		unsigned int errorNumber = mysql_errno(&mysql);

		if (errorNumber == CR_SERVER_GONE_ERROR)
		{
			pStatus = Error;
		}

		// error appears to be a disconnect error, may need to try again.
		if (errorNumber == CR_SERVER_LOST || errorNumber == CR_SERVER_GONE_ERROR)
		{
			if (retryOnFailureOnce)
			{
				std::cout << "Database Error: Lost connection, attempting to recover...." << std::endl;
				MySQLRequestResult requestResult = QueryDatabase(query, querylen, false);

				if (requestResult.Success())
				{
					std::cout << "Reconnection to database successful." << std::endl;
					return requestResult;
				}

			}
			pStatus = Error;
			char *errorBuffer = new char[MYSQL_ERRMSG_SIZE];
			snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql));
			return MySQLRequestResult(nullptr, 0, 0, 0, 0, (uint32)mysql_errno(&mysql), errorBuffer);
		}
		char *errorBuffer = new char[MYSQL_ERRMSG_SIZE];
		snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql));
		return MySQLRequestResult(nullptr, 0, 0, 0, 0, mysql_errno(&mysql),errorBuffer);
	}

	// successful query. get results.
	MYSQL_RES* res = mysql_store_result(&mysql);
	uint32 rowCount = 0;

	if (res != nullptr)
	{
		rowCount = (uint32)mysql_num_rows(res);
	}
	MySQLRequestResult requestResult(res, (uint32)mysql_affected_rows(&mysql), rowCount, (uint32)mysql_field_count(&mysql), (uint32)mysql_insert_id(&mysql));
	return requestResult;
}
Beispiel #2
0
MySQLRequestResult DBcore::QueryDatabase(const char* query, uint32 querylen, bool retryOnFailureOnce)
{
	LockMutex lock(&MDatabase);

	// Reconnect if we are not connected before hand.
	if (pStatus != Connected)
		Open();

	// request query. != 0 indicates some kind of error.
	if (mysql_real_query(&mysql, query, querylen) != 0)
	{
		unsigned int errorNumber = mysql_errno(&mysql);

		if (errorNumber == CR_SERVER_GONE_ERROR)
			pStatus = Error;

		// error appears to be a disconnect error, may need to try again.
		if (errorNumber == CR_SERVER_LOST || errorNumber == CR_SERVER_GONE_ERROR)
		{

			if (retryOnFailureOnce)
			{
				std::cout << "Database Error: Lost connection, attempting to recover...." << std::endl;
				MySQLRequestResult requestResult = QueryDatabase(query, querylen, false);

				if (requestResult.Success())
				{
					std::cout << "Reconnection to database successful." << std::endl;
					return requestResult;
				}

			}

			pStatus = Error;

			auto errorBuffer = new char[MYSQL_ERRMSG_SIZE];

			snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql));

			return MySQLRequestResult(nullptr, 0, 0, 0, 0, (uint32)mysql_errno(&mysql), errorBuffer);
		}

		auto errorBuffer = new char[MYSQL_ERRMSG_SIZE];
		snprintf(errorBuffer, MYSQL_ERRMSG_SIZE, "#%i: %s", mysql_errno(&mysql), mysql_error(&mysql));

		/* Implement Logging at the Root */
		if (mysql_errno(&mysql) > 0 && strlen(query) > 0){
			if (LogSys.log_settings[Logs::MySQLError].is_category_enabled == 1)
				Log(Logs::General, Logs::MySQLError, "%i: %s \n %s", mysql_errno(&mysql), mysql_error(&mysql), query);
		}

		return MySQLRequestResult(nullptr, 0, 0, 0, 0, mysql_errno(&mysql),errorBuffer);

	}

	// successful query. get results.
	MYSQL_RES* res = mysql_store_result(&mysql);
	uint32 rowCount = 0;

	if (res != nullptr)
        rowCount = (uint32)mysql_num_rows(res);

	MySQLRequestResult requestResult(res, (uint32)mysql_affected_rows(&mysql), rowCount, (uint32)mysql_field_count(&mysql), (uint32)mysql_insert_id(&mysql));
	
	if (LogSys.log_settings[Logs::MySQLQuery].is_category_enabled == 1)
	{
		if ((strncasecmp(query, "select", 6) == 0)) {
			Log(Logs::General, Logs::MySQLQuery, "%s (%u row%s returned)", query, requestResult.RowCount(), requestResult.RowCount() == 1 ? "" : "s");
		}
		else {
			Log(Logs::General, Logs::MySQLQuery, "%s (%u row%s affected)", query, requestResult.RowsAffected(), requestResult.RowsAffected() == 1 ? "" : "s");
		}
	}

	return requestResult;
}