Query Example

FDO API

 
Query Example
 
 
 

In the Data Maintenance chapter, we created an instance of the FdoFeatureClass SampleFeatureClass and assigned values to its integer, string, and geometry properties (see Example: Inserting an Integer, a String, and a Geometry Value). The sample code in the following query example selects this instance and retrieves the values of its properties. Specifically, the sample code does the following:

  1. Creates the select command, and
  2. Points the select command at the target FdoFeatureClass SampleFeatureClass, and
  3. Creates a filter to identify which instance of SampleFeatureClass to select, and
  4. Points the select command at the filter, and
  5. Executes the command, which returns an FdoIFeatureReader object, and
  6. Loops through the feature reader object, which contains one or more query results depending on the filter arguments. In the sample code provided, there is only one result.
  7. Finally, the code extracts the property values from each query result.
// we have one FdoFeatureClass object in the DataStore
// create a query that returns this object
// create the select command
FdoPtr<FdoISelect> sampleSelect;
sampleSelect = (FdoISelect *)
  connection->CreateCommand(FdoCommandType_Select);
// point the select command at the target FdoFeatureClass
// SampleFeatureClass
sampleSelect->SetFeatureClassName(L"SampleFeatureClass");
// create the filter by
// 1. creating an FdoIdentifier object containing the name of 
//   the identity property
FdoPtr<FdoIdentifier> queryPropertyName;
queryPropertyName =
  FdoIdentifier::Create(L"SampleIdentityDataProperty");
// 2. creating an FdoDataValue object containing the value of the
//   identity property
FdoPtr<FdoDataValue> queryPropertyValue;
queryPropertyValue = FdoDataValue::Create(101);
// 3. calling FdoComparisonCondition::Create() passing in the
//   the queryPropertyName, an enumeration constant signifying an
//   equals comparison operation, and the queryPropertyValue
FdoPtr<FdoFilter> filter;
filter = FdoComparisonCondition::Create(queryPropertyName,
  FdoComparisonOperations_EqualTo, queryPropertyValue);
// point the select command at the filter
sampleSelect->SetFilter(filter);
// execute the select command
FdoPtr<FdoIFeatureReader> queryResults;
queryResults = sampleSelect->Execute();
// declare variables needed to capture query results
FdoPtr<FdoClassDefinition> classDef;
FdoPtr<FdoPropertyDefinitionCollection> properties;
FdoInt32 numProperties = 0;
FdoPropertyDefinition * propertyDef;
FdoPropertyType propertyType;
FdoDataType dataType;
FdoDataPropertyDefinition * dataPropertyDef;
FdoString * propertyName = NULL;
FdoPtr<FdoByteArray> byteArray;
FdoIGeometry * geometry = NULL;
FdoGeometryType geometryType = FdoGeometryType_None;
FdoIPolygon * polygon = NULL;
FdoILinearRing * exteriorRing = NULL;
FdoILinearRing * interiorRing = NULL;
FdoIDirectPosition * position = NULL;
FdoInt32 dimensionality = FdoDimensionality_XY;
FdoInt32 numPositions = 0;
FdoInt32 numInteriorRings = 0;
// loop through the query results
while (queryResults->ReadNext()) {
  // get the feature class object and its properties
  classDef = queryResults->GetClassDefinition();
  properties = classDef->GetProperties();
  // loop through the properties
  numProperties = properties->GetCount();
  for(int i = 0; i < numProperties; i++) {
    propertyDef = properties->GetItem(i);
    // get the property name and property type
    propertyName = propertyDef->GetName();
    propertyType = propertyDef->GetPropertyType();
    switch (propertyType) {
      // it’s a data property
      case FdoPropertyType_DataProperty:
        dataPropertyDef =
          dynamic_cast<FdoDataPropertyDefinition *>
          (propertyDef);
        dataType = dataPropertyDef->GetDataType();
        switch (dataType) {
          case FdoDataType_Boolean:
            break;
          case FdoDataType_Int32:
            break;
          case FdoDataType_String:
            break;
          default:
        }
        break;
      // it’s a geometric property
      // convert the byte array to a geometry
      // and determine the derived type of the geometry
      case FdoPropertyType_GeometricProperty:
        byteArray = queryResults->GetGeometry(propertyName);
        geometry =
          sampleGeometryFactory->CreateGeometryFromAgf 
          (byteArray);
        geometryType = geometry->GetDerivedType();
        // resolve the derived type into a list of ordinates
        switch (geometryType) {
          case FdoGeometryType_None:
            break;
          case FdoGeometryType_Point:
            break;
          case FdoGeometryType_LineString:
            break;
          case FdoGeometryType_Polygon:
            polygon = dynamic_cast<FdoIPolygon *>(geometry);
            exteriorRing = polygon->GetExteriorRing();
            dimensionality = exteriorRing-
              >GetDimensionality();
            numPositions = exteriorRing->GetCount();
            double X, Y, Z, M;
            for(int i=0; i<numPositions; i++) {
              position = exteriorRing->GetItem(i);
              if (dimensionality & FdoDimensionality_Z && 
                dimensionality & FdoDimensionality_M) {
                X = position->GetX();
                Y = position->GetY();
                Z = position->GetZ();
                M = position->GetM();
              else if (dimensionality & FdoDimensionality_Z 
              && !(dimensionality & FdoDimensionality_M)) {
                X = position->GetX();
                Y = position->GetY();
                Z = position->GetZ();
              else {
                X = position->GetX();
                Y = position->GetY();
              }
            }
            numInteriorRings = polygon-
              >GetInteriorRingCount();
            for(int i=0; i<numInteriorRings; i++) {
              interiorRing = polygon->GetInteriorRing(i);
              // do same for interior ring as exterior ring
            }
            break;
          case FdoGeometryType_MultiPoint:
            break;
          case FdoGeometryType_MultiLineString:
            break;
          case FdoGeometryType_MultiPolygon:
            break;
          case FdoGeometryType_MultiGeometry:
            break;
          case FdoGeometryType_CurveString:
            break;
          case FdoGeometryType_CurvePolygon:
            break;
          case FdoGeometryType_MultiCurveString:
            break;
          case FdoGeometryType_MultiCurvePolygon:
            break;
          default:
        }
        break;
      default:
    }
  }
}