library/src/databases/bpdb/query_helpers/BP-DBSelectQueryHelpers.cc
// Adds a where clause to the select settings based on input and type.  Performs sanitization and
// corruption detection before adding.
Adds a where clause to the select settings based on input and type.  Performs sanitization and
corruption detection before adding.
Adds a where clause to the select settings based on input and type . Performs sanitization and corruption detection before adding .
BP_ERROR_T BP_DBSyntaxGenSelectInfoAddWhereClause
(
		P_BPDB_SQLGEN                syngen,
		BPDB_SELECT_WHERE_VALUE_TYPE type,
		char *                       conjunction,
		char *                       field_name,
		char *                       limiter,
		char *                       value,
		size_t                       value_len
)
{
	if(!syngen)
	{
		return ERR_FAILURE;
	}
	if(!syngen->bpdb)
	{
		return ERR_FAILURE;
	}
	if(!syngen->select_info)
		return ERR_FAILURE;
	if(!syngen->select_info->select_query_parts_hreg)
		return ERR_FAILURE;
	if(!type)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with an unset type parameter.");
		return ERR_FAILURE;
	}
	if(!field_name)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with a null field name.");
		return ERR_FAILURE;
	}
	if(!limiter)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with a null limiter parameter.");
		return ERR_FAILURE;
	}
	if(!value)
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with a null value parameter.");
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonableForSelectFieldSpecifierDB(field_name, bpstrlen(field_name)))
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with an unreasonable field name (bad characters).");
		return ERR_FAILURE;
	}
	if(!BP_StringIsReasonableForMariadbComparisonOperatorDB(limiter, bpstrlen(limiter)))
	{
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with an unreasonable limiter parameter (bad characters).");
		return ERR_FAILURE;
	}
	if(conjunction)
	{
		if(!BP_StringIsReasonableForMariadbConjunctionDB(conjunction, bpstrlen(conjunction)))
		{
			BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with an unreasonable conjunction parameter (bad characters).");
			return ERR_FAILURE;
		}
	}
	size_t clause_entry_count = BP_HashRegCountEntriesInTableByPathQuery
	(
		syngen->select_info->select_query_parts_hreg,
		"/where_clauses",
		BP_HASH_TABLE_COUNT_POLICY_COUNT_ONLY_TABLE_ENTRIES
	);
	char clause_position[256];
	BP_ZERO_STACK(clause_position);
	sprintf((char *) &clause_position, "where_clause_%zu", clause_entry_count);
	char *table_path[] =
	{
		"where_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(conjunction)
	{
		if
		(
			!BP_HashRegAddDataToTableByPathQuery
			(
				syngen->select_info->select_query_parts_hreg,
				lookup_path,
				"conjunction",
				BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
				conjunction,
				bpstrlen(conjunction)
			)
		)
		{
			bpfree(lookup_path);
			BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add conjunction parameter.");
			return ERR_FAILURE;
		}
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"field_name",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
			field_name,
			bpstrlen(field_name)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add field_name parameter.");
		return ERR_FAILURE;
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"limiter",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
			limiter,
			bpstrlen(limiter)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add limiter parameter.");
		return ERR_FAILURE;
	}
	size_t value_type = type;
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"value_type",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T,
			(char *) &value_type,
			sizeof(size_t)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add value_type parameter.");
		return ERR_FAILURE;
	}
	if
	(
		!BP_HashRegAddDataToTableByPathQuery
		(
			syngen->select_info->select_query_parts_hreg,
			lookup_path,
			"value_len",
			BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T,
			(char *) &value_len,
			sizeof(size_t)
		)
	)
	{
		bpfree(lookup_path);
		BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add value_type parameter.");
		return ERR_FAILURE;
	}
	%%%%%%
	%%%%%%
	char tmp_integer_buff[512];
	BP_ZERO_STACK(tmp_integer_buff);
	long int * tmp_long_int_ptr = NULL;
	switch(type)
	{
		case BPDB_SELECT_WHERE_VALUE_TYPE_TABLE_AND_COLUMN:
		case BPDB_SELECT_WHERE_VALUE_TYPE_QUOTED_LITERAL:
			if(!BP_StringIsReasonablePrintableString(value, bpstrlen(value)))
			{
				bpfree(lookup_path);
				BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was called with an unreasonable value parameter (bad characters in BPDB_SELECT_WHERE_VALUE_TYPE_QUOTED_LITERAL or BPDB_SELECT_WHERE_VALUE_TYPE_TABLE_AND_COLUMN type).");
				return ERR_FAILURE;
			}
			if
			(
				!BP_HashRegAddDataToTableByPathQuery
				(
					syngen->select_info->select_query_parts_hreg,
					lookup_path,
					"value",
					BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
					value,
					bpstrlen(value)
				)
			)
			{
				bpfree(lookup_path);
				BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add value parameter.");
				return ERR_FAILURE;
			}
			break;
		case BPDB_SELECT_WHERE_VALUE_TYPE_HEX_STRING:
			if
			(
				!BP_HashRegAddDataToTableByPathQuery
				(
					syngen->select_info->select_query_parts_hreg,
					lookup_path,
					"value",
					BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB,
					value,
					value_len
				)
			)
			{
				bpfree(lookup_path);
				BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add value parameter (BPDB_SELECT_WHERE_VALUE_TYPE_HEX_STRING).");
				return ERR_FAILURE;
			}
			break;
		case BPDB_SELECT_WHERE_VALUE_TYPE_POSITIVE_INTEGER:
			tmp_long_int_ptr = (long int *) value;
			printf("\n GOT POSITIVE INTEGER TYPE?!: %li", *tmp_long_int_ptr);
			sprintf((char *) &tmp_integer_buff, "%li", *tmp_long_int_ptr);
			if
			(
				!BP_HashRegAddDataToTableByPathQuery
				(
					syngen->select_info->select_query_parts_hreg,
					lookup_path,
					"value",
					BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
					value,
					value_len
				)
			)
			{
				bpfree(lookup_path);
				BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add value parameter.");
				return ERR_FAILURE;
			}
			break;
		case BPDB_SELECT_WHERE_VALUE_TYPE_NEGATIVE_INTEGER:
			tmp_long_int_ptr = (long int *) value;
			sprintf((char *) &tmp_integer_buff, "%li", *tmp_long_int_ptr);
			if
			(
				!BP_HashRegAddDataToTableByPathQuery
				(
					syngen->select_info->select_query_parts_hreg,
					lookup_path,
					"value",
					BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING,
					value,
					value_len
				)
			)
			{
				bpfree(lookup_path);
				BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was unable to add value parameter.");
				return ERR_FAILURE;
			}
			break;
		default:
			BP_DBAddErrorMsg(syngen->bpdb, "BP_DBSyntaxGenSelectInfoAddWhereClause was passed in a bad type.  Conversion failed.");
			break;
	}
	bpfree(lookup_path);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_DBSyntaxGenSelectInfoAddWhereClause ( P_BPDB_SQLGEN syngen , BPDB_SELECT_WHERE_VALUE_TYPE type , char * conjunction , char * field_name , char * limiter , char * value , size_t value_len ) { if ( ! syngen ) { return ERR_FAILURE ; } if ( ! syngen -> bpdb ) { return ERR_FAILURE ; } if ( ! syngen -> select_info ) return ERR_FAILURE ; if ( ! syngen -> select_info -> select_query_parts_hreg ) return ERR_FAILURE ; if ( ! type ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! field_name ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! limiter ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! value ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonableForSelectFieldSpecifierDB ( field_name , bpstrlen ( field_name ) ) ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( ! BP_StringIsReasonableForMariadbComparisonOperatorDB ( limiter , bpstrlen ( limiter ) ) ) { BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } if ( conjunction ) { if ( ! BP_StringIsReasonableForMariadbConjunctionDB ( conjunction , bpstrlen ( conjunction ) ) ) { 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 ( conjunction ) { if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , conjunction , bpstrlen ( conjunction ) ) ) { 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 , field_name , bpstrlen ( field_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 , limiter , bpstrlen ( limiter ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } size_t value_type = type ; if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_SIZE_T , ( char * ) & value_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_SIZE_T , ( char * ) & value_len , sizeof ( size_t ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } char tmp_integer_buff [ 512 ] ; BP_ZERO_STACK ( tmp_integer_buff ) ; long int * tmp_long_int_ptr = NULL ; switch ( type ) { case BPDB_SELECT_WHERE_VALUE_TYPE_TABLE_AND_COLUMN : case BPDB_SELECT_WHERE_VALUE_TYPE_QUOTED_LITERAL : if ( ! BP_StringIsReasonablePrintableString ( value , bpstrlen ( value ) ) ) { 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 , value , bpstrlen ( value ) ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } break ; case BPDB_SELECT_WHERE_VALUE_TYPE_HEX_STRING : if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB , value , value_len ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } break ; case BPDB_SELECT_WHERE_VALUE_TYPE_POSITIVE_INTEGER : tmp_long_int_ptr = ( long int * ) value ; printf ( " \n " , * tmp_long_int_ptr ) ; sprintf ( ( char * ) & tmp_integer_buff , " " , * tmp_long_int_ptr ) ; if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , value , value_len ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } break ; case BPDB_SELECT_WHERE_VALUE_TYPE_NEGATIVE_INTEGER : tmp_long_int_ptr = ( long int * ) value ; sprintf ( ( char * ) & tmp_integer_buff , " " , * tmp_long_int_ptr ) ; if ( ! BP_HashRegAddDataToTableByPathQuery ( syngen -> select_info -> select_query_parts_hreg , lookup_path , " " , BP_HASH_TABLE_KEYED_ENTRY_TYPE_UTF8_STRING , value , value_len ) ) { bpfree ( lookup_path ) ; BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; return ERR_FAILURE ; } break ; default : BP_DBAddErrorMsg ( syngen -> bpdb , " " ) ; break ; } bpfree ( lookup_path ) ; return ERR_SUCCESS ; }
Adds a where clause to the select settings based on input and type.
Adds a where clause to the select settings based on input and type .
// run basic checks // set select info // run reasonable-ness checks. // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% 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 %%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // attempt to add conjunction // attempt to add field_name // attempt to add limiter // set value type // attempt to add value type // attempt to add value length // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Parse Values Based on Type %%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create buffer to hold integer conversion buffers // this is used to dereference long integer values // switch on the type // add direct values // string values must always be reasonable/printable // set hex value // add positive integer // add negative integer // destroy the lookup path // return indicating success