Select a simple large object into an open file

The getcd_of sample program from the demo directory shows how to select a simple large object from the database into an open file. The following figure shows a code excerpt that selects the cat_descr column into a file that the user specifies.
Figure 1: Code excerpt from the getcd_of sample program
EXEC SQL BEGIN DECLARE SECTION;
   char db_name[30];
   mlong cat_num;
   loc_t cat_descr;
EXEC SQL END DECLARE SECTION;


if((fd = open(descfl, O_WRONLY)) < 0)
   {
   printf("\nCan't open file: %s, errno: %d\n", descfl, errno);
   EXEC SQL disconnect current;
   printf("GETCD_OF Sample Program over.\n\n"):
        exit(1);
   }
/*
 * Prepare locator structure for select of cat_descr
 */
cat_descr.loc_loctype = LOCFILE;           /* set loctype for open file */
cat_descr.loc_fd = fd;                     /* load the file descriptor */
cat_descr.loc_oflags = LOC_APPEND;         /* set loc_oflags to append */
EXEC SQL select catalog_num, cat_descr     /* verify catalog number */
   into :cat_num, :cat_descr from catalog
   where catalog_num = :cat_num;
if(exp_chk2("SELECT", WARNNOTIFY) != 100)    /* if not found */
   printf("\nCatalog number %ld not found in catalog table\n", 
      cat_num);
else
   {
   if(ret < 0)
      {
      


      exit(1);
      }
   }
To prepare the locator structure for the SELECT statement, the getcd_of program sets the cat_descr locator structure fields as follows:
  • The loc_loctype field is set to LOCFILE to tell Informix® ESQL/C to place the text for the cat_descr column in the open file.
  • The loc_fd field is set to the fd file descriptor to identify the open file.
  • The loc_oflags field is set to LOC_APPEND to specify that the data is to be appended to any data that exists in the file.
To access the file descriptor (loc_fd) field of the locator structure, the getcd_of program uses the name cat_descr.loc_fd. However, the actual name of this field in the locator structure is as follows:
cat_descr.lc_union.lc_file.lc_fd

The shortcut name of loc_fd is defined as a macro in the locator.h file.

After Informix ESQL/C writes data to an open file, it sets the following fields of the locator structure:
  • The loc_size field contains the number of bytes written to the open file.
  • The loc_indicator field contains -1 if the selected simple-large-object value is null.
  • The loc_status field contains the status of the operation: 0 for success and a negative value if an error has occurred. For possible causes of the error, see Error returns in loc_status.