/*
 *  Copyright 1995, 1996, 1997 by the
 *  Association for Information and Image Management International
 *      1100 Wayne Avenue
 *      Silver Spring, MD 20910-5603
 *      Tel: 301/587-8202
 *      Fax: 301/587-2711
 *  All Rights Reserved.
 *  DMA (Document Management Alliance) working group.
 */

/* quexp2.cpp

    This example is for the following query:
 
    Find all priority 3 documents of any document class that
    are filed in "blue" folders, and return the name of the
    document and who filed it in the folder.

    SELECT D.name, R.filer
    FROM   F INNER JOIN R ON F.This = R.Tail
               INNER JOIN D (IncludeSubclasses=TRUE) ON R.Head = D.This
    WHERE  F.color = "blue" AND D.priority = 3
    ORDER BY <null>

    0: F ("Folder") is a subclass of dmaClass_Container
    1: R ("Ref. Cont. Relationship") is a subclass of
           dmaClass_ReferentialContainmentRelationship 
    2: D ("Document") is a subclass of dmaClass_DocVersion
*/

#define DMA_INIT_CD
#include <query.h>

/* Declare DmaId variables for the searchable class ID's.
*/
#define DVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 } }
DMA_EXTERN_C DmaId D = DVal;

#define FVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09 } }
DMA_EXTERN_C DmaId F = FVal;

#define RVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09 } }
DMA_EXTERN_C DmaId R = RVal;
    
/* Declare DmaId variables for the property ID's.
*/
#define priorityVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0A } }
DMA_EXTERN_C DmaId priority = priorityVal;

#define colorVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0B } }
DMA_EXTERN_C DmaId color = colorVal;

#define filerVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0C } }
DMA_EXTERN_C DmaId filer = filerVal;

#define nameVal { 0x12345678, 0x1234, 0x5678, \
                 { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x0A } }
DMA_EXTERN_C DmaId name = nameVal;

/* Declare the variables needed to perform the query.
*/

    JoinExprElt             From2 [] = 
        {
            /* F[0] */
            { 0, &F, DMA_FALSE, NULL, NULL, 0, NULL, 0, NULL },

            /* INNER JOIN R[1] ON [0].This = [1].Tail */
            { 1, &R, DMA_FALSE, &dmaJoinOperator_Inner,
              &dmaQueryOperator_EqualObject,
              0, &dmaProp_This, 1, &dmaProp_Tail
            },

            /* INNER JOIN D[2] (IncludeSubclases = TRUE)
                   ON [1].Head = [2].This
            */
            { 2, &D, DMA_TRUE, &dmaJoinOperator_Inner,
              &dmaQueryOperator_EqualObject,
              1, &dmaProp_Head, 2, &dmaProp_This
            },

            { -1, NULL, DMA_FALSE, NULL, NULL, 0, NULL, 0, NULL }
        };


    Property                Select2 [] = 
        {
            { 2, &name },
            { 1, &filer },
            { -1, NULL }
        };

    Operator                N12 = { &dmaQueryOperator_And };
    Operator                N22 = { &dmaQueryOperator_EqualString };
    Property                N32 = { 0, &color };
    StringConst             N42 = { L"blue" };
    Operator                N52 = { &dmaQueryOperator_EqualInteger32 };
    Property                N62 = { 1, &filer };
    IntegerConst            N72 = { 3 };


    /* F.color = "blue" AND D.priority = 3
    */
    QueryNode               QueryExpr2 [] =
        {
            { OperatorNodeType, 0, &N12 },
                { OperatorNodeType, 1, &N22 },
                    { PropertyNodeType, 2, &N32 },
                    { IntegerConstNodeType, 2, &N42 },
                { OperatorNodeType, 1, &N52 },
                    { PropertyNodeType, 2, &N62 },
                    { StringConstNodeType, 2, &N72 },
            { StopperNodeType, -1, NULL }
        };

    QueryRoot                QR2 = { &From2[0], &Select2[0],
                                     &QueryExpr2[0], NULL, DMA_FALSE };


DmaRC
QueryExample2(void)
{
    DmaRC                   rc;
    IdmaScope *             pScope = NULL;
    IdmaProperties *        pErrNode = NULL;
    IdmaResultSet *         pResultSet = NULL;
    IdmaProperties *        pResultRow = NULL;
    DmaIndex32              SelectionsOffset;

    /* TBD: Initializataion to get system object, doc space object, 
       and target scope object = pScope.
    */

    rc = StartQuery(&QR2, pScope, &pResultSet);
    CHK(rc);

    while (1)
    {
        rc = pResultSet->GetNextResultRow(IID_IdmaProperties,
                                          (pDmapv)&pResultRow);
        if (rc != DMARC_OK)
        {
            break;
        }

        rc = pResultRow->GetPropValInteger32ById(
                             &dmaProp_SelectListOffset,
                             &SelectionsOffset);
        CHK(rc);

        /* TBD: Not shown: Access the values of the properties of
           the query result row referred to by pResultRow
           corresponding to the properties selected in the Selections
           list, e.g.,

               DmaIndex32    x;
               <datatype>    y;
               rc = pResultRow->GetPropVal<datatype>ByIndex(
                                    SelectionsOffset + x,
                                    &y);

           to store the value for selection list element x into y.
        */

        RELEASE(pResultRow);  /* Release the Query Result row */
    }

theend:
    return(DMARC_OK);
}