#include <stdio.h>
#include "libshun.h"
/* Obtain all instances of specific XML documents */
int main()
{
/* Handle variables */
SHUNHCON connectionHandle;
SHUNHSTMT statementHandle_serch;
SHUNHSTMT statementHandle_rec_id;
/* 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_search;
SHUNRECID *recID_search_rec_id;
SHUNDATA *dataInfo_serch;
SHUNDATA *dataInfo_rec_id;
SHUNPOS *firstPosition, *lastPosition;
/* Error variables */
SHUNHANDLE errorHandle;
int errorLevel;
char *errorMessage;
/* Variable initialization */
connectionHandle = NULL;
statementHandle_serch = NULL;
statementHandle_rec_id = 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_serch );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Specify number of items to find, search expression, etc. */
startNo = 1;
returnRequestCount = 30;
queryForm = "/document {/base/city == 'Sydney' "
"AND /information/date == '2006/07/18'}";
returnForm = "/document/base/name, /document/base/price";
sortForm = NULL;
/* Call API and perform data search */
status = ShunSearch(
statementHandle_serch,
startNo,
NULL,
0,
returnRequestCount,
queryForm,
returnForm,
sortForm,
&hitCount,
&returnCount,
&returnableCount,
&recID_search,
&dataInfo_serch,
&firstPosition,
&lastPosition );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_serch;
goto ErrorEnd;
}
/* Extract number of hits */
printf( "Number of hits = %d\n", hitCount );
printf( "Number of responses = %d\n", returnCount );
/* Allocation of data manipulation handles */
status = ShunAllocHandle( connectionHandle, &statementHandle_rec_id );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Obtain all data based on ID */
if ( hitCount > 0 ) {
status = ShunSearchRecId(
statementHandle_rec_id,
returnCount,
recID_search,
&recID_search_rec_id,
&dataInfo_rec_id );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_rec_id;
goto ErrorEnd;
}
for (i = 0; i < returnCount; ++i) {
printf( "[Details]Item No. %d =%s\n", startNo + i, dataInfo_rec_id[i].Data);
}
}
/* Release data manipulation handle */
status = ShunFreeHandle( statementHandle_rec_id );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_rec_id;
goto ErrorEnd;
}
/* Release data manipulation handle */
status = ShunFreeHandle( statementHandle_serch );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle_serch;
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_rec_id != NULL ) {
status = ShunFreeHandle( statementHandle_rec_id );
if ( status != SHUN_SUCCESS ) {
printf("Data manipulation handle release error: %d\n", status);
}
}
/* Release data manipulation handle */
if ( statementHandle_serch != NULL ) {
status = ShunFreeHandle( statementHandle_serch );
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;
} |