Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Appendix J Sample C Programs> J.1 Searching Data

J.1.2 Obtaining XML Documents that Match Search Conditions in a Specified Format

Part of the data obtained as search results is sometimes used as additional conditions to perform the next search operation. In this case, it is common to obtain only part of the data as search results instead of the entire data.

The example below shows how to use the C APIs to find out how many hotels match the date and location specified as the search condition, as well as to get partial information.

mark1Search Conditions

'I would like to know the names and accommodation rates of up to 30 of the hotels in Sydney that are available on 2006/07/18.'

Perform a search using the date (2006/07/18) and the location (Sydney) as the search conditions, and then obtain the names and accommodation rates of 30 of the hotels from the results of this search.

mark1An Example of Using the APIs

The following is a sample program using the C APIs.

#include <stdio.h>

#include "libshun.h"

/* Obtain matching XML documents in a specified format */
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 = NULL;

  /* 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;
}

mark1Execution Results

Number of hits      = 3
[Result] Item No. 1 = <document><name>Hotel 1</name><price>350</price></document>
[Result] Item No. 2 = <document><name>Hotel 2</name><price>150</price></document>
[Result] Item No. 3 = <document><name>Hotel 3</name><price>250</price></document>

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006