library/src/blockclass/BP-BlockClass.cc
// Retrieve a result from the database using a like query constraint.  This is unfiltered
// sql, so be careful for untrusted input.  This facility is provided under trusted grounds,
// so you should take that into consideration before implementing its use under dynamic conditions.
Retrieve a result from the database using a like query constraint.  This is unfiltered
sql, so be careful for untrusted input.  This facility is provided under trusted grounds,
so you should take that into consideration before implementing its use under dynamic conditions.
Retrieve a result from the database using a like query constraint . This is unfiltered sql so be careful for untrusted input . This facility is provided under trusted grounds so you should take that into consideration before implementing its use under dynamic conditions .
BP_ERROR_T Block::retrieveResultFromDbHrefLike(char *like, bool unspidered, bool unaudited, size_t retrieve_order)
{
	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);
	const char * base_query      = (char *) "SELECT id, href FROM results where href LIKE ?";
	const char * status_clause   = (char *) " and status=0 ";
	const char * spidered_clause = (char *) " and spidered=0 ";
	const char * clause_end      = (char *) "ORDER BY RAND() LIMIT 1";
	char builder_query[512] = {0};
	snprintf(builder_query, 510, "%s%s%s%s",base_query,	unaudited ? status_clause : " ",	unspidered ? spidered_clause : " ",	clause_end);
	size_t query_size = bpstrlen(builder_query);
	if (this->mysqlPrepareQuery(builder_query) == ERR_FAILURE)
	{
		this->AddErrorToStack((char*) "Error in retrieveResultFromDb: Cannot prepare query.");
		BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_FAILURE);
	}
	this->mysqlAddParameter(MYSQL_TYPE_STRING, like, bpstrlen(like));
	this->mysqlBindParameters();
	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)
		this->AddErrorToStack((char*) "\n Cannot add result");
	if (this->mysqlBindResults() == ERR_FAILURE)
		this->AddErrorToStack((char*) "Cannot bind results");
	if (this->mysqlExecuteStatement() == ERR_FAILURE)
	{
		this->AddErrorToStack("Error: retrieveResultFromDbHrefLike failed to execute mysql statement.");
	}
	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);
	if(result_href)
	{
		tq_free(this->tq, result_href);
	}
	BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(ERR_SUCCESS);
}
BP_ERROR_T Block :: retrieveResultFromDbHrefLike ( char * like , bool unspidered , bool unaudited , size_t retrieve_order ) { 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 ) ; const char * base_query = ( char * ) " " ; const char * status_clause = ( char * ) " " ; const char * spidered_clause = ( char * ) " " ; const char * clause_end = ( char * ) " " ; char builder_query [ 512 ] = { 0 } ; snprintf ( builder_query , 510 , " " , base_query , unaudited ? status_clause : " " , unspidered ? spidered_clause : " " , clause_end ) ; size_t query_size = bpstrlen ( builder_query ) ; if ( this -> mysqlPrepareQuery ( builder_query ) == ERR_FAILURE ) { this -> AddErrorToStack ( ( char * ) " " ) ; BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_FAILURE ) ; } this -> mysqlAddParameter ( MYSQL_TYPE_STRING , like , bpstrlen ( like ) ) ; this -> mysqlBindParameters ( ) ; 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 ) this -> AddErrorToStack ( ( char * ) " \n " ) ; if ( this -> mysqlBindResults ( ) == ERR_FAILURE ) this -> AddErrorToStack ( ( char * ) " " ) ; if ( this -> mysqlExecuteStatement ( ) == ERR_FAILURE ) { this -> AddErrorToStack ( " " ) ; } 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 ) ; if ( result_href ) { tq_free ( this -> tq , result_href ) ; } BP_BLOCK_MARIADB_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( ERR_SUCCESS ) ; }
Retrieve a result from the database using a like query constraint.
Retrieve a result from the database using a like query constraint .
// require valid mysql session // enter critical section // result index // set base query (prepared statement) // dynamic (non-adhoc) sql generation // append base query length // prepare the query // add href like parameter // add index result // add href buffer result // run lookup query on url to make sure it doesn't already exist // execute the statement // store results // retch a result (max one result for this query) // destroy statement // NOTABLE DATA STORAGE: store result index/href identifier // NOTABLE DATA STORAGE: store result href data // destroy tmp variable on exit // return indicating success