IBM Books

Image, Audio, and Video Extenders Administration and Programming


Before you begin programming for DB2 extenders

Before you develop a program that uses the DB2 extenders, you should be familiar with the DB2 application development process and programming techniques as described in DB2 Application Development Guide. The process for developing programs that use DB2 extenders is essentially the same as that for traditional DB2 applications.

Your application program code will differ from a traditional DB2 application because of the new data types and functions that are defined by the extenders. For example, the following figure shows an application coded in C that uses the Image Extender to identify GIF images stored in a database table. After the images are identified, the program calls an image browser to display them.

As the example illustrates, an application that uses a DB2 extender needs to perform the following functions:

(1)Include extender definitions. The dmbimage.h file in the example is the include (header) file for the Image Extender. The include file defines the constants, variables, and function prototypes for the extender.

(2)Define host variables as necessary to contain input to or output from a UDF, or input to an API call. In the example, hvFormat, hvSize, hvWidth, hvHeight, and hvComment are host variables that are used to contain data that is retrieved by the Image Extender UDFs. The host variable hvImg_hdl is used to contain an image handle that is specified as input to an Image Extender API call.

(3)Specify UDF requests as necessary. In the example, SIZE, WIDTH, HEIGHT, COMMENT, and FORMAT are Image Extender UDFs.

(4)Specify API calls as necessary. In the example, DBiBrowse is an API call to a local C function that displays images whose handles are retrieved from a table.

Figure 18. An application that uses a DB2 extender

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlcodes.h>
#include <dmbimage.h> (1)
 
int count=0;
 
long
main(int argc,char *argv[])
{
EXEC SQL BEGIN DECLARE SECTION; (2)
         char hvImg_hdl[251];                   /* image handle */
         char hvDBName[19];                     /* database name */
         char hvName[40];                       /* employee name */
         char hvFormat[9];                      /* image format */
         long hvSize;                           /* image size */
         long hvWidth;                          /* image width */
         long hvHeight;                         /* image height */
         struct  {
                  short len;
                  char data[32700]
         } hvComment;                           /* comment about the image */
 EXEC SQL END DECLARE SECTION;
 
/*  Connect to database  */
 
strcpy(hvDBName, argv[1]);                      /* copy the database name */
 
EXEC SQL CONNECT TO :hvDBName IN SHARE MODE;
/*
 * Set current function path
*/
EXEC SQL SET CURRENT FUNCTION PATH = mmdbsys, CURRENT FUNCTION PATH;
/*
 * Select (query) using Image Extender UDF
 *
 * The SQL statement below finds all images in GIF format.
 */
EXEC SQL DECLARE c1 CURSOR FOR
         SELECT PICTURE, NAME,                (3)
                SIZE(PICTURE), WIDTH(PICTURE),
                HEIGHT(PICTURE), COMMENT(PICTURE)
         FROM EMPLOYEE
         WHERE PICTURE IS NOT NULL AND
               FORMAT(PICTURE) LIKE 'GIF%'
FOR FETCH ONLY;
 
EXEC SQL OPEN c1;
for (;;) {
         EXEC SQL FETCH c1 INTO :hvImg_hdl, :hvName, :hvSize,
                                :hvWidth, :hvHeight, :hvComment;
         if (SQLCODE != 0)
                   break;
 
         printf("\nRecord %d:\n", ++count);
         printf("employee name = '%s'\n", hvName);
         printf("image size = %d bytes, width=%d, height=%d\n",
                                                 hvSize, hvWidth, hvHeight);
         hvComment.data[Comment.len]='\0';
         printf("comment len = %d\n", hvComment.len);
 printf("comment = %s\n", hvComment.data);
/*
* The API call below displays the images
*/
(4) rc=DBiBrowse ("ib %s",MMDB_PLAY_HANDLE,hvImg_hdl,
                         MMDB_PLAY_WAIT);
}
 
EXEC SQL CLOSE c1;
 
/* end of program */


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]