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.3 Obtain Every Instance of Particular XML Documents

The search process performs the operations stated in J.1.1 Obtaining the Number of XML Documents that Match Search Conditions and J.1.2 Obtaining XML Documents that Match Search Conditions in a Specified Format to obtain all the data based on the filtering conditions.

The following example shows how the C APIs are used. It shows how all the data of applicable hotels can be retrieved based on the hotel names obtained using the operations stated in J.1.2 Obtaining XML Documents that Match Search Conditions in a Specified Format.

mark1Search Conditions

Find the names of 30 hotels in Sydney with vacancies on 2006/07/18Perform a search using the date (2006/07/18) and the location (Sydney) as the search conditions, then obtain detailed information on a particular hotel using the record identifier corresponding to that hotel name.

mark1An Example of Using the APIs

The following is a sample program using the C APIs.

#include <stdio.h>

#include "libshun.h"

/* Obtain all instances of specific XML documents */
int main()
{
  /* Handle variables */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle_serch;
  SHUNHSTMT statementHandle_rec_id;

  /* 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_search;
  SHUNRECID *recID_search_rec_id;
  SHUNDATA  *dataInfo_serch;
  SHUNDATA  *dataInfo_rec_id;
  SHUNPOS *firstPosition, *lastPosition;

  /* Error variables */
  SHUNHANDLE errorHandle;
  int errorLevel;
  char *errorMessage;


  /* Variable initialization */
  connectionHandle = NULL;
  statementHandle_serch = NULL;
  statementHandle_rec_id = 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_serch );
  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_serch,
    startNo,
    NULL,
    0,
    returnRequestCount,
    queryForm,
    returnForm,
    sortForm,
    &hitCount,
    &returnCount,
    &returnableCount,
    &recID_search,
    &dataInfo_serch,
    &firstPosition,
    &lastPosition );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_serch;
    goto ErrorEnd;
  }

  /* Extract number of hits */
  printf( "Number of hits                = %d\n", hitCount );
  printf( "Number of responses                  = %d\n", returnCount );

  
  /* Allocation of data manipulation handles */
  status = ShunAllocHandle( connectionHandle, &statementHandle_rec_id );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }
  
  /* Obtain all data based on ID */
  if ( hitCount > 0 ) {
    status = ShunSearchRecId(
      statementHandle_rec_id,
      returnCount,
      recID_search,
      &recID_search_rec_id,
      &dataInfo_rec_id );
    if ( status != SHUN_SUCCESS ) {
      errorHandle = (SHUNHANDLE)statementHandle_rec_id;
      goto ErrorEnd;
    }
    for (i = 0; i < returnCount; ++i) {
      printf( "[Details]Item No. %d =%s\n", startNo + i, dataInfo_rec_id[i].Data);
    }
  }
  
  /* Release data manipulation handle */
  status = ShunFreeHandle( statementHandle_rec_id );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_rec_id;
    goto ErrorEnd;
  }
  
  /* Release data manipulation handle */
  status = ShunFreeHandle( statementHandle_serch );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle_serch;
    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_rec_id != NULL ) {
    status = ShunFreeHandle( statementHandle_rec_id );
    if ( status != SHUN_SUCCESS ) {
      printf("Data manipulation handle release error: %d\n", status);
    }
  }

  /* Release data manipulation handle */
  if ( statementHandle_serch != NULL ) {
    status = ShunFreeHandle( statementHandle_serch );
    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
Number of responses                  = 3
 [Details] Item No. 1 = <document>
    <base>
        <name>Hotel 1</name>
        <city>Sydney</city>
        <address>Bondi Beach</address>
        <detail>http://xxxxx.com.au</detail>
        <price>350</price>
    </base>
    <information>
        <date>2006/07/18</date>
    </information>
    <note>En-suite bathroom and toilet, two minutes walk to train station XX</note>
</document>
[Details] Item No. 2 = <document>
    <base>
        <name>Hotel 2</name>
        <city>Sydney</city>
        <address>Bondi Beach</address>
        <detail>http://xxxxx.com.au</detail>
        <price>150</price>
    </base>
    <information>
        <date>2006/07/18</date>
    </information>
    <note>En-suite bathroom and toilet, five minutes walk to train station XX</note>
</document>
[Details] Item No. 3 = <document>
    <base>
        <name>Hotel 3</name>
        <city>Sydney</city>
        <address>Bondi Beach</address>
        <detail>http://xxxxx.com.au</detail>
        <price>250</price>
    </base>
    <information>
        <date>2006/07/18</date>
    </information>
    <note>En-suite bathroom and toilet, ten minutes walk to train station XX</note>
</document>

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006