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 they have been sorted according to a specific element.
To show how the C APIs are used, the following example specifies the date and location in the search conditional expression, obtains the number of hotels that match the conditions and partial information about those hotels, and then uses the accommodation rate as a key to sort the results.
'I would like to know 30 hotels in Sydney that are available on 2006/07/18, and I want to obtain the results sorted in descending order according to the accommodation rate.
Specify the date (2006/07/18) and the location (Sydney) as search conditions and execute the search. Then, obtain the top 30 hotel names after they have been sorted in descending order according to the accommodation rate.
The following is a sample program using the C APIs.
#include <stdio.h> #include "libshun.h" /* Obtain XML documents that match specified search conditions after the documents have been sorted */ 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; /* 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 {/base/city == 'Sydney' " "AND /information/date == '2006/07/18'}"; returnForm = "/document/base/name, /document/base/price"; sortForm = "/document/base/price/text() DESC"; /* 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 ) { 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 = 3 [Result]Item No. 1 = <document><name>Hotel 1</name><price>350</price></document> [Result]Item No. 2 = <document><name>Hotel 3</name><price>250</price></document> [Result]Item No. 3 = <document><name>Hotel 2</name><price>150</price></document> |
Contents
Index
![]() ![]() |