library/src/databases/bpdb/BP-DBJSON.cc
// attempt to fetch next result from JSON provided a lookup path query (eg. /select/results)
attempt to fetch next result from JSON provided a lookup path query
attempt to fetch next result from JSON provided a lookup path query
BP_ERROR_T BP_DBFetchNextResultIntoJSON
(
	P_BPDB    bpdb,
	char *    results_path_lookup,
	P_BP_JSON completed_json_query,
	BP_BOOL   destroy_results_before_stacking_result
)
{
	if(!bpdb)
		return ERR_FAILURE;
	if(!bpdb->sqlgen_reg)
		return ERR_FAILURE;
	if(!bpdb->sqlgen_reg->hive)
		return ERR_FAILURE;
	if(!bpdb->sqlgen_reg->total_data_entries_in_hive)
		return ERR_FAILURE;
	if(!results_path_lookup)
		return ERR_FAILURE;
	if(!completed_json_query)
		return ERR_FAILURE;
	if(!BP_StringIsReasonablePrintableString(results_path_lookup, bpstrlen(results_path_lookup), BP_FALSE, BP_FALSE))
		return ERR_FAILURE;
	P_BP_JSON curr_query = completed_json_query;
	if(!curr_query)
	{
		return ERR_FAILURE;
	}
	char ** results_path = BP_HashRegStringLookupToPathArray(results_path_lookup, bpstrlen(results_path_lookup));
	if(!results_path)
		return ERR_FAILURE;
	if(!results_path[0])
	{
		BP_DestroyPointerArrayNullTerminated((void **) results_path);
		bpfree(results_path);
		return ERR_FAILURE;
	}
	if(!results_path[1])
	{
		BP_DestroyPointerArrayNullTerminated((void **) results_path);
		bpfree(results_path);
		return ERR_FAILURE;
	}
	char *path_lookup[] =
	{
		results_path[0],
		"results",
		"sqlgen_uuid",
		NULL
	};
	P_BP_HTKE uuid_lookup = BP_JSONLookupEntryByPath(curr_query, (char **) &path_lookup);
	if(!uuid_lookup)
	{
		BP_DestroyPointerArrayNullTerminated((void **) results_path);
		bpfree(results_path);
		return ERR_FAILURE;
	}
	P_BPDB_SQLGEN sqlgen_lookup = BP_DBLookupSQLGeneratorByUUID(bpdb, (char *) uuid_lookup->data);
	if(!BP_DBSGFetchResult(sqlgen_lookup, sqlgen_lookup->select_info, BP_TRUE))
	{
		BP_DestroyPointerArrayNullTerminated((void **) results_path);
		bpfree(results_path);
		return ERR_FAILURE;
	}
	char * tmp_idx = BP_GenRandomUUID();
	size_t bind_result_iter = 0;
	for(; bind_result_iter < sqlgen_lookup->select_info->bind_results_n; bind_result_iter++)
	{
		MYSQL_BIND * curr_result = &sqlgen_lookup->select_info->bind_results[bind_result_iter];
		char numeric_conversion_buff[256];
		BP_ZERO_STACK(numeric_conversion_buff);
		char timestamp_conversion_buff[512];
		BP_ZERO_STACK(timestamp_conversion_buff);
		long * tmp_long_ptr = NULL;
		char *tmp_path[20];
		BP_ZERO_STACK(tmp_path);
		tmp_path[0] = results_path[0];
		tmp_path[1] = "results";
		tmp_path[2] = tmp_idx;
		tmp_path[3] = sqlgen_lookup->select_info->field_names[bind_result_iter];
		tmp_path[4] = NULL;
		MYSQL_TIME * tmp_time = NULL;
		char *tmp_hex_blob_buff = NULL;
		char *tmp_hex_blob_buff_final = NULL;
		switch(curr_result->buffer_type)
		{
			case MYSQL_TYPE_DECIMAL:
				break;
			case MYSQL_TYPE_TINY:
				break;
			case MYSQL_TYPE_NEWDECIMAL:
				break;
			case MYSQL_TYPE_ENUM:
				break;
			case MYSQL_TYPE_SET:
				break;
			case MYSQL_TYPE_BIT:
				break;
			case MYSQL_TYPE_SHORT:
				sprintf((char *) &numeric_conversion_buff, "%hi", curr_result->buffer);
				BP_JSONAddNodeByPath(curr_query, tmp_path, numeric_conversion_buff, bpstrlen(numeric_conversion_buff), BP_FALSE);
				break;
			case MYSQL_TYPE_INT24:
			case MYSQL_TYPE_LONG:
				BP_ZERO_STACK(numeric_conversion_buff);
				tmp_long_ptr = (long *) curr_result->buffer;
				sprintf((char *) &numeric_conversion_buff, "%i", (long) *tmp_long_ptr);
				BP_JSONAddNodeByPath(curr_query, tmp_path, numeric_conversion_buff, bpstrlen(numeric_conversion_buff), BP_FALSE);
				break;
			case MYSQL_TYPE_LONGLONG:
				sprintf((char *) &numeric_conversion_buff, "%lli", curr_result->buffer);
				BP_JSONAddNodeByPath(curr_query, tmp_path, numeric_conversion_buff, bpstrlen(numeric_conversion_buff), BP_FALSE);
				break;
			case MYSQL_TYPE_FLOAT:
				sprintf((char *) &numeric_conversion_buff, "%f", curr_result->buffer);
				BP_JSONAddNodeByPath(curr_query, tmp_path, numeric_conversion_buff, bpstrlen(numeric_conversion_buff), BP_FALSE);
				break;
			case MYSQL_TYPE_DOUBLE:
				sprintf((char *) &numeric_conversion_buff, "%f", curr_result->buffer);
				BP_JSONAddNodeByPath(curr_query, tmp_path, numeric_conversion_buff, bpstrlen(numeric_conversion_buff), BP_FALSE);
				break;
			case MYSQL_TYPE_NULL:
				BP_JSONAddNodeByPath(curr_query, tmp_path, (char *) "NULL", bpstrlen("NULL"), BP_FALSE);
				break;
			case MYSQL_TYPE_DATETIME:
			case MYSQL_TYPE_YEAR:
			case MYSQL_TYPE_NEWDATE:
			case MYSQL_TYPE_DATE:
			case MYSQL_TYPE_TIME:
			case MYSQL_TYPE_TIMESTAMP:
				tmp_time = (MYSQL_TIME *) curr_result->buffer;
				sprintf
				(
					(char *) &timestamp_conversion_buff,
					"%.02u-%.02u-%.02u %.02u:%.02u:%.02u",
					tmp_time->year,
					tmp_time->month,
					tmp_time->day,
					tmp_time->hour,
					tmp_time->minute,
					tmp_time->second
				);
				BP_JSONAddNodeByPath(curr_query, tmp_path, (char *) timestamp_conversion_buff, bpstrlen(timestamp_conversion_buff), BP_FALSE);
				break;
			case MYSQL_TYPE_TINY_BLOB:
			case MYSQL_TYPE_MEDIUM_BLOB:
			case MYSQL_TYPE_LONG_BLOB:
			case MYSQL_TYPE_BLOB:
				tmp_hex_blob_buff = BP_BufferToMySQLHexString((char *) curr_result->buffer, curr_result->buffer_length, NULL);
				if(tmp_hex_blob_buff)
				{
					BP_JSONAddNodeByPath(curr_query, tmp_path, (char *) tmp_hex_blob_buff_final, bpstrlen(tmp_hex_blob_buff_final), BP_FALSE);
					if(tmp_hex_blob_buff)
						bpfree(tmp_hex_blob_buff);
					tmp_hex_blob_buff = NULL;
				}
				break;
			case MYSQL_TYPE_GEOMETRY:
				break;
			case MYSQL_TYPE_VARCHAR:
			case MYSQL_TYPE_VAR_STRING:
			case MYSQL_TYPE_STRING:
				BP_JSONAddNodeByPath(curr_query, tmp_path, (char *) curr_result->buffer, curr_result->buffer_length, BP_FALSE);
				break;
			default:
				break;
		}
	}
	if(results_path)
	{
		BP_DestroyPointerArrayNullTerminated((void **) results_path);
		bpfree(results_path);
	}
	bpfree(tmp_idx);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_DBFetchNextResultIntoJSON ( P_BPDB bpdb , char * results_path_lookup , P_BP_JSON completed_json_query , BP_BOOL destroy_results_before_stacking_result ) { if ( ! bpdb ) return ERR_FAILURE ; if ( ! bpdb -> sqlgen_reg ) return ERR_FAILURE ; if ( ! bpdb -> sqlgen_reg -> hive ) return ERR_FAILURE ; if ( ! bpdb -> sqlgen_reg -> total_data_entries_in_hive ) return ERR_FAILURE ; if ( ! results_path_lookup ) return ERR_FAILURE ; if ( ! completed_json_query ) return ERR_FAILURE ; if ( ! BP_StringIsReasonablePrintableString ( results_path_lookup , bpstrlen ( results_path_lookup ) , BP_FALSE , BP_FALSE ) ) return ERR_FAILURE ; P_BP_JSON curr_query = completed_json_query ; if ( ! curr_query ) { return ERR_FAILURE ; } char * * results_path = BP_HashRegStringLookupToPathArray ( results_path_lookup , bpstrlen ( results_path_lookup ) ) ; if ( ! results_path ) return ERR_FAILURE ; if ( ! results_path [ 0 ] ) { BP_DestroyPointerArrayNullTerminated ( ( void * * ) results_path ) ; bpfree ( results_path ) ; return ERR_FAILURE ; } if ( ! results_path [ 1 ] ) { BP_DestroyPointerArrayNullTerminated ( ( void * * ) results_path ) ; bpfree ( results_path ) ; return ERR_FAILURE ; } char * path_lookup [ ] = { results_path [ 0 ] , " " , " " , NULL } ; P_BP_HTKE uuid_lookup = BP_JSONLookupEntryByPath ( curr_query , ( char * * ) & path_lookup ) ; if ( ! uuid_lookup ) { BP_DestroyPointerArrayNullTerminated ( ( void * * ) results_path ) ; bpfree ( results_path ) ; return ERR_FAILURE ; } P_BPDB_SQLGEN sqlgen_lookup = BP_DBLookupSQLGeneratorByUUID ( bpdb , ( char * ) uuid_lookup -> data ) ; if ( ! BP_DBSGFetchResult ( sqlgen_lookup , sqlgen_lookup -> select_info , BP_TRUE ) ) { BP_DestroyPointerArrayNullTerminated ( ( void * * ) results_path ) ; bpfree ( results_path ) ; return ERR_FAILURE ; } char * tmp_idx = BP_GenRandomUUID ( ) ; size_t bind_result_iter = 0 ; for ( ; bind_result_iter < sqlgen_lookup -> select_info -> bind_results_n ; bind_result_iter ++ ) { MYSQL_BIND * curr_result = & sqlgen_lookup -> select_info -> bind_results [ bind_result_iter ] ; char numeric_conversion_buff [ 256 ] ; BP_ZERO_STACK ( numeric_conversion_buff ) ; char timestamp_conversion_buff [ 512 ] ; BP_ZERO_STACK ( timestamp_conversion_buff ) ; long * tmp_long_ptr = NULL ; char * tmp_path [ 20 ] ; BP_ZERO_STACK ( tmp_path ) ; tmp_path [ 0 ] = results_path [ 0 ] ; tmp_path [ 1 ] = " " ; tmp_path [ 2 ] = tmp_idx ; tmp_path [ 3 ] = sqlgen_lookup -> select_info -> field_names [ bind_result_iter ] ; tmp_path [ 4 ] = NULL ; MYSQL_TIME * tmp_time = NULL ; char * tmp_hex_blob_buff = NULL ; char * tmp_hex_blob_buff_final = NULL ; switch ( curr_result -> buffer_type ) { case MYSQL_TYPE_DECIMAL : break ; case MYSQL_TYPE_TINY : break ; case MYSQL_TYPE_NEWDECIMAL : break ; case MYSQL_TYPE_ENUM : break ; case MYSQL_TYPE_SET : break ; case MYSQL_TYPE_BIT : break ; case MYSQL_TYPE_SHORT : sprintf ( ( char * ) & numeric_conversion_buff , " " , curr_result -> buffer ) ; BP_JSONAddNodeByPath ( curr_query , tmp_path , numeric_conversion_buff , bpstrlen ( numeric_conversion_buff ) , BP_FALSE ) ; break ; case MYSQL_TYPE_INT24 : case MYSQL_TYPE_LONG : BP_ZERO_STACK ( numeric_conversion_buff ) ; tmp_long_ptr = ( long * ) curr_result -> buffer ; sprintf ( ( char * ) & numeric_conversion_buff , " " , ( long ) * tmp_long_ptr ) ; BP_JSONAddNodeByPath ( curr_query , tmp_path , numeric_conversion_buff , bpstrlen ( numeric_conversion_buff ) , BP_FALSE ) ; break ; case MYSQL_TYPE_LONGLONG : sprintf ( ( char * ) & numeric_conversion_buff , " " , curr_result -> buffer ) ; BP_JSONAddNodeByPath ( curr_query , tmp_path , numeric_conversion_buff , bpstrlen ( numeric_conversion_buff ) , BP_FALSE ) ; break ; case MYSQL_TYPE_FLOAT : sprintf ( ( char * ) & numeric_conversion_buff , " " , curr_result -> buffer ) ; BP_JSONAddNodeByPath ( curr_query , tmp_path , numeric_conversion_buff , bpstrlen ( numeric_conversion_buff ) , BP_FALSE ) ; break ; case MYSQL_TYPE_DOUBLE : sprintf ( ( char * ) & numeric_conversion_buff , " " , curr_result -> buffer ) ; BP_JSONAddNodeByPath ( curr_query , tmp_path , numeric_conversion_buff , bpstrlen ( numeric_conversion_buff ) , BP_FALSE ) ; break ; case MYSQL_TYPE_NULL : BP_JSONAddNodeByPath ( curr_query , tmp_path , ( char * ) " " , bpstrlen ( " " ) , BP_FALSE ) ; break ; case MYSQL_TYPE_DATETIME : case MYSQL_TYPE_YEAR : case MYSQL_TYPE_NEWDATE : case MYSQL_TYPE_DATE : case MYSQL_TYPE_TIME : case MYSQL_TYPE_TIMESTAMP : tmp_time = ( MYSQL_TIME * ) curr_result -> buffer ; sprintf ( ( char * ) & timestamp_conversion_buff , " " , tmp_time -> year , tmp_time -> month , tmp_time -> day , tmp_time -> hour , tmp_time -> minute , tmp_time -> second ) ; BP_JSONAddNodeByPath ( curr_query , tmp_path , ( char * ) timestamp_conversion_buff , bpstrlen ( timestamp_conversion_buff ) , BP_FALSE ) ; break ; case MYSQL_TYPE_TINY_BLOB : case MYSQL_TYPE_MEDIUM_BLOB : case MYSQL_TYPE_LONG_BLOB : case MYSQL_TYPE_BLOB : tmp_hex_blob_buff = BP_BufferToMySQLHexString ( ( char * ) curr_result -> buffer , curr_result -> buffer_length , NULL ) ; if ( tmp_hex_blob_buff ) { BP_JSONAddNodeByPath ( curr_query , tmp_path , ( char * ) tmp_hex_blob_buff_final , bpstrlen ( tmp_hex_blob_buff_final ) , BP_FALSE ) ; if ( tmp_hex_blob_buff ) bpfree ( tmp_hex_blob_buff ) ; tmp_hex_blob_buff = NULL ; } break ; case MYSQL_TYPE_GEOMETRY : break ; case MYSQL_TYPE_VARCHAR : case MYSQL_TYPE_VAR_STRING : case MYSQL_TYPE_STRING : BP_JSONAddNodeByPath ( curr_query , tmp_path , ( char * ) curr_result -> buffer , curr_result -> buffer_length , BP_FALSE ) ; break ; default : break ; } } if ( results_path ) { BP_DestroyPointerArrayNullTerminated ( ( void * * ) results_path ) ; bpfree ( results_path ) ; } bpfree ( tmp_idx ) ; return ERR_SUCCESS ; }
attempt to fetch next result from JSON provided a lookup path query (eg.
attempt to fetch next result from JSON provided a lookup path query ( eg .
// run basic sanity checks // clone from path query // fail if we have no current query // convert string lookup to path array // ensure we have path elements // set path lookup // attempt to lookup the sqlgen uuid for this query // exit if we have no uuid // now attempt to lookup sqlgen by uuid // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Fetch Records %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // fetch result // display the select info // BP_DBDisplaySelectInfo(sqlgen_lookup->select_info); // generate random uuid index // iterate through the bind results // set current result from result stack // create conversion buff // create timestamp conversion buff // set temporary long pointer // set path buffer // set the query add path // set temporary time // temporary hex buffers for blob types // gather supported data types // unprocessed types // numeric types // process timestamps // CURRENT_TIMESTAMP() EXAMPLE: "2016-11-21 17:33:15" // set timestamp // create timestamp // process blob fields (returned as hexidecimal) // create hex blob // add node by path // set temp hex blob // set buff // unhandled type // process string types // default (do nothing) // destroy results path // destroy the temporarily retrieved index // return indicating success