#include <stdio.h>
#include <string.h>
#include "libshun.h"
/* Use the direct access to search data */
int main()
{
/* Handle variables */
SHUNHCON connectionHandle;
SHUNHSTMT statementHandle;
/* Working variables */
int status;
int i;
/* Input parameters */
char *hostName;
int portNo;
char *ShunFileName;
char *returnForm;
char *keyName;
SHUNKEY keyInfo_search[1];
/* Output Parameters*/
int returnCount;
SHUNRECID *recID;
SHUNDATA *dataInfo;
SHUNKEY *keyInfo_out;
/* Error variables */
SHUNHANDLE errorHandle;
int errorLevel;
char *errorMessage;
/* Initialize variables */
connectionHandle = NULL;
statementHandle = NULL;
/* Allocate connection handle */
status = ShunAllocHandle( NULL, &connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = connectionHandle;
goto ErrorEnd;
}
/* Establish a connection by specifying the host name, port number and Shunsaku File name */
hostName = "DirSvr1";
portNo = 33101;
ShunFileName = "shunsakuFile1";
status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Allocate data manipulation handle */
status = ShunAllocHandle( connectionHandle, &statementHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
returnForm = "/";
/* Set the direct access key */
keyName = "key";
keyInfo_search[0].Key = "0001";
keyInfo_search[0].Key_Len = strlen( keyInfo_search[0].Key );
/* Call the API and search data directly */
status = ShunSearchKey( statementHandle,
keyName,
SHUN_KEY_COMPLETE_MATCH,
1,
keyInfo_search,
returnForm,
&returnCount,
&recID,
&dataInfo,
&keyInfo_out );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)statementHandle;
goto ErrorEnd;
}
/* Display the search results */
printf( "Number of hits = %d\n", returnCount );
for (i = 0; i < returnCount; ++i) {
printf( "[Result] Item No. %d \n", i + 1 );
printf( "Direct access key: %s\n", keyInfo_out[i].Key );
printf( "Data :\n" );
printf( "%s\n", dataInfo[i].Data );
}
/* Release the data manipulation handle */
status = ShunFreeHandle( statementHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Close connection */
status = ShunDisconnect( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = (SHUNHANDLE)connectionHandle;
goto ErrorEnd;
}
/* Release the connection handle */
status = ShunFreeHandle( connectionHandle );
if ( status != SHUN_SUCCESS ) {
errorHandle = NULL;
goto ErrorEnd;
}
return 0;
ErrorEnd: /* Error processing if an error occurs */
/* Get 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 obtaining error message : %d\n", status);
}
/* Release the data manipulation handle */
if ( statementHandle != NULL ) {
status = ShunFreeHandle( statementHandle );
if ( status != SHUN_SUCCESS ) {
printf("Error releasing data manipulation handle : %d\n", status);
}
}
/* Close connection */
if ( connectionHandle != NULL) {
status = ShunDisconnect( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf("Connection close error : %d\n", status);
}
}
/* Release the connection handle */
if ( connectionHandle != NULL) {
status = ShunFreeHandle( connectionHandle );
if ( status != SHUN_SUCCESS ) {
printf("Connection handle release error : %d\n", status);
}
}
return 1;
} |