Insert a simple large object from a named file

The updcd_nf sample program from the demo directory shows how to insert a simple large object from a named file into the database. The program updates the cat_descr TEXT column from a named input file. The program assumes that this input file has the following format:
Babe Ruth signature glove. Black leather. Infield/outfield
   style. Specify right- or left-handed.
The following figure shows a code excerpt that updates the cat_descr column in the catalog table from text in a named file.
Figure 1. Code excerpt from the updcd_nf sample program
EXEC SQL BEGIN DECLARE SECTION;
   mlong cat_num;
   loc_t cat_descr;
EXEC SQL END DECLARE SECTION;


   cat_descr.loc_loctype = LOCMEMORY;         /* set loctype for in memory */
   cat_descr.loc_bufsize = -1;                /* let server get memory */
   EXEC SQL select catalog_num, cat_descr     /* verify catalog number */
       into :cat_num, :cat_descr from catalog
      where catalog_num = :cat_num;

   /* if error,display and quit */
   if ((ret = exp_chk2("SELECT", WARNNOTIFY)) == 100) 
      {
      printf("\nCatalog number %ld not found in catalog table\n",
         cat_num);
      EXEC SQL disconnect current;
      printf("UPDCD_NF Sample Program over.\n\n");
      exit(1);
      }
   if(ret<0)
      {
      EXEC SQL disconnect current;
      printf("UPDCD_NF Sample Program over.\n\n");
      exit(1);
      }
   prdesc();                                   /* print current cat_descr */

   /* Update? */
   ans[0] = ' ';
   while((ans[0] = LCASE(ans[0]))  != 'y' && ans[0]  != 'n')
      {
      printf("Update this description? (y/n) ");
      scanf("%1s", ans);
      }
   if(ans[0] == 'y')
      {
      cat_descr.loc_loctype = LOCFNAME;       /* set type to named file */
      cat_descr.loc_fname = descfl;           /* supply file name */
      cat_descr.loc_oflags = LOC_RONLY;       /* set file-open mode (read) */
      cat_descr.loc_size = -1;                /* set size to size of file */
      EXEC SQL update catalog
         set cat_descr = :cat_descr           /* update cat_descr column */
         where catalog_num = :cat_num;
      if(exp_chk2("UPDATE", WARNNOTIFY) < 0)   /* check status */
         {
         EXEC SQL disconnect current;
         printf("UPDCD_NF Sample Program over.\n\n");
         exit(1);
         }
      printf("Update complete.\n");
      }

The updcd_nf program in Code excerpt from the updcd_nf sample program first performs a SELECT statement on the catalog table for a catalog number that the user enters in response to a prompt. The SELECT statement returns the catalog_num and cat_descr columns. The prdesc() function (Guide to the prdesc.c file) displays the current content of cat_descr.

The program then asks whether the user wants to update this description. If the user answers yes (ans[0] = = 'y'), the updcd_nf program prepares the locator structure as follows to update the cat_descr column from text in a file that the user has specified:
  • The cat_descr.loc_loctype field is set to LOCFNAME to indicate that the source of the update text is a named file.
  • The cat_descr.loc_fname field is set to descfl, the name of the file that contains the simple-large-object data.
  • The cat_descr.loc_oflags field is set to LOC_RONLY to tell Informix® ESQL/C to open the file in read-only mode.
  • The cat_descr.loc_size field is set to -1 to tell Informix® ESQL/C to transfer the simple large object all at once, not to transfer it in smaller pieces, one piece at a time. You can also set the loc_oflags field to the LOC_USEALL mask to perform this operation.

If you insert a null simple-large-object value, your program also needs to set the loc_indicator field to -1.

After Informix® ESQL/C reads data from the named file and sends it to the database server, Informix® ESQL/C updates the loc_size field with the number of bytes read from the named file and sent to the database server. Informix® ESQL/C also sets the loc_status field to indicate 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.