検索操作において、対象となるデータが大量に存在する場合、条件式に一致するデータすべてを参照することは非現実的です。このため、大量のデータが存在する場合は、条件式に一致するデータ件数を取得することが必要です。
日付と場所を検索条件式に指定して、条件に一致するホテルの件数を取得する例を用いて、C APIの使用例を示します。
「2006年7月18日に大阪で宿泊可能なホテルは何件ありますか?」
年月日(2006年7月18日)および場所(大阪)を条件に検索を行います。
以下にC APIを使用したプログラミング例を示します。
#include <stdio.h> #include "libshun.h" /* 検索条件に一致するXML文書の件数を求める */ int main() { /* ハンドル変数群 */ SHUNHCON connectionHandle; SHUNHSTMT statementHandle; /* 作業用変数群 */ int status; /* 入力パラメタ群 */ char *hostName; int portNo; char *ShunFileName; char *queryForm; /* 出力パラメタ群*/ int hitCount; /* エラー用変数群 */ SHUNHANDLE errorHandle; int errorLevel; char *errorMessage; /* 変数の初期化 */ connectionHandle = NULL; statementHandle = NULL; /* コネクションハンドルの割当て */ status = ShunAllocHandle( NULL, &connectionHandle ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)connectionHandle; goto ErrorEnd; } /* ホスト名・ポート番号、Shunsakuファイル名を指定してコネクションを確立 */ hostName = "DirSvr1"; portNo = 23101; ShunFileName = "shunsakuFile1"; status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)connectionHandle; goto ErrorEnd; } /* データ操作ハンドルの割当て */ status = ShunAllocHandle( connectionHandle, &statementHandle ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)connectionHandle; goto ErrorEnd; } /* 検索式の指定 */ queryForm = "/document {/base/prefecture == '大阪' " "AND /information/date == '2006年07月18日'}"; /* API呼出し 検索式を指定してヒット件数を取得 */ status = ShunGetHitCount( statementHandle, queryForm, &hitCount ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)statementHandle; goto ErrorEnd; } /* ヒット件数の取出し */ printf( "ヒット件数 = %d\n", hitCount ); /* データ操作ハンドルの解放 */ status = ShunFreeHandle( statementHandle ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)statementHandle; goto ErrorEnd; } /* コネクションの切断 */ status = ShunDisconnect( connectionHandle ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)connectionHandle; goto ErrorEnd; } /* コネクションハンドルの解放 */ status = ShunFreeHandle( connectionHandle ); if ( status != SHUN_SUCCESS ) { errorHandle = (SHUNHANDLE)connectionHandle; goto ErrorEnd; } return 0; ErrorEnd: /* エラーが発生した際のエラー処理 */ /* エラー情報の取得 */ status = ShunGetErrorMessage(errorHandle, &errorLevel, &errorMessage); if ( status == SHUN_SUCCESS ) { switch ( errorLevel ) { case SHUN_ERROR_CONNECTION: printf("エラーレベル = SHUN_ERROR_CONNECTION\n"); break; case SHUN_ERROR_DATA: printf("エラーレベル = SHUN_ERROR_DATA\n"); break; } printf("エラーメッセージ = %s\n", errorMessage); } else { printf("エラーメッセージ取得エラー : %d\n", status); } /* データ操作ハンドルの解放 */ if ( statementHandle != NULL ) { status = ShunFreeHandle( statementHandle ); if ( status != SHUN_SUCCESS ) { printf("データ操作ハンドル解放エラー : %d\n", status); } } /* コネクションの切断 */ if ( connectionHandle != NULL) { status = ShunDisconnect( connectionHandle ); if ( status != SHUN_SUCCESS ) { printf("コネクション切断エラー : %d\n", status); } } /* コネクションハンドルの解放 */ if ( connectionHandle != NULL) { status = ShunFreeHandle( connectionHandle ); if ( status != SHUN_SUCCESS ) { printf("コネクションハンドル解放エラー : %d\n", status); } } return 1; } |
ヒット件数 = 3