Insert from an lvarchar pointer host variable

The following example code illustrates the steps to insert data from an lvarchar pointer host variable to an opaque type column. To simplify the example, the code does not check returned values for errors.
#include <stdio.h>
exec sql include “polyvar.h” /* includes udt - polygon_type */

main()
{
   exec sql begin declare section;
      lvarchar ‘polygon_type’ *mypoly1
      char *buffer;
      int size, p_id, len;
   exec sql end declare section;

   exec sql create table polygon_tab (p_id int, poly polygon_type);
   ifx_var_flag(&mypoly1, 0); /* User does allocation */
   buffer = malloc(50);

   /* String representation of mypoly1 copied into buffer*/
   strcpy(buffer, “5, 10, 20 15, 10, 5, -1, 0, 0”); 
   size = strlen(buffer);
   ifx_var_alloc(&myploy1, size+1); /* Allocate memory for data in 
                                     * mypoly1 */
   ifx_var_setlen(&myploy1, size); /* Set length of data bufferin 
                                    * mypoly1 */
   ifx_var_setdata(&mypoly1, buffer, size); /* Store data inside mypoly1 
                                            */
   exec sql insert into polygon_tab values (1, :mypoly1);
   ifx_var_setnull(&mypoly1, 1); /* Set data buffer in mypoly1 to NULL 
                                  */
   ifx_var_dealloc(&mypoly1); /* Deallocate the data buffer in mypoly1 
                               */
   free (buffer);
}
The example code performs the following steps:
  1. It declares the lvarchar pointer host variable, *mypoly1.
  2. It creates a table that consists of an integer ID column, p_id, and a column of polygons, polygon_type.
  3. It calls the ifx_var_flag() function to specify that it will allocate memory for the data buffer (flag equals 0).
  4. It creates a buffer, copies the string representation of the polygon to it, and sets the size variable to the size of the buffer.
  5. It calls ifx_var_alloc(), ifx_var_setlen(), and ifx_var_setdata() to allocate the data transfer buffer, set the length of the buffer, and copy the data from the application buffer to the data transfer buffer.
  6. It inserts an ID value of 1 and mypoly1 to the polygon_tab table.

Copyright© 2019 HCL Technologies Limited