在開發使用 DB2 Extender 的程式之前,您應該熟悉 DB2 應用程式開發程序和程式設計技術,相關資訊請參閱 DB2 Application Development Guide。 開發使用 DB2 Extender 的程式,其程序本質上與傳統 DB2 應用程式相同。
您的應用程式之程式碼與傳統 DB2 應用程式有所不同,是因為有 Extender 所定義的新資料類型和 函數。 例如, 下列圖表中以 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 是用來包含影像 handle, 該 handle 被指定為 Image Extender API 呼叫的輸入。
(3)依需要指定 UDF 要求。在範例中,SIZE、 WIDTH、 HEIGHT、COMMENT 及 FORMAT 皆為 Image Extender UDF。
(4)依需要指定 API 呼叫。在範例中,DBiBrowse 是呼叫本端 C 函數的 API, 而函數顯示的影像,其 handle 是擷取自表格。
#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 */ |