#include <stdio.h>
#include "libshun.h"
/* ヒット件数の上限値を設定する */
int main()
{
/* ハンドル変数群 */
SHUNHCON connectionHandle;
SHUNHSTMT statementHandle;
/* 作業用変数群 */
int status;
int i;
/* 入力パラメータ群 */
char *hostName;
int portNo;
char *ShunFileName;
int startNo;
int returnRequestCount;
char *queryForm;
char *returnForm;
char *sortForm;
/* 出力パラメータ群*/
int hitCount;
int returnCount;
int returnableCount;
SHUNRECID *recID;
SHUNDATA *dataInfo;
SHUNPOS *firstPosition, *lastPosition;
int isHitCountLimitOver;
/* エラー用変数群 */
SHUNHANDLE errorHandle;
int errorLevel;
char *errorMessage;
/* 変数の初期化 */
connectionHandle = NULL;
statementHandle = NULL;
/* コネクションハンドルの割当て */
status = ShunAllocHandle( NULL, &connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = connectionHandle;
goto ErrorEnd;
}
/* ホスト名・ポート番号、Shunsakuファイル名を指定してコネクションを確立 */
hostName = "DirSvr1";
portNo = 33101;
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;
}
/* 検索件数、検索式などの指定*/
startNo = 1;
returnRequestCount = 5;
queryForm = "/document/base/name = 'ホテル'";
returnForm = "/document/base/name/text(), /document/base/price/text()";
sortForm = "/document/base/price/text()";
/* ヒット件数リミッタを設定 */
status = ShunSetStatementAttr( statementHandle, SHUN_ATTR_HIT_COUNT_LIMIT, 5 );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
/* API呼出し データ検索 */
status = ShunSearch(
statementHandle,
startNo,
NULL,
0,
returnRequestCount,
queryForm,
returnForm,
sortForm,
&hitCount,
&returnCount,
&returnableCount,
&recID,
&dataInfo,
&firstPosition,
&lastPosition );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
/* ヒット件数がオーバーしたかどうか取得 */
status = ShunGetStatementAttr( statementHandle, SHUN_ATTR_HIT_COUNT_LIMIT_OVER, &isHitCountLimitOver );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
if ( isHitCountLimitOver == SHUN_TRUE ) {
printf( "ヒット件数リミッタをオーバーしました。\n" );
}
/* ヒット件数の取出し */
printf( "ヒット件数 = %d\n", hitCount );
/* 検索したデータの表示 */
if ( hitCount > 0 ) {
for (i = 0; i < returnCount; ++i) {
if ( dataInfo[i].Data_Len > 0 ) {
printf( "[結果]%d件目 =%s\n", startNo + i, dataInfo[i].Data);
}
else {
printf( "[結果]%d件目 = データなし\n", startNo + i );
}
}
}
/* データ操作ハンドルの解放 */
status = ShunFreeHandle( statementHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* コネクションの切断 */
status = ShunDisconnect( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* コネクションハンドルの解放 */
status = ShunFreeHandle( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = NULL;
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;
} |