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

Appendix I Sample C++ .NET Programs> I.1 Searching Data

I.1.5 Aggregating the Values of XML Documents that Match Search Conditions

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++ .NET are used, the following example specifies the location in the search conditional expression and extracts the cheapest accommodation rate, the most expensive accommodation rate, and the average accommodation rate.

mark1Search Conditions

'Among the hotels with a vacancy on 2006/07/18, I want to use aggregation to obtain the cheapest hotel, the most expensive hotel and the average hotel rate according to district.'

Specify the date (2006/07/18) as the search condition and then apply aggregation expressions (min, max and avg) to the results to obtain aggregated results.

mark1An Example of Using the APIs

The following is a sample program using the C++ .NET.

#using <mscorlib.dll>
#using <system.dll>
#using <Fujitsu.Shunsaku.dll>

using namespace System;
using namespace Fujitsu::Shunsaku;

class SampleSort2 {
public:
  static void Main( String *args __gc[] ) {
    ShunService *service = 0;

    try {
      // Create ShunService
      service = new ShunService();

      // Connect Shunsaku by specifying the host name and port number
      service->Host = "DirSvr1";
      service->Port = 33101;
      service->Connect();

      // Create ShunSearchRequirement
      ShunSearchRequirement *req = new ShunSearchRequirement();
      
      // Set each type of search conditions
      req->QueryExpression = "/document/information/date == '2006/07/18'";
      req->ReturnExpression = "/document/base/city/text(), min(/document/base/price/text()), max(/document/base/price/text()), avg(/document/base/price/text())";
      req->SortExpression = "/document/base/city/text()";
      req->ReplyNumber = 1;
      req->RequestCount = 30;
      
      // Specify the search condition and execute the search
      ShunResultSet *rs = service->Search( req );

      // Display the search results
      // Obtain the data that correspond to the search conditions
      int i = 0;
      ShunRecordEnumerator *enumerator = rs->Records->GetEnumerator();
      while ( enumerator->MoveNext() ) {
        ShunRecord *record = enumerator->Current;
        ++i;
        Console::WriteLine( "[Result] Item No. {0}:", __box( i ) );
        String *result __gc[,] = record->GetDividedDataAsTwoRankArray();
        Console::WriteLine("  District          :{0}", result[0,0]);
        Console::WriteLine("  Cheapest rate:{0}", result[1,0]);
        Console::WriteLine("  Most expensive rate:{0}", result[2,0]);
        Console::WriteLine("  Average rate:{0}", result[3,0]);
      }

      // Close the connection to Shunsaku
      service->Disconnect();

    }
    catch ( ShunContinuousException *e ) {
      // Processing to perform if ShunContinuousException occurs
      try {
        if( service != 0 && service->State == ShunConnectionState::Open ) {
          service->Disconnect();
        }
      }
      catch ( ShunException *ex ) {
        Console::WriteLine( "Error message : {0}", ex->Message );
      }
      Console::WriteLine( "Error message : {0}", e->Message );
    }
    catch ( ShunConnectionTerminatedException *e ) {
      // Processing to perform if ShunConnectionTerminatedException occurs
      Console::WriteLine( "Error message : {0}", e->Message );
    }
  }
};
int main() {
  return SampleSort2::Main( Environment::GetCommandLineArgs() );
}

mark1Execution Results

[Result] Item No.1:
  District: Adelaide
  Cheapest rate: 100
  Most expensive rate: 300
  Average rate: 200
[Result] Item No.2:
  District: Sydney
  Cheapest rate: 150
  Most expensive rate: 350
  Average rate: 250

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006