Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX - |
Contents
Index
![]() ![]() |
Appendix J Sample C Programs | > J.1 Searching Data |
When performing a search, the user sometimes prefers to obtain the search results after the value of a specific element has been aggregated.
To show how the C APIs are used, the following example specifies the date and location in the search conditional expression and extracts the number of hotels that match the conditions, the most expensive accommodation rate, the cheapest accommodation rate, and the average accommodation rate.
'Among the hotels with a vacancy on 2006/07/18, I want to find the most expensive hotel rate, the cheapest hotel rate, and the average hotel rate in each city.'
Perform the search using the date (2006/07/18) as the search condition.
The following is a sample program using the C APIs.
#include <stdio.h> #include "libshun.h" /* Obtain the XML documents that match the specified search conditions after the values have been aggregated */ int main() { /* Handle variables */ SHUNHCON connectionHandle; SHUNHSTMT statementHandle; /* Work variables */ int status; int i, k; /* 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; /* 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 number of items to find, search expression, etc. */ startNo = 1; returnRequestCount = 30; queryForm = "/document/information/date == '2006/07/18'"; returnForm = "/document/base/city/text()," "count(/document/base/city/text())," "max(/document/base/price/text())," "min(/document/base/price/text())," "avg(/document/base/price/text())"; sortForm = "/document/base/city/text()"; /* 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; } /* 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 ) { /* To make retrieved data well formed, convert \001 and \002 to spaces */ for (k = 0; k < dataInfo[i].Data_Len; ++k) { switch ( dataInfo[i].Data[k] ) { case '\001': dataInfo[i].Data[k] = ' '; break; case '\002': dataInfo[i].Data[k] = ' '; break; default: break; } } 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)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; } |
Number of hits = 6 [Result]Item No. 1 = Adelaide 3 300 100 200 [Result]Item No. 2 = Sydney 3 350 150 250 |
Contents
Index
![]() ![]() |