library/src/databases/bpdb/BP-DBSQLSyntaxGenerators.cc
// Checks a field name list and make sure elements match up
Checks a field name list and make sure elements match up
Checks a field name list and make sure elements match up
BP_ERROR_T BP_DBCheckSelectFieldNameList
(
	P_BPDB     bpdb,
	P_BP_LINKL field_name_list,
	P_BP_LINKL table_name_list
)
{
	if(!bpdb)
		return ERR_FAILURE;
	if(!field_name_list)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBCheckSelectFieldNameList was called with a NULL field_name_list parameter.");
		return ERR_FAILURE;
	}
	if(!field_name_list->total_allocations)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBCheckSelectFieldNameList was called with field_name_list->total_allocations == 0.");
		return ERR_FAILURE;
	}
	if(!table_name_list)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBCheckSelectFieldNameList was called with a NULL table_name_list parameter.");
		return ERR_FAILURE;
	}
	if(!table_name_list->total_allocations)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBCheckSelectFieldNameList was called with table_name_list->total_allocations == 0.");
		return ERR_FAILURE;
	}
	P_BP_LINKL_DATA field_name_iter = TAILQ_FIRST(&field_name_list->head);
	if(!field_name_iter)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBCheckSelectFieldNameList attempted to find a first element within the first_name_list, and failed.");
		return ERR_FAILURE;
	}
	size_t field_name_list_idx = 0;
	TAILQ_FOREACH(field_name_iter, &field_name_list->head, linkl_data)
	{
		if
		(
			field_name_iter->data_type != BP_MEMPROF_ALLOC_TYPE_STRDUP   &&
			field_name_iter->data_type != BP_MEMPROF_ALLOC_TYPE_STRNDUP
		)
		{
			field_name_list_idx++;
			continue;
		}
		char * field_elem = (char *) field_name_iter->data;
		if(!field_elem)
			break;
		size_t field_len = bpstrlen(field_elem);
		if(!field_len)
		{
			char tmp_buff[512];
			BP_ZERO_STACK(tmp_buff);
			sprintf((char *) &tmp_buff, "BP_DBCheckSelectFieldNameList encountered an empty field (aka empty/zero-length string) at list index position %zu", field_name_list_idx);
			BP_DBAddErrorMsg(bpdb, (char *) &tmp_buff);
			return ERR_FAILURE;
		}
		BP_BOOL field_is_ok = BP_StringIsReasonableForSelectFieldSpecifierDB(field_elem, field_len);
		if(field_is_ok == BP_FALSE)
		{
			char tmp_buff[1024+bpstrlen(field_elem)];
			BP_ZERO_STACK(tmp_buff);
			sprintf((char *) &tmp_buff, "BP_DBCheckSelectFieldNameList reports '%s' failed sanity check in BP_StringIsReasonableForSelectFieldSpecifierDB", field_elem);
			BP_DBAddErrorMsg(bpdb, (char *) &tmp_buff);
			return ERR_FAILURE;
		}
		char ** table_and_field = BP_SplitBuffer(field_elem, ".");
		if(!table_and_field)
		{
			char tmp_buff[1024+bpstrlen(field_elem)];
			BP_ZERO_STACK(tmp_buff);
			sprintf((char *) &tmp_buff, "BP_DBCheckSelectFieldNameList reports '%s' could not be split into two parts (missing/bad-syntax period separator)", field_elem);
			BP_DBAddErrorMsg(bpdb, (char *) &tmp_buff);
			return ERR_FAILURE;
		}
		if(!BP_LinkLSearchForString(table_name_list, table_and_field[0], bpstrlen(table_and_field[0])))
		{
			char tmp_buff[1024+bpstrlen(table_and_field[0])];
			BP_ZERO_STACK(tmp_buff);
			sprintf((char *) &tmp_buff, "BP_DBCheckSelectFieldNameList reports that the table '%s' could not be found in table_name_list ", table_and_field[0]);
			BP_DBAddErrorMsg(bpdb, (char *) &tmp_buff);
			BP_DestroyPointerArray((void **) table_and_field, 2);
			bpfree(table_and_field);
			return ERR_FAILURE;
		}
		 display table and field
		BP_DestroyPointerArray((void **) table_and_field, 2);
		bpfree(table_and_field);
		field_name_list_idx++;
	}
	return ERR_SUCCESS;
}
BP_ERROR_T BP_DBCheckSelectFieldNameList ( P_BPDB bpdb , P_BP_LINKL field_name_list , P_BP_LINKL table_name_list ) { if ( ! bpdb ) return ERR_FAILURE ; if ( ! field_name_list ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } if ( ! field_name_list -> total_allocations ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } if ( ! table_name_list ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } if ( ! table_name_list -> total_allocations ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } P_BP_LINKL_DATA field_name_iter = TAILQ_FIRST ( & field_name_list -> head ) ; if ( ! field_name_iter ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return ERR_FAILURE ; } size_t field_name_list_idx = 0 ; TAILQ_FOREACH ( field_name_iter , & field_name_list -> head , linkl_data ) { if ( field_name_iter -> data_type != BP_MEMPROF_ALLOC_TYPE_STRDUP && field_name_iter -> data_type != BP_MEMPROF_ALLOC_TYPE_STRNDUP ) { field_name_list_idx ++ ; continue ; } char * field_elem = ( char * ) field_name_iter -> data ; if ( ! field_elem ) break ; size_t field_len = bpstrlen ( field_elem ) ; if ( ! field_len ) { char tmp_buff [ 512 ] ; BP_ZERO_STACK ( tmp_buff ) ; sprintf ( ( char * ) & tmp_buff , " " , field_name_list_idx ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_buff ) ; return ERR_FAILURE ; } BP_BOOL field_is_ok = BP_StringIsReasonableForSelectFieldSpecifierDB ( field_elem , field_len ) ; if ( field_is_ok == BP_FALSE ) { char tmp_buff [ 1024 + bpstrlen ( field_elem ) ] ; BP_ZERO_STACK ( tmp_buff ) ; sprintf ( ( char * ) & tmp_buff , " " , field_elem ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_buff ) ; return ERR_FAILURE ; } char * * table_and_field = BP_SplitBuffer ( field_elem , " " ) ; if ( ! table_and_field ) { char tmp_buff [ 1024 + bpstrlen ( field_elem ) ] ; BP_ZERO_STACK ( tmp_buff ) ; sprintf ( ( char * ) & tmp_buff , " " , field_elem ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_buff ) ; return ERR_FAILURE ; } if ( ! BP_LinkLSearchForString ( table_name_list , table_and_field [ 0 ] , bpstrlen ( table_and_field [ 0 ] ) ) ) { char tmp_buff [ 1024 + bpstrlen ( table_and_field [ 0 ] ) ] ; BP_ZERO_STACK ( tmp_buff ) ; sprintf ( ( char * ) & tmp_buff , " " , table_and_field [ 0 ] ) ; BP_DBAddErrorMsg ( bpdb , ( char * ) & tmp_buff ) ; BP_DestroyPointerArray ( ( void * * ) table_and_field , 2 ) ; bpfree ( table_and_field ) ; return ERR_FAILURE ; } BP_DestroyPointerArray ( ( void * * ) table_and_field , 2 ) ; bpfree ( table_and_field ) ; field_name_list_idx ++ ; } return ERR_SUCCESS ; }
Checks a field name list and make sure elements match up
Checks a field name list and make sure elements match up
// ensure we have a bpdb (required for error reporting) // ensure we have a field name list // ensure there are allocations in the list // ensure we have a table name list // ensure we have allocations in the list // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Walk Field Name List %%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // attempt to get the first item // ensure we have a first item // index counter // looks for a pointer in the item // only work with strndup/strdup types // set field element // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run Basic Checks %%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // break loop if the field name doesn't exist (aka null ptr) // calculate field length // exit with failure due to 0 length field (UNACCEPTABLE) // create some stack space // add message indicating that we found an empty field // add error and exit // check if the string is reasonable // if the field isn't ok, exit indicating failure // create some stack space // add message indicating that we found an empty field // add error and exit // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Separate Field from Table and Check %%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create table and field // create some stack space // add message indicating that we found an empty field // add error and exit // dbg: // printf("\n Searching for Table and Field:: %s - %s", table_and_field[0], table_and_field[1]); // ensure the table is within the list of table names // dbg: // printf("\n [!!] Failed to look up table %s", table_and_field[0]); // BP_DisplayLinkL(table_name_list); // create some stack space // add message indicating that we found an empty field // add error and exit // destroy the pointer array // destroy the table and field entry // return indicating failure // dbg: display table and field // printf("\n Got Table and Field?: %s - %s", table_and_field[0], table_and_field[1]); // destroy the pointer array // destroy the table and field entry // increment the field name list index // return indicating success