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.1 Obtaining the Number of XML Documents that Match Search Conditions

If there is a large amount of data to be searched, it is not practical to look up all the data items that match the conditional expression. In such cases, it is useful to find out how many data items match the specified condition.

The example below shows how to use the APIs to find out how many hotels match the date and location specified as the search conditions.

mark1Search Conditions

'How many hotels in Sydney with vacancies on 2006/07/18?'Perform a search using the date (2006/07/18) and the location (Sydney) as the search conditions.

mark1An Example of Using the APIs

The following is a sample program using the C APIs.

#include <stdio.h>
#include "libshun.h"

/* Obtain the number of XML documents that match specified search conditions */
int main()
{
  /* Handle variables */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle;

  /* Work variables */
  int status;

  /* Input parameters */
  char *hostName;
  int portNo;
  char *ShunFileName;
  char *queryForm;

  /* Output parameters */
  int hitCount;

  /* 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 search expression */
  queryForm = "/document {/base/city == 'Sydney' "
              "AND /information/date == '2006/07/18'}";

  /* Call API then specify search expression and obtain number of hits */
  status = ShunGetHitCount(
    statementHandle,
    queryForm,
    &hitCount );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle;
    goto ErrorEnd;
  }

  /* Extract number of hits */
  printf( "Number of hits                = %d\n", hitCount );
  
/* 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

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006