library/src/databases/bpdb/BP-DBJSON.cc
// attempt to run a stack of queries from JSON
attempt to run a stack of queries from JSON
attempt to run a stack of queries from JSON
P_BP_JSON BP_DBRunQueriesFromJSON(P_BPDB bpdb, P_BP_JSON json_query)
{
	if(!bpdb)
		return NULL;
	if(!json_query)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBRunQueriesFromJSON was called with a NULL json_query parameter.  This likely means your query JSON failed to parse correctly.");
		return NULL;
	}
	if(!json_query->json_out_hreg)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBRunQueriesFromJSON was called with a NULL json_query->json_out_hreg parameter.  This likely means your query JSON failed to parse correctly.");
		return NULL;
	}
	if(!json_query->json_out_hreg->hive)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBRunQueriesFromJSON was called with a NULL json_query->json_out_hreg->hive parameter.  Corruption detected.");
		return NULL;
	}
	if(!json_query->json_out_hreg->hive->data)
	{
		BP_DBAddErrorMsg(bpdb, "BP_DBRunQueriesFromJSON was called with a NULL json_query->json_out_hreg->hive->data parameter.  This likely means your query JSON failed to parse correctly.");
		return NULL;
	}
	P_BP_JSON query_res = NULL;
	P_BP_JSON curr_query = NULL;
	char *tmp_clone_lookup_path = NULL;
	BP_HASH_ITER_OPEN((P_BP_HTKE) json_query->json_out_hreg->hive, query_iter)
	{
		if(ht_memmatch_key_front_part(query_iter, "select_", bpstrlen("select_")) == ERR_SUCCESS)
		{
			tmp_clone_lookup_path = BP_HashRegCreateReasonableStringPathFromKeyedEntry(json_query->json_out_hreg, query_iter);
			curr_query = BP_JSONCloneFromPathQuery
			(
				json_query,
				tmp_clone_lookup_path,
				BP_TRUE,
				400,
				400
			);
			if(!BP_DBRunSelectQueryFromJSON_internal(bpdb, curr_query))
			{
				printf("\n [!!] Select query from JSON failed.");
				BP_JSONDestroy(curr_query);
				bpfree(tmp_clone_lookup_path);
				continue;
			}
			char ** select_query_path_array = BP_HashRegCreatePathFromKeyedEntry
			(
				json_query->json_out_hreg,
				query_iter
			);
			char * path_lookup_query[10];
			BP_ZERO_STACK(path_lookup_query);
			path_lookup_query[0] = query_iter->key;
			path_lookup_query[1] = "results";
			path_lookup_query[2] = "sqlgen_uuid";
			path_lookup_query[3] = NULL;
			P_BP_HTKE sqlgen_uuid_entry = BP_JSONLookupEntryByPath(curr_query, (char **) &path_lookup_query);
			if(sqlgen_uuid_entry)
			{
				path_lookup_query[2] = NULL;
				BP_JSONAddNodeByPath(json_query, (char **) &path_lookup_query, NULL, 0, BP_FALSE);
				path_lookup_query[2] = "sqlgen_uuid";
				BP_JSONAddNodeByPath
				(
					json_query,
					(char **) &path_lookup_query,
					(char *) sqlgen_uuid_entry->data,
					sqlgen_uuid_entry->data_size,
					BP_FALSE
				);
			}
			BP_DestroyPointerArrayNullTerminated((void **) select_query_path_array);
			bpfree(select_query_path_array);
			BP_JSONDestroy(curr_query);
			ht_free(json_query->json_out_hreg, tmp_clone_lookup_path);
		}
		else
		if(ht_memmatch_key(query_iter, "update", bpstrlen("update")))
		{
			printf("\n Got update query?");
		}
		else
		if(ht_memmatch_key(query_iter, "insert", bpstrlen("insert")))
		{
			printf("\n Got insert query?");
		}
		else
		if(ht_memmatch_key(query_iter, "call", bpstrlen("call")))
		{
			printf("\n Got call query?");
		}
		else
		{
			printf("\n Got unknown or bad query?: %s", query_iter->key);
		}
	}
	BP_HASH_ITER_CLOSE;
	return NULL;
}
P_BP_JSON BP_DBRunQueriesFromJSON ( P_BPDB bpdb , P_BP_JSON json_query ) { if ( ! bpdb ) return NULL ; if ( ! json_query ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return NULL ; } if ( ! json_query -> json_out_hreg ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return NULL ; } if ( ! json_query -> json_out_hreg -> hive ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return NULL ; } if ( ! json_query -> json_out_hreg -> hive -> data ) { BP_DBAddErrorMsg ( bpdb , " " ) ; return NULL ; } P_BP_JSON query_res = NULL ; P_BP_JSON curr_query = NULL ; char * tmp_clone_lookup_path = NULL ; BP_HASH_ITER_OPEN ( ( P_BP_HTKE ) json_query -> json_out_hreg -> hive , query_iter ) { if ( ht_memmatch_key_front_part ( query_iter , " " , bpstrlen ( " " ) ) == ERR_SUCCESS ) { tmp_clone_lookup_path = BP_HashRegCreateReasonableStringPathFromKeyedEntry ( json_query -> json_out_hreg , query_iter ) ; curr_query = BP_JSONCloneFromPathQuery ( json_query , tmp_clone_lookup_path , BP_TRUE , 400 , 400 ) ; if ( ! BP_DBRunSelectQueryFromJSON_internal ( bpdb , curr_query ) ) { printf ( " \n " ) ; BP_JSONDestroy ( curr_query ) ; bpfree ( tmp_clone_lookup_path ) ; continue ; } char * * select_query_path_array = BP_HashRegCreatePathFromKeyedEntry ( json_query -> json_out_hreg , query_iter ) ; char * path_lookup_query [ 10 ] ; BP_ZERO_STACK ( path_lookup_query ) ; path_lookup_query [ 0 ] = query_iter -> key ; path_lookup_query [ 1 ] = " " ; path_lookup_query [ 2 ] = " " ; path_lookup_query [ 3 ] = NULL ; P_BP_HTKE sqlgen_uuid_entry = BP_JSONLookupEntryByPath ( curr_query , ( char * * ) & path_lookup_query ) ; if ( sqlgen_uuid_entry ) { path_lookup_query [ 2 ] = NULL ; BP_JSONAddNodeByPath ( json_query , ( char * * ) & path_lookup_query , NULL , 0 , BP_FALSE ) ; path_lookup_query [ 2 ] = " " ; BP_JSONAddNodeByPath ( json_query , ( char * * ) & path_lookup_query , ( char * ) sqlgen_uuid_entry -> data , sqlgen_uuid_entry -> data_size , BP_FALSE ) ; } BP_DestroyPointerArrayNullTerminated ( ( void * * ) select_query_path_array ) ; bpfree ( select_query_path_array ) ; BP_JSONDestroy ( curr_query ) ; ht_free ( json_query -> json_out_hreg , tmp_clone_lookup_path ) ; } else if ( ht_memmatch_key ( query_iter , " " , bpstrlen ( " " ) ) ) { printf ( " \n " ) ; } else if ( ht_memmatch_key ( query_iter , " " , bpstrlen ( " " ) ) ) { printf ( " \n " ) ; } else if ( ht_memmatch_key ( query_iter , " " , bpstrlen ( " " ) ) ) { printf ( " \n " ) ; } else { printf ( " \n " , query_iter -> key ) ; } } BP_HASH_ITER_CLOSE ; return NULL ; }
attempt to run a stack of queries from JSON
attempt to run a stack of queries from JSON
// ensure that we have bpdp // basic null checks // query result // current query pointer // clone lookup path (used for cloning json for queries) // iterate through queries // if we have a select query, process it // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Process Select Query %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // create lookup path // clone from path query // run select query using cloned json input // ---- ADD RESULTS PATH AFTER QUERY RUN ---------- // array as a path // create lookup query // path lookup query // attempt to regenerate uuid // if we have results // remove the "sqlgen_uuid" entry in the query stack // add table // restore sqlgen paramter // add node // destroy the path // cleanup // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Process Result If We Have One %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // destroy json on loop // BP_JSONDestroy(curr_query); // curr_query = NULL;