#include <stdio.h>
#include "libshun.h"
/* Obtain the number of XML documents that match specified search conditions */
int main()
{
/* Handle variables */
SHUNHCON connectionHandle;
SHUNHSTMT statementHandle;
/* Work variables */
int status;
/* Input parameters */
char *hostName;
int portNo;
char *ShunFileName;
char *queryForm;
/* Output parameters */
int hitCount;
/* 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 = (SHUNHANDLE)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 search expression */
queryForm = "/document {/base/city == 'Sydney' "
"AND /information/date == '2006/07/18'}";
/* Call API then specify search expression and obtain number of hits */
status = ShunGetHitCount(
statementHandle,
queryForm,
&hitCount );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
/* Extract number of hits */
printf( "Number of hits = %d\n", hitCount );
/* Release data manipulation handle */
status = ShunFreeHandle( statementHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
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 = (SHUNHANDLE)connectionHandle;
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 message = %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;
} |