/**************************************************************************
 *
 * linefromshape.c
 *
 * This program demonstrates how to insert spatial data into a database
 * in ESRI shapefile format using the Informix Spatial DataBlade's
 * SE_LineFromShape function.
 *
 * This program creates a table and inserts several polygons into it.
 * For an example program which shows how to insert all spatial types,
 * please see load_shapes.c
 *
 * The following command will likely compile this source:
 *
 *   cc -o linefromshape linefromshape.c \
 *        -I$INFORMIXDIR/incl/cli -L$INFORMIXDIR/lib/cli -lifcli -lifdmr
 *
 **************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>

#ifdef WINNT
#include <io.h>
#include <windows.h>
#include <conio.h>
#endif

#include <infxcli.h>     /* ODBC typedefs and data structures */
#include "odbcutils.c"   /* ODBC utility functions */

/*
 * Constants, macros, typedefs, data structures, & functions for 
 * processing ESRI shapefile format data.
 */
#include "commfuncs.h"
#include "commfuncs.c"
#include "shapefuncs.c"


void main (int argc, char **argv)
{
    SQLHDBC       hdbc;
    SQLHENV       henv;
    SQLHSTMT      hstmt;
    char          sql_stmt[200];
    int           rc;
    SDWORD        pcbvalue1, pcbvalue2, pcbvalue3;
    Geometry      geom = {0};
    Point         pt[30];
    int           max_alloced = 0;
    int           sewer_id;
    int           sewer_class;
    char         *sewer_shape_buf;
    int           sewer_shape_len;
    int           srid;

    /* Check for the correct number of arguments entered. */
    if (argc < 2)
    {
        printf ("Usage: %s <datasource> [<srid>]\n", argv[0]);
        exit (1);
    }

    /* Connect to the database. */
    server_connect ((UCHAR *) argv[1], &henv, &hdbc);

    /* Obtain srid to use for inserts */
    srid = (argc > 2) ? atoi(argv[2]) : 0;

    /* Allocate memory for the SQL statement handle and
     * associate the statement handle with the connection handle. */
    rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
	returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");

    rc = SQLExecDirect (hstmt, (UCHAR *) "DROP TABLE sewers", SQL_NTS);
    rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE sewers (sewer_id integer, class integer, sewer ST_Linestring)", SQL_NTS);
    returncode_check (NULL, hstmt, rc, "SQLExecDirect");

    /* Initialize entries for non-spatial columns in the table  */
    sewer_id = 123;
    sewer_class = 100;
    
    /* Populate the linestring point array. */
    pt[0].x = 0;   pt[0].y = 0;
    pt[1].x = 1;   pt[1].y = 1;
    pt[2].x = 2;   pt[2].y = 2;
    pt[3].x = 3;   pt[3].y = 3;
    pt[4].x = 4.5; pt[4].y = 5.4;
    pt[5].x = 7;   pt[5].y = 7;

    geom.type = geomLineString;
    geom.num_points = 6;
    geom.num_parts  = 1;
    geom.pt = pt;

    /* Convert the linestring to an ESRI shape. */
    geom_to_shape (&geom, &max_alloced, &sewer_shape_len, &sewer_shape_buf);
               

    /* INFORMIX_EXTEST_BEGIN linefromshape */

    /* Create the SQL insert statement to populate the sewerlines
     * table. The question marks are parameter markers that indicate
     * the column values that will be inserted at run time. */
    sprintf(sql_stmt,
            "INSERT INTO sewers (sewer_id,class,sewer) "
            "VALUES(?, ?, SE_LineFromShape(?, %d))", srid);

    /* Prepare the SQL statement for execution. */
    rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
    returncode_check (NULL, hstmt, rc, "SQLPrepare");

    /* Bind the sewer_id to the first parameter. */
    pcbvalue1 = 0;
    rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
                           SQL_INTEGER, 0, 0,
                           &sewer_id, 0, &pcbvalue1);
    returncode_check (NULL, hstmt, rc, "SQLBindParameter");

    /* Bind the sewer_class to the second parameter. */
    pcbvalue2 = 0;
    rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_SLONG,
                           SQL_INTEGER, 0, 0,
                           &sewer_class, 0, &pcbvalue2);
    returncode_check (NULL, hstmt, rc, "SQLBindParameter");

    /* Bind the sewer geometry to the third parameter. */
    pcbvalue3 = sewer_shape_len;
    rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY,
                           SQL_INFX_UDT_LVARCHAR, sewer_shape_len, 0,
                           sewer_shape_buf, sewer_shape_len, &pcbvalue3);
    returncode_check (NULL, hstmt, rc, "SQLBindParameter");

    /* Execute the insert statement. */
    rc = SQLExecute (hstmt);
    returncode_check (NULL, hstmt, rc, "SQLExecute");

    /* INFORMIX_EXTEST_END linefromshape */

    SQLFreeStmt (hstmt, SQL_CLOSE);          /* Close the statement handle */
    SQLFreeHandle (SQL_HANDLE_STMT, hstmt);  /* Free the statement handle */
    SQLDisconnect (hdbc);                    /* Close the connection */
    SQLFreeHandle (SQL_HANDLE_DBC, hdbc);    /* Free the database handle */
    SQLFreeHandle (SQL_HANDLE_ENV, henv);    /* Free the ODBC environment */
    printf( "\nTest Complete\n");
}