awips2/ncep/gov.noaa.nws.ncep.viz.rsc.ncgrid/dgdriv_c/dbrunquery.c
Steve Harris 40aa780b3d 12.4.1-10 baseline
Former-commit-id: 7fa9dbd5fb [formerly 4bfbdad17d] [formerly 9f8cb727a5] [formerly 8485b90ff8 [formerly 9f8cb727a5 [formerly bf53d06834caa780226121334ac1bcf0534c3f16]]]
Former-commit-id: 8485b90ff8
Former-commit-id: 73930fb29d0c1e91204e76e6ebfdbe757414f319 [formerly a28d70b5c5]
Former-commit-id: 33a67cdd82
2012-05-01 18:06:13 -05:00

124 lines
4.6 KiB
C

#include "geminc.h"
#include "gemprm.h"
#include "dbcmn.h"
#include <sys/timeb.h>
void db_runQuery ( char *queryText, char *queryResult, int *iret )
/************************************************************************
* *
* db_runQuery *
* *
* This function sends a query, specified in queryText to the AWISP II *
* end point and returns the result of the query in the queryResult *
* string. *
* *
* void db_runQuery ( char *queryText, char *queryResult, int *iret ) *
* *
* Input parameters: *
* *queryText char Text of the query *
* *
* Output parameters: *
* *queryResult char Query result *
* *iret int return code: *
* -1: query run unsuccessfully *
* 0: normal *
** *
* Log: *
* *
* m.gamazaychikov/CWS 01/10 Created *
************************************************************************/
{
char headcmd[128], popncmd[512], uengine[50], sep[5], *errorReturn = NULL;
int queryResultSize = 20000;
int chars_read=0 , ier, ier1;
FILE *read_fp;
struct timeb t_start, t_open, t_read, t_current;
/*---------------------------------------------------------------------*/
ftime(&t_start);
*iret = 0;
/*
echo 'import GempakCatalogTimeQuery;query =GempakCatalogTimeQuery.GempakCatalogTimeQuery("mosaic");query.setParameters("BREF|2.0");return query.execute();' | uengine -r python
*/
/*
* Create the string containing the curl command and flags:
* -s Silent mode. Makes Curl mute - does not show progress
* meter or error messages.
* -d Sends the specified data in a POST request to the HTTP
* server.
*
sprintf (headcmd, "%s", "curl -s -d");
*/
/*
* command to direct string to standard input
*/
sprintf (headcmd, "%s", "echo");
/*
* Create a string that invokes the AWIPS II uEngine CLI untility
*/
sprintf (sep, "%s", "|");
sprintf (uengine, "%s", "uengine -r python -m");
/*
* Create the string for the open pipe
*/
sprintf (popncmd, "%s \'%s\' %s %s", headcmd, queryText, sep, uengine);
/*
* Initialize the queryResult string, open the pipe,
* and read data from the pipe
*/
memset (queryResult, '\0', sizeof(queryResult));
ftime(&t_open);
read_fp = popen(popncmd, "r");
ftime(&t_current);
printf("\t\t\t time spent in db_runquery in popen: %d\n", (int) (1000.0 * (t_current.time - t_open.time) + (t_current.millitm - t_open.millitm)));
if ( read_fp != NULL ) {
/* comment out for now - remove later
*/
ftime(&t_read);
chars_read = fread( queryResult, sizeof( char ),
queryResultSize, read_fp );
queryResult[chars_read] = '\0';
ftime(&t_current);
printf("\t\t\t time spent in db_runquery in read: %d\n", (int) (1000.0 * (t_current.time - t_read.time) + (t_current.millitm - t_read.millitm)));
/* comment out for now - remove later
while (chars_read > 0) {
queryResult[chars_read - 1] = '\0';
chars_read = fread(queryResult, sizeof(char),
queryResultSize, read_fp);
}
*/
pclose(read_fp);
if ( chars_read == 0 ) {
ier = -16;
er_wmsg ( "DB", &ier, uengine, &ier1, 2, strlen(uengine) );
*iret = -1;
return;
}
}
else {
ier = -11;
er_wmsg ( "DB", &ier, NULL, &ier1, 2, 0 );
*iret = -1;
return;
}
errorReturn = strstr (queryResult, "error");
if ( errorReturn != NULL ) {
ier = -12;
er_wmsg ( "DB", &ier, NULL, &ier1, 2, 0 );
*iret = -1;
return;
}
ftime(&t_current);
printf("\t\t\t time spent in db_runquery: %d\n", (int) (1000.0 * (t_current.time - t_start.time) + (t_current.millitm - t_start.millitm)));
return;
}