wkbtosql.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**************************************************************************
  2. *
  3. * wkbtosql.c
  4. *
  5. * This program demonstrates how to insert spatial data into a database
  6. * in OGIS well-known binary format using the Informix Spatial DataBlade's
  7. * ST_WkbToSql function.
  8. *
  9. * ST_WkbToSql is a special variation on ST_GeomFromWkb which
  10. * assigns srid = 0 to the input shape.
  11. *
  12. * This program creates a table and inserts several polygons into it.
  13. * For an example program which shows how to insert all spatial types,
  14. * please see load_wkb.c
  15. *
  16. * The following command will likely compile this source:
  17. *
  18. * cc -o wkbtosql wkbtosql.c \
  19. * -I$INFORMIXDIR/incl/cli -L$INFORMIXDIR/lib/cli -lifcli -lifdmr
  20. *
  21. **************************************************************************/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <float.h>
  26. #ifdef WINNT
  27. #include <io.h>
  28. #include <windows.h>
  29. #include <conio.h>
  30. #endif
  31. #include <infxcli.h> /* ODBC typedefs and data structures */
  32. #include "odbcutils.c" /* ODBC utility functions */
  33. /*
  34. * Constants, macros, typedefs, data structures, & functions for
  35. * processing OGIS well-known-binary format data.
  36. */
  37. #include "commfuncs.h"
  38. #include "commfuncs.c"
  39. #include "wkbfuncs.c"
  40. void main (int argc, char **argv)
  41. {
  42. SQLHDBC hdbc;
  43. SQLHENV henv;
  44. SQLHSTMT hstmt;
  45. char sql_stmt[200];
  46. int rc;
  47. SDWORD pcbvalue1, pcbvalue2;
  48. Geometry geom = {0};
  49. Point pt[30];
  50. int suboffsets[5];
  51. int max_alloced = 0;
  52. int lot_id;
  53. char *lot_wkb_buf;
  54. int lot_wkb_len;
  55. /* Check for the correct number of arguments entered. */
  56. if (argc < 2)
  57. {
  58. printf ("Usage: %s <datasource>\n", argv[0]);
  59. exit (1);
  60. }
  61. /* Connect to the database. */
  62. server_connect ((UCHAR *) argv[1], &henv, &hdbc);
  63. /* Allocate memory for the SQL statement handle and
  64. * associate the statement handle with the connection handle. */
  65. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
  66. returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");
  67. rc = SQLExecDirect (hstmt, (UCHAR *) "DROP TABLE lots", SQL_NTS);
  68. rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE lots (lot_id integer, lot ST_Geometry)", SQL_NTS);
  69. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  70. /* Initialize entries for non-spatial columns in the table */
  71. lot_id = 1010;
  72. /* Populate the polygon point array */
  73. suboffsets[0] = 0;
  74. pt[0].x = 2; pt[0].y = 57;
  75. pt[1].x = 21.5; pt[1].y = 57;
  76. pt[2].x = 21.5; pt[2].y = 38;
  77. pt[3].x = 2; pt[3].y = 38;
  78. pt[4].x = 2; pt[4].y = 57;
  79. geom.type = geomPolygon;
  80. geom.num_points = 5;
  81. geom.num_parts = 1;
  82. geom.num_subparts = 1;
  83. geom.suboffsets = suboffsets;
  84. geom.pt = pt;
  85. /* Convert the points to a Well Known Binary
  86. * representation of a polygon. */
  87. geom_to_wkb (&geom, &max_alloced, &lot_wkb_len, &lot_wkb_buf);
  88. /* INFORMIX_EXTEST_BEGIN wkbtosql */
  89. /* Create the SQL insert statement to populate the lots table.
  90. * The question marks are parameter markers that indicate the
  91. * column values that will be inserted at run time. */
  92. sprintf(sql_stmt,
  93. "INSERT INTO lots (lot_id, lot) "
  94. "VALUES (?, ST_WKBToSQL(?))");
  95. /* Prepare the SQL statement for execution */
  96. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  97. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  98. /* Bind the lot_id to the first parameter. */
  99. pcbvalue1 = 0;
  100. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
  101. SQL_INTEGER, 0, 0,
  102. &lot_id, 0, &pcbvalue1);
  103. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  104. /* Bind the lot geometry to the second parameter. */
  105. pcbvalue2 = lot_wkb_len;
  106. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  107. SQL_INFX_UDT_LVARCHAR, lot_wkb_len, 0,
  108. lot_wkb_buf, lot_wkb_len, &pcbvalue2);
  109. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  110. /* Execute the insert statement. */
  111. rc = SQLExecute (hstmt);
  112. returncode_check (NULL, hstmt, rc, "SQLExecute");
  113. /* INFORMIX_EXTEST_END wkbtosql */
  114. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  115. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  116. SQLDisconnect (hdbc); /* Close the connection */
  117. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  118. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  119. printf( "\nTest Complete\n");
  120. }