Example #1
0
/**
 * Create and run the basic query on the gridvalue table, for use by the wciReadFloat function
 */
static void runWciReadFloatQueryGrid(struct ReadStore * out, FuncCallContext * funcctx, FunctionCallInfo fcinfo)
{
	initializeGeos();

	struct WciReadParameterCollection p;
	parseReadParameters(& p, fcinfo);
	const char * whatToSelect = "value, dataprovidername, placename::text, placegeometry, referencetime, validtimefrom, validtimeto, validtimeindeterminatecode, valueparametername, valueunitname, levelparametername, levelunitname, levelfrom, levelto, levelindeterminatecode, dataversion, confidencecode, valuestoretime, valueid, valuetype, placeid";
	const char * gridQuery = build_query(& p, GridTable, OutputFloat, whatToSelect, NULL);
	elog(DEBUG1, gridQuery);

	// Perform primary query
	SPIPlanPtr queryPlan = getSpiPlan(gridQuery);
	if (SPI_OK_SELECT != SPI_execute_plan(queryPlan, NULL, NULL, true, 0))
	{
		ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg(
				"Error when performing base query")));
	}
	MemoryContext oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);


	if ( PG_ARGISNULL(1) )
	{
		ReadStoreGridReturnInit(out, SPI_tuptable, SPI_processed, NULL);
	}
	else
	{
		text * location_t = PG_GETARG_TEXT_P(1);
		const char * location = TextPGetCString(location_t);
		ReadStoreGridReturnInit(out, SPI_tuptable, SPI_processed, location);
	}
	MemoryContextSwitchTo(oldcontext);
}
Example #2
0
//------------------------------------------------------------------------------
// deals with public API methods:
std::string KClient::public_method(const std::string& method, 
				const KInput& input) const
{
   // build method URL
   std::string path = "/" + version_ + "/public/" + method;
   std::string method_url = url_ + path;   
   curl_easy_setopt(curl_, CURLOPT_URL, method_url.c_str());

   // build postdata 
   std::string postdata = build_query(input);
   curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, postdata.c_str());

   // reset the http header
   curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, NULL);
 
   // where CURL write callback function stores the response
   std::string response;
   curl_easy_setopt(curl_, CURLOPT_WRITEDATA, static_cast<void*>(&response));
  
   // perform CURL request
   CURLcode result = curl_easy_perform(curl_);
   if (result != CURLE_OK) {
      std::ostringstream oss;  
      oss << "curl_easy_perform() failed: "<< curl_easy_strerror(result);
      throw std::runtime_error(oss.str());
   }

   return response;
}
Example #3
0
static int odbc_log(struct ast_cdr *cdr)
{
	int res = 0;
	char timestr[150];

	ast_mutex_lock(&odbc_lock);
	build_query(cdr, timestr, sizeof(timestr));

	if (connected) {
		res = odbc_do_query();
		if (res < 0) {
			if (option_verbose > 10)
				ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Reconnecting to dsn %s\n", dsn);
			odbc_disconnect();
			res = odbc_init();
			if (res < 0) {
				if (option_verbose > 10)
					ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: %s has gone away!\n", dsn);
				odbc_disconnect();
			} else {
				if (option_verbose > 10)
					ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Trying Query again!\n");
				SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
				build_query(cdr, timestr, sizeof(timestr)); /* what a waste. If we have to reconnect, we have to build a new query */
				res = odbc_do_query();
				if (res < 0) {
					if (option_verbose > 10)
						ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
				}
			}
		}
	} else {
		if (option_verbose > 10)
			ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: Query FAILED Call not logged!\n");
	}
	SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt);
	ast_mutex_unlock(&odbc_lock);
	return 0;
}
Example #4
0
Datum getWciBrowseValueParameterQuery(PG_FUNCTION_ARGS)
{
	struct WciReadParameterCollection p;
	parseReadParameters(& p, fcinfo);

	const char * query = build_query(& p, GridTable, OutputGid,
			"valueparametername, valueunitname, count(*)",
			"GROUP BY valueparametername, valueunitname");

	text * ret = CStringGetTextP(query);

	PG_RETURN_TEXT_P(ret);
}
Example #5
0
Datum getWciBrowsePlaceQuery(PG_FUNCTION_ARGS)
{
	struct WciReadParameterCollection p;
	parseReadParameters(& p, fcinfo);

	const char * query = build_query(& p, GridTable, OutputGid,
			"placename, min(referencetime), max(referencetime), count(*)",
			"GROUP BY placename");

	text * ret = CStringGetTextP(query);

	PG_RETURN_TEXT_P(ret);
}
Example #6
0
Datum getWciBrowseLevelParameterQuery(PG_FUNCTION_ARGS)
{
	struct WciReadParameterCollection p;
	parseReadParameters(& p, fcinfo);

	const char * query = build_query(& p, GridTable, OutputGid,
			"levelparametername, levelunitname, min(levelfrom), max(levelto), count(*)",
			"GROUP BY levelparametername, levelunitname");

	text * ret = CStringGetTextP(query);

	PG_RETURN_TEXT_P(ret);
}
Example #7
0
Datum getWciBrowseDataVersionQuery(PG_FUNCTION_ARGS)
{
	struct WciReadParameterCollection p;
	parseReadParameters(& p, fcinfo);

	const char * query = build_query(& p, GridTable, OutputGid,
			"dataversion, count(*)",
			"GROUP BY 1 ORDER BY 1");

	text * ret = CStringGetTextP(query);

	PG_RETURN_TEXT_P(ret);
}
Example #8
0
//------------------------------------------------------------------------------
// deals with private API methods:
std::string KClient::private_method(const std::string& method, 
				 const KInput& input) const
{   
   // build method URL
   std::string path = "/" + version_ + "/private/" + method;
   std::string method_url = url_ + path;

   curl_easy_setopt(curl_, CURLOPT_URL, method_url.c_str());

   // create a nonce and and postdata 
   std::string nonce = create_nonce();
   std::string postdata = "nonce=" + nonce;

   // if 'input' is not empty generate other postdata
   if (!input.empty())
      postdata = postdata + "&" + build_query(input);
   curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, postdata.c_str());

   // add custom header
   curl_slist* chunk = NULL;

   std::string key_header =  "API-Key: "  + key_;
   std::string sign_header = "API-Sign: " + signature(path, nonce, postdata);

   chunk = curl_slist_append(chunk, key_header.c_str());
   chunk = curl_slist_append(chunk, sign_header.c_str());
   curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, chunk);
   
   // where CURL write callback function stores the response
   std::string response;
   curl_easy_setopt(curl_, CURLOPT_WRITEDATA, static_cast<void*>(&response));

   // perform CURL request
   CURLcode result = curl_easy_perform(curl_);

   // free the custom headers
   curl_slist_free_all(chunk);
  
   // check perform result
   if (result != CURLE_OK) {
      std::ostringstream oss;
      oss << "curl_easy_perform() failed: " << curl_easy_strerror(result);
      throw std::runtime_error(oss.str());
   }
   
   return response;
}
Example #9
0
/**
 * Create and run the basic query on the floatvalue table, for use by the wciReadFloat function
 */
static void runWciReadFloatQueryFloat(struct ReadStore * out, FunctionCallInfo fcinfo)
{
	struct WciReadParameterCollection p;
	parseReadParameters(& p, fcinfo);

	// This must match exactly the return type for wci.returnfloat
	const char * whatToSelect = "value::float, dataprovidername, placename::text, st_astext(placegeometry), referencetime, validtimefrom, validtimeto, validtimeindeterminatecode, valueparametername, valueunitname, levelparametername, levelunitname, levelfrom, levelto, levelindeterminatecode, dataversion, confidencecode, valuestoretime, valueid, valuetype";
	const char * gridQuery = build_query(& p, FloatTable, OutputFloat, whatToSelect, NULL);
	elog(DEBUG1, gridQuery);

	// Perform primary query
	SPIPlanPtr queryPlan = getSpiPlan(gridQuery);
	if (SPI_OK_SELECT != SPI_execute_plan(queryPlan, NULL, NULL, true, 0))
	{
		ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg(
				"Error when performing base query")));
	}

	out->tuples = SPI_tuptable;
	out->tupleCount = SPI_processed;
}
Example #10
0
int
query_main(QueryData *q, char *host)
{
    int connfd, n, m, i;
    char *myhost;
    char *answer = (char *) calloc(1280,1);
    char *query = q->query;
    int cnt;

    if(host == NULL) {
      myhost = "2001:4f8:3:36:2e0:81ff:fe23:90d3"; // toutatis.taht.net
    } else {
      myhost = host;
    }
    build_query(q); 
    if(q->options.verbose) fprintf(stderr,"Connecting: %s\n",myhost);
    connfd = connect_client(myhost, QUERY_PORT, AF_UNSPEC, SOCK_DGRAM);

    if (connfd < 0) {
         fprintf(stderr,
                 "client error:: could not create connected socket "
                 "socket\n");
         return -1;
    }
    if(q->options.verbose) {
      fprintf(stderr,"Writing query: \"%s\" to socket of length %d\n", 
	      query, strlen(query));
    }
    // fixme - generate legal checksum and check length
    m= write(connfd, query, strlen(query));
    memset(answer, 0, MAX_MTU);
    n = read(connfd,
             answer,
             MAX_MTU); // FIXME, leave running and timeout
    if(q->options.verbose) fprintf(stderr,"Got response: %s\n", answer);
    close(connfd);
    strcpy(q->query,answer); // ycuk - figmeout
    return(answer_parse(q));
}
Example #11
0
int execute_request(int use_post,const char *base_url, const int param_count, request_param *params[],  int(*process_received_response)(response*) ) {
    CURL *curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    char *request_string;
    build_query(&request_string,curl,param_count,params);
    if(curl) {
        response r;
        init_empty_response(&r);
        if(use_post) {
            curl_easy_setopt(curl,CURLOPT_URL,base_url);
            curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request_string);
            curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, read_response);
            curl_easy_setopt(curl,CURLOPT_WRITEDATA, &r);
        } else {
            char *full_url = malloc(sizeof(base_url) + sizeof(request_string) + 2);//one for null terminator and one for '?'
            strcpy(full_url,base_url);
            strcat(full_url,"?");
            strcat(full_url,request_string);
            curl_easy_setopt(curl,CURLOPT_URL,full_url);
            curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, read_response);
            curl_easy_setopt(curl,CURLOPT_WRITEDATA, &r);
            free(full_url);
        }
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            fprintf(stderr,"curl failed: %s\n",curl_easy_strerror(res));
        } else {
            process_received_response(&r);
        }
        free(r.ptr);
        free(request_string);
        curl_easy_cleanup(curl);
    }
    return 0;
}
Example #12
0
void process_query(int argc,char *argv[])
{
	FILE *q_file;//,*r_doc_dic;

	//doc_dictionay *r_dd_node = (doc_dictionay *)malloc(sizeof(doc_dictionay));
	
	int q_terms,i;
	int qt_count;//Actual count of the query terms found in the tree
	
	char query_term[50];
	char pf_name[200];

	pFile *qt_pfs[argc-3];// Max of argc -2 query terms, one link to each
	long dfs[argc-3];// same as above for Doc Freq
	
	//r_doc_dic = fopen("../bin/docdic","rb");
	q_file = fopen("../bin/query","w+");
	q_terms = argc-3;// 1st two r filename & -s

	for(i=3;i<argc;i++)
	{
		fprintf(q_file,"%s",argv[i]);
		if(i+1 != argc)			
			fprintf(q_file,"%c",' ');
	}
	fclose(q_file);
	
	do_stem("../bin/query");

	build_query();
/*
	q_file = fopen("../bin/stemop","r+");
	qt_count = -1;
	while(fscanf(q_file,"%s",query_term)!=EOF)
	{
		query_term[strlen(query_term)]='\0';
		if(isSW(query_term)) 
		{
			printf("%s is a stop word\n",query_term);
			continue;
			
		}
		else
		{
			//apply md5
			//search for word in posting file
			//get the node , get the posting list
			//perform query [just display all files containing the word now]

			mdstr *a1;
			strcpy(g_key,query_term);
			g_key[strlen(g_key)]='\0';
			a1 = (mdstr *)do_test(g_key);//Generate MD5
			strcpy(g_hex,a1->hash);
			g_hex[32]='\0';
			free(a1);
			toBinary();
			sres *srch ;
			srch = tree_ssearch(g_key,g_bin);
			if(srch->found == 0)
				printf("\nkey '%s' is NOT found in the tree\n",g_key);
			else if(srch->found ==1)
			{
				printf("\nKey '%s' found in the tree with keyvalue %d, %d\n\n",g_key,srch->keyval,srch->near->keyval);
				qt_count++;//Incriment Query Terms
				qt_pfs[qt_count] = srch->near->pf;
				dfs[qt_count] = srch->near->df;


///Test Search				printf("%s is present in the Docs with ID & Freq\n");
				printf("DOC ID\tTerm Freq\nPath\n");
			
				FILE *qt_pf;  
				pFile *ret_pf_node;
				strcpy(pf_name,"../bin/ifiles/");	pf_name[strlen(pf_name)]='\0';
				strcat(pf_name,g_bin);		pf_name[strlen(pf_name)]='\0';

				qt_pf = fopen(pf_name,"rb"); // Opened the Posting File
				ret_pf_node = (pFile *)malloc(sizeof(pFile));

				while(fread(ret_pf_node, sizeof(struct pFile), 1, qt_pf))
				{
					
					// Get the actual file name r_doc_dic
					fseek(r_doc_dic,0,SEEK_SET); //GO to start
					fseek(r_doc_dic,sizeof(doc_dictionay)*(ret_pf_node->did - 1),SEEK_SET); //Go to req. node
					fread(r_dd_node,sizeof(doc_dictionay),1,r_doc_dic);//Read the node from file & print file name
		
					printf("%ld\t%ld\n",ret_pf_node->did,ret_pf_node->tfd);
					printf("from bfile: ID: %ld path:%s\n\n",r_dd_node->docID,r_dd_node->path);
					
					
				}

				fclose(qt_pf);
///End o Test Search
			}//else if(srch->found ==1)
			
		}//else if !stopword
	}// End of While	
*/
	
	
}