Insert from a var binary host variable

To insert the data that a var binary host variable contains into an opaque-type column, the code fragment in Accessing the internal format of the image opaque data type with a var binary host variable takes the following steps:
  1. Loads the image data from an external JPEG, GIF, or PPM file into the image_t internal data structure.

    The load_image() C routine loads the user_image structure from an external file. The definition of the image_t internal data structure, which Internal data structures for the image opaque data type shows, must be available to your program. Therefore, the code fragment includes the image.h header file, which defines the image_t structure.

    The getsize() C function is provided as part of the support for the image opaque type; it returns the size of the image_t structure.

  2. Allocates memory for the data buffer of the var binary host variable, vbin_image.

    The ifx_var_flag() function with a flag value of 0 notifies that the application will perform memory allocation for the vbin_image host variable. The ifx_var_alloc() function then allocates for the data buffer the number of bytes that the image data requires (imgsz).

  3. Stores the image_t structure in the data buffer of the vbin_image host variable.

    The ifx_var_setdata() function saves the data that the user_image structure contains into the vbin_image data buffer. This function also requires the size of the data buffer, which the getsize() function has returned.

  4. Inserts the data that the vbin_image data buffer contains into the image_col opaque-type column.

    When the database server executes the INSERT statement, it calls the receive support function for the image data type to perform any translation necessary between the internal format of the data that the client application has sent (image_t) and the internal format of the image data type on disk.

  5. Deallocates the data buffer of the vbin_image host variable.

    The ifx_var_dealloc() function deallocates the vbin_image data buffer.

To insert a null value into an opaque-type column with a var binary host variable, you can use either of the following methods:
  • Set an indicator variable that is associated with a var binary host variable to -1.
    The following code fragment uses the image_ind indicator variable and the vbin_image host variable to insert a null value into the circle_col column:
    #include <image.h>;
    
    EXEC SQL BEGIN DECLARE SECTION;
       var binary 'image' vbin_image;
       int image_ind;
    EXEC SQL END DECLARE SECTION;
    
    image_ind = -1;
    EXEC SQL insert into image_tab 
       values (:vbin_image:image_ind);
  • Use the ifx_var_setnull() function to set the data buffer of the var binary host variable to a null value.
    For the same vbin_image host variable, the following lines use the ifx_var_setnull() function to insert a null value into the circle_col column:
    ifx_var_setnull(&vbin_image, 1);
    EXEC SQL insert into image_tab values (:vbin_image);