/*
 * simple minded front-end to run sequoia benchmark queries, gather timings
 * for each while avoiding initialization costs for each one.
 */
#include <sys/time.h>
#include "tmp/libpq-fe.h"

char *Queries[] =
{
/*Q2*/	"retrieve  (data = raster_bclip(rasters.data, \"(-1704753, -581511, -1604753, -481829)\"::box), rasters.time) where rasters.frequency = 5 sort by time",

/*Q3*/	"retrieve into rastertmp (rasters.time, data = raster_bclip (rasters.data, \"(-1704753, -581511, -1604753, -481829)\"::box)) where rasters.time = 1",

/*Q3*/	"retrieve (x=raster_avg{rastertmp.data})",

/*Q4*/	"retrieve into foo_1 (time = 1, resolution = 64, frequency = 5, data = raster_lowerres(raster_bclip(rasters.data, \"(-1704753, -581511, -1604753, -481829)\"::box), 64)) where rasters.time = 1",

/*Q5*/	"retrieve (gpoints.all) where gpoints.name = \"Abadi Creek\" ",

/*Q6*/	"retrieve into foo_2 (polygons.all) where polygons.location && \"(-1704753, -581511, -1604753, -481829)\"::polygon ",

/*Q7*/	"retrieve (polygons.location) where (size(polygons.location) > \"1014420.0\"::float8) and (make_circle(\"(-1704753, -581511)\"::point, \"3170.0\"::float8) <|> polygons.location)",

/*Q8*/	"retrieve (polygons.landuse, polygons.location) where (polygons.location && make_box(gpoints.location,\"50.0\"::float8)) and (gpoints.name = \"Zaca Ridge\") ",

/*Q9*/	"retrieve (polygons.location, x=raster_clip(rasters.data, polygons.location)) where polygons.landuse = 92 and rasters.frequency = 5 and rasters.time = 1",

/*Q10*/	"retrieve into Query10 (gpoints.name, gpoints.location) where (gpoints.location @ polygons.location) and (polygons.landuse = 91)",

/*Q10*/	"retrieve (Query10.name) where pt_nislands(Query10.location) = 0"
};

#ifdef DEBUG
#define PQexec(STRING) (char *)printf("%s\n", STRING)
#endif

main()
{
    struct timeval tpStart;
    struct timeval tpEnd;
    long secs, usecs;
    char *qresult;
    char buf[64];
    extern char* getenv();
    char* dbname;
    
    dbname = getenv("DBNAME");
    if (dbname)
      PQsetdb(dbname);
    else
      PQsetdb("bench");
    

    /* query1 */
    printf("Start of Query1\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(" load \"polyfuncs.so\" ");
    qresult = PQexec(" load \"raster.so\" ");
    gettimeofday(&tpEnd, NULL);
    printf("initialization nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);

    /* query2 */

    printf("Start of Query2\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[0]);
    gettimeofday(&tpEnd, NULL);
    printf("query2 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    /* query 3 */

    printf("Start of Query3\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[1]);
    qresult = PQexec(Queries[2]);
    gettimeofday(&tpEnd, NULL);
    printf("query3 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    /* query 4 */

    printf("Start of Query4\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[3]);
    gettimeofday(&tpEnd, NULL);
    printf("query4 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);

    /* query 5 */

    printf("Start of Query5\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[4]);
    gettimeofday(&tpEnd, NULL);
    printf("query5 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    /* query 6 */

    printf("Start of Query6\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[5]);
    gettimeofday(&tpEnd, NULL);
    printf("query6 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);

    /* query 7 */

    printf("Start of Query7\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[6]);
    gettimeofday(&tpEnd, NULL);
    printf("query7 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    /* query 8 */

    printf("Start of Query8\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[7]);
    gettimeofday(&tpEnd, NULL);
    printf("query8 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    /* query 9 */

    printf("Start of Query9\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[8]);
    gettimeofday(&tpEnd, NULL);
    printf("query9 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    /* query 10 */
    
    printf("Start of Query10\n");
    gettimeofday(&tpStart, NULL);
    qresult = PQexec(Queries[9]);
    qresult = PQexec(Queries[10]);
    gettimeofday(&tpEnd, NULL);
    printf("query10 nsecs was %ld\n", tpEnd.tv_sec - tpStart.tv_sec);
    PQclear("blank");

    exit(0);
}
