在开发使用 DB2 Extender 的程序之前,您应 熟悉 DB2 Application Development Guide 中描述的 DB2 应用程序开发过程和程序设计技术。开发使用 DB2 Extender 的程序的流程基本上与开发传统 DB2 应用程序的流程相同。
由于 Extender 定义了新的数据结构和函数, 因此应用程序代码将与传统 DB2 应用程序不同。例如,下图显示了一个用 C 语言编码的应用程序, 它使用 Image Extender 来识别存储在数据库表中的 GIF 图象。在识别出图象之后,该程序调用图象浏览器加以显示。
正如示例中所示,使用 DB2 Extender 的应用程序需要执行下列功能:
(1)包括 Extender 定义。示例中的 dmbimage.h 文件是 Image Extender 的包含(首标)文件。该包含文件为 Extender 定义常量、变量和函数原型。
(2)定义包含 UDF 的输入或输出或 API 调用的输入所必需的主变量。在该示例中,hvFormat、hvSize、hvWidth、 hvHeight 和 hvComment 是用于包含 Image Extender UDF 检索到的数据的主变量。主变量 hvImg_hdl 用于包含被指定为 Image Extender API 调用的输入的图象句柄。
(3)指定必需的 UDF 请求。在示例中,SIZE、WIDTH、HEIGHT、COMMENT 和 FORMAT 都是 Image Extender UDF。
(4)指定必需的 API 调用。在该示例中,DBiBrowse 是对某个本地 C 函数的 API 调用, 该函数显示从表中检索到它的句柄的图象。
#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 */ |