library/src/databases/bpdb/query_helpers/BP-DBSelectQueryHelpers.cc
// Adds a join part to the select settings based on input and type.  Performs sanitization and
// corruption detection before adding.
Adds a join part to the select settings based on input and type.  Performs sanitization and
corruption detection before adding.
Adds a join part to the select settings based on input and type . Performs sanitization and corruption detection before adding .
BP_ERROR_T BP_DBSyntaxGenSelectInfoAddJoinPart
(
	P_BPDB_SQLGEN         syngen,
	BPDB_SELECT_JOIN_TYPE type,
	char *                table_name,
	char *                front_join,
	char *                comparison_operator,
	char *                tail_join
)
{
	if(!syngen)
	{
		return ERR_FAILURE;
	}
	if(!syngen->select_info)
		return ERR_FAILURE;
	if(!syngen->select_info->select_query_parts_hreg)
		return ERR_FAILURE;
	if(!syngen->bpdb)
	{
		return ERR_FAILURE;
	}
	if(!type)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with an unset type parameter.");
		return ERR_FAILURE;
	}
	if(!table_name)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with a null table name.");
		return ERR_FAILURE;
	}
	if(!front_join)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with a null front join.");
		return ERR_FAILURE;
	}
	if(!comparison_operator)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with a null comparison operator.");
		return ERR_FAILURE;
	}
	if(!tail_join)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with a null tail join.");
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonableForTableNameDB(table_name, bpstrlen(table_name)))
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with an unreasonable talbe name (bad characters).");
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonableForSelectFieldSpecifierDB(front_join, bpstrlen(front_join)))
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with an unreasonable talbe name (bad characters).");
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonableForMariadbComparisonOperatorDB(comparison_operator, bpstrlen(comparison_operator)))
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with an unreasonable comparison operator (bad characters).");
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonablePrintableString(tail_join, bpstrlen(tail_join)))
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was called with an unreasonable tail join (bad characters).");
		return ERR_FAILURE;
	}
	size_t clause_entry_count = BP_HashRegCountEntriesInTableByPathQuery
	(
		syngen->select_info->select_query_parts_hreg,
		"/join_clauses",
		BP_HASH_TABLE_COUNT_POLICY_COUNT_ONLY_TABLE_ENTRIES
	);
	char clause_position[256];
	BP_ZERO_STACK(clause_position);
	sprintf((char *) &clause_position, "join_clause_%zu", clause_entry_count);
	char *table_path[] =
	{
		"join_clauses",
		(char *) &clause_position,
		NULL
	};
	BP_HashRegAddTableByPath
	(
		syngen->select_info->select_query_parts_hreg,
		(char **) &table_path
	);
	P_BP_HTKE table_lookup = BP_HashRegLookupTableByPath
	(
		syngen->select_info->select_query_parts_hreg,
		(char **) &table_path
	);
	if(!table_lookup)
	{
		return ERR_FAILURE;
	}
	char *lookup_path = BP_HashRegEntryToPathQuery(table_lookup);
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"type",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T,
			(char *) &type,
			sizeof(size_t)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was unable to add value_type parameter.");
		return ERR_FAILURE;
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"table_name",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
			table_name,
			bpstrlen(table_name)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was unable to add table_name parameter.");
		return ERR_FAILURE;
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"front_join",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
			front_join,
			bpstrlen(front_join)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was unable to add table_name parameter.");
		return ERR_FAILURE;
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"comparison_operator",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
			comparison_operator,
			bpstrlen(comparison_operator)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was unable to add table_name parameter.");
		return ERR_FAILURE;
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"tail_join",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
			tail_join,
			bpstrlen(tail_join)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddJoinPart was unable to add tail_join parameter.");
		return ERR_FAILURE;
	}
	bpfree(lookup_path);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_DBSyntaxGenSelectInfoAddJoinPart ( P_BPDB_SQLGEN syngen , BPDB_SELECT_JOIN_TYPE type , char * table_name , char * front_join , char * comparison_operator , char * tail_join ) { if ( ! syngen ) { return ERR_FAILURE ; } if ( ! syngen -> select_info ) return ERR_FAILURE ; if ( ! syngen -> select_info -> select_query_parts_hreg ) return ERR_FAILURE ; if ( ! syngen -> bpdb ) { return ERR_FAILURE ; } if ( ! type ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! table_name ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! front_join ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! comparison_operator ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! tail_join ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonableForTableNameDB ( table_name , bpstrlen ( table_name ) ) ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonableForSelectFieldSpecifierDB ( front_join , bpstrlen ( front_join ) ) ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonableForMariadbComparisonOperatorDB ( comparison_operator , bpstrlen ( comparison_operator ) ) ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonablePrintableString ( tail_join , bpstrlen ( tail_join ) ) ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } size_t clause_entry_count = BP_HashRegCountEntriesInTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , " " , BP_HASH_TABLE_COUNT_POLICY_COUNT_ONLY_TABLE_ENTRIES ) ; char clause_position [ 256 ] ; BP_ZERO_STACK ( clause_position ) ; sprintf ( ( char * ) & clause_position , " " , clause_entry_count ) ; char * table_path [ ] = { " " , ( char * ) & clause_position , NULL } ; BP_HashRegAddTableByPath ( syngen -> select_info -> select_query_parts_hreg , ( char * * ) & table_path ) ; P_BP_HTKE table_lookup = BP_HashRegLookupTableByPath ( syngen -> select_info -> select_query_parts_hreg , ( char * * ) & table_path ) ; if ( ! table_lookup ) { return ERR_FAILURE ; } char * lookup_path = BP_HashRegEntryToPathQuery ( table_lookup ) ; if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T , ( char * ) & type , sizeof ( size_t ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , table_name , bpstrlen ( table_name ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , front_join , bpstrlen ( front_join ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , comparison_operator , bpstrlen ( comparison_operator ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , tail_join , bpstrlen ( tail_join ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } bpfree ( lookup_path ) ; return ERR_SUCCESS ; }
Adds a join part to the select settings based on input and type.
Adds a join part to the select settings based on input and type .
// run basic checks // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Run String Checks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // BPDB_SELECT_JOIN_TYPE type, // char *table_name, // char *front_join, // char *comparison_operator, // char *tail_join // check table name // check front join (should always be a field specifier) // check front join (should always be a field specifier) // check tail join (can be roughly any value, validated during query) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Clause Table %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // count the clauses // create clause position // setup table add path // add table path // attempt to lookup table // exit if the table was not added ok // create lookup path // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Add Fields after Checks Complete %%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // BPDB_SELECT_JOIN_TYPE type, // char *table_name, // char *front_join, // char *comparison_operator, // char *tail_join // attempt to add value type // attempt to add table_name // attempt to add table_name // attempt to add table_name // attempt to add tail_join // destroy the lookup path // return indicating success