#include <stdio.h>
#include "libshun.h"
/* Set the maximum value of number of hits */
int main()
{
/* Handle variables */
SHUNHCON connectionHandle;
SHUNHSTMT statementHandle;
/* Work variables */
int status;
int i;
/* Input parameters */
char *hostName;
int portNo;
char *ShunFileName;
int startNo;
int returnRequestCount;
char *queryForm;
char *returnForm;
char *sortForm;
/* Output parameters */
int hitCount;
int returnCount;
int returnableCount;
SHUNRECID *recID;
SHUNDATA *dataInfo;
SHUNPOS *firstPosition, *lastPosition;
int isHitCountLimitOver;
/* Error variables */
SHUNHANDLE errorHandle;
int errorLevel;
char *errorMessage;
/* Variable initialization */
connectionHandle = NULL;
statementHandle = NULL;
/* Connection handle allocation */
status = ShunAllocHandle( NULL, &connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = connectionHandle;
goto ErrorEnd;
}
/* Specify host name, port number and Shunsaku file name and establish connection */
hostName = "DirSvr1";
portNo = 33101;
ShunFileName = "shunsakuFile1";
status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Allocation of data manipulation handles */
status = ShunAllocHandle( connectionHandle, &statementHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Specify number of items to find, search expression, etc.*/
startNo = 1;
returnRequestCount = 5;
queryForm = "/document/base/name = 'Hotel'";
returnForm = "/document/base/name/text(), /document/base/price/text()";
sortForm = "/document/base/price/text()";
/* Set the limiter of number of hits */
status = ShunSetStatementAttr( statementHandle, SHUN_ATTR_HIT_COUNT_LIMIT, 5 );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
/* Call API and perform data search */
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;
}
/* Obtain that whether the number of hits has exceeded. */
status = ShunGetStatementAttr( statementHandle, SHUN_ATTR_HIT_COUNT_LIMIT_OVER, &isHitCountLimitOver );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
if ( isHitCountLimitOver == SHUN_TRUE ) {
printf( "The number of hits has exceeded the limiter of number of hits.\n" );
}
/* Extract number of hits */
printf( "Number of hits = %d\n", hitCount );
/* Display data that was found */
if ( hitCount > 0 ) {
for (i = 0; i < returnCount; ++i) {
if ( dataInfo[i].Data_Len > 0 ) {
printf( "[Result] Item No %d =%s\n", startNo + i, dataInfo[i].Data);
}
else {
printf( "[Result] Item No %d = No data\n", startNo + i );
}
}
}
/* Release data manipulation handle */
status = ShunFreeHandle( statementHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Disconnect */
status = ShunDisconnect( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Release connection handle */
status = ShunFreeHandle( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = NULL;
goto ErrorEnd;
}
return 0;
ErrorEnd: /* Processing to perform in the event of an error */
/* Obtain error information */
status = ShunGetErrorMessage(errorHandle,
&errorLevel, &errorMessage);
if ( status == SHUN_SUCCESS ) {
switch ( errorLevel ) {
case SHUN_ERROR_CONNECTION:
printf("Error level = SHUN_ERROR_CONNECTION\n");
break;
case SHUN_ERROR_DATA:
printf("Error level = SHUN_ERROR_DATA\n");
break;
}
printf("Error level = %s\n", errorMessage);
}
else {
printf("Error message acquisition error : %d\n", status);
}
/* Release data manipulation handle */
if ( statementHandle != NULL ) {
status = ShunFreeHandle( statementHandle );
if ( status != SHUN_SUCCESS ) {
printf("Data manipulation handle release error : %d\n", status);
}
}
/* Disconnect */
if ( connectionHandle != NULL) {
status = ShunDisconnect( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf("Disconnection error : %d\n", status);
}
}
/* Release connection handle */
if ( connectionHandle != NULL) {
status = ShunFreeHandle( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf("Connection handle release error : %d\n", status);
}
}
return 1;
} |