library/src/blockclass/BP-BlockClass.cc
// Retrieve a random target from the db
Retrieve a random target from the db
Retrieve a random target from the db
BP_ERROR_T Block::retrieveResultFromDb(char *where_clause)
{
	if (!this->mysqlSession)
	{
		this->AddErrorToStack((char*) "Error in retrieveResultFromDb (NULL Mysql session");
		return ERR_FAILURE;
	}
	BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION;
	size_t result_index = 0;
	char * result_href = (char *) tq_calloc(this->tq, 2048, 1, BPLN);
	char * base_query = (char *) "SELECT id, href FROM results where %s and status=0 ORDER BY RAND() LIMIT 1";
	if (!where_clause)
		where_clause = (char *) " ";
	size_t query_size = 0;
	query_size += bpstrlen(where_clause);
	query_size += bpstrlen(base_query);
	char *query = (char *) tq_calloc(this->tq, query_size, 1, BPLN);
	snprintf(query, query_size - 1, base_query, where_clause);
	if (this->mysqlPrepareQuery(query) == ERR_FAILURE) {
		this->AddErrorToStack((char*) "Error in retrieveResultFromDb: Cannot prepare query.");
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	if (this->mysqlAddResult(MYSQL_TYPE_LONG, (char *) &result_index, sizeof(result_index)) == ERR_FAILURE)
		printf("\n Cannot add result");
	if (this->mysqlAddResult(MYSQL_TYPE_STRING, (char *) result_href, 2047) == ERR_FAILURE)
		printf("\n Cannot add result");
	if (this->mysqlBindResults() == ERR_FAILURE)
		printf("\n Cannot bind results");
	if (this->mysqlExecuteStatement() == ERR_FAILURE)
	{
	}
	this->mysqlStoreResults();
	this->mysqlFetchResult();
	this->mysqlDestroyStatement();
	this->web_result_id = result_index;
	snprintf((char *) &this->web_result_href, sizeof(this->web_result_href)-1, "%s", result_href);
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_SUCCESS);
}
BP_ERROR_T Block :: retrieveResultFromDb ( char * where_clause ) { if ( ! this -> mysqlSession ) { this -> AddErrorToStack ( ( char * ) " " ) ; return ERR_FAILURE ; } BP_BLOCK_MARIADB_ENTER_CRITICAL_SECTION ; size_t result_index = 0 ; char * result_href = ( char * ) tq_calloc ( this -> tq , 2048 , 1 , BPLN ) ; char * base_query = ( char * ) " " ; if ( ! where_clause ) where_clause = ( char * ) " " ; size_t query_size = 0 ; query_size += bpstrlen ( where_clause ) ; query_size += bpstrlen ( base_query ) ; char * query = ( char * ) tq_calloc ( this -> tq , query_size , 1 , BPLN ) ; snprintf ( query , query_size - 1 , base_query , where_clause ) ; if ( this -> mysqlPrepareQuery ( query ) == ERR_FAILURE ) { this -> AddErrorToStack ( ( char * ) " " ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } if ( this -> mysqlAddResult ( MYSQL_TYPE_LONG , ( char * ) & result_index , sizeof ( result_index ) ) == ERR_FAILURE ) printf ( " \n " ) ; if ( this -> mysqlAddResult ( MYSQL_TYPE_STRING , ( char * ) result_href , 2047 ) == ERR_FAILURE ) printf ( " \n " ) ; if ( this -> mysqlBindResults ( ) == ERR_FAILURE ) printf ( " \n " ) ; if ( this -> mysqlExecuteStatement ( ) == ERR_FAILURE ) { } this -> mysqlStoreResults ( ) ; this -> mysqlFetchResult ( ) ; this -> mysqlDestroyStatement ( ) ; this -> web_result_id = result_index ; snprintf ( ( char * ) & this -> web_result_href , sizeof ( this -> web_result_href ) - 1 , " " , result_href ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_SUCCESS ) ; }
Retrieve a random target from the db
Retrieve a random target from the db
// require valid mysql session // enter critical section // result index // set blank where clause if its null // append where clause length // append base query length // allocate query size // prepare the query // add index // add href buffer // run lookup query on url to make sure it doesn't already exist // execute the statement // printf("\n Cannot execute statement."); // store results // retch a result (max one result for this query) // destroy statement // store result index/href // return indicating success