Getting the Query for a ProDataSet

What I'm looking for is a procedure that can take the handle of a ProDataSet and produce a Query Clause that would walk through the ProDataSet.

For example
Code:
DEFINE DATASET dsSports2000
    FOR ttCustomer, ttOrder, ttOrderLine
        DATA-RELATION dr1 FOR ttCustomer, ttOrder NESTED
            RELATION-FIELD(CustNum,CustNum)
        DATA-RELATION dr2 FOR ttOrder, ttOrderLine NESTED
            RELATION-FIELD(OrderNum,OrderNum).
Would return
Code:
FOR EACH ttCustomer,
EACH ttOrder WHERE ttOrder.CustNum = ttCustomer.CustNum,
EACH ttOrderLine WHERE ttOrderLine.OrderNum = ttOrder.OrderNum

Has anyone done this or something similar?

I'm guessing the NUM-BUFFERS attribute of the PDS and the WHERE-STRING of the data-relations will come into it somewhere?
 
One severe headache later...

Code:
/* Receive the handle of the ProDataSet as an input parameter */
DEFINE INPUT  PARAMETER ihPDS AS HANDLE      NO-UNDO.
/* Define a temp-table to hold all the relations involved in the table */
DEFINE TEMP-TABLE ttRelation NO-UNDO
    FIELD Sequence      AS INTEGER
    FIELD ParentBuff    AS HANDLE
    FIELD ChildBuff     AS HANDLE
    FIELD WhereString   AS CHARACTER
    FIELD IsNested      AS LOGICAL
    INDEX x1 ParentBuff
             IsNested
             Sequence.
 
DEFINE VARIABLE iLoop       AS INTEGER     NO-UNDO.
DEFINE VARIABLE hBuffer     AS HANDLE      NO-UNDO.
DEFINE VARIABLE hRelation   AS HANDLE      NO-UNDO.
DEFINE VARIABLE cString     AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cBuffList   AS CHARACTER   NO-UNDO.
/* Populate the table of relations */
DO iLoop = 1 TO ihPDS:NUM-RELATIONS:
    ASSIGN hRelation = ihPDS:GET-RELATION(iLoop).
    CREATE ttRelation.
    ASSIGN ttRelation.Sequence      = iLoop
           ttRelation.ParentBuff    = hRelation:PARENT-BUFFER
           ttRelation.ChildBuff     = hRelation:CHILD-BUFFER
           ttRelation.WhereString   = hRelation:WHERE-STRING
           ttRelation.IsNested      = hRelation:NESTED.
END.
/* Start parsing with the top level buffers in turn */
DO iLoop = 1 TO ihPDS:NUM-TOP-BUFFERS:
    ASSIGN hBuffer  = ihPDS:GET-TOP-BUFFER(iLoop).
    RUN TopBuffer(INPUT hBuffer).
END.
PROCEDURE TopBuffer:
    DEFINE INPUT  PARAMETER ihBuffer AS HANDLE      NO-UNDO.
    DEFINE VARIABLE cString AS CHARACTER   NO-UNDO.
    ASSIGN cString    = "FOR EACH " + ihBuffer:NAME
           cBuffList  = STRING(ihBuffer).
    /* Process all nested buffers for the top level buffer */
    RUN NestBuffer(INPUT ihBuffer,
                   INPUT-OUTPUT cString,
                   INPUT-OUTPUT cBuffList).
    /* Publish our result */
    PUBLISH "DataSetQuery" (cString,cBuffList).
    /* Now process all non-nested buffers of the parent */
    FOR EACH ttRelation
        WHERE ttRelation.ParentBuff EQ ihBuffer
        AND   ttRelation.IsNested   EQ FALSE:
        RUN TopBuffer(INPUT ttRelation.ChildBuff).
    END.
END PROCEDURE.
PROCEDURE NestBuffer:
    DEFINE INPUT  PARAMETER ihBuffer AS HANDLE      NO-UNDO.
    DEFINE INPUT-OUTPUT PARAMETER iocString     AS CHARACTER   NO-UNDO.
    DEFINE INPUT-OUTPUT PARAMETER iocBuffList   AS CHARACTER   NO-UNDO.
    /* Append all nested buffers to the query clause */
    FOR EACH ttRelation
        WHERE ttRelation.ParentBuff EQ ihBuffer
        AND   ttRelation.IsNested   EQ TRUE:
        ASSIGN iocString    = iocString 
                            + ", EACH "
                            + ttRelation.ChildBuff:NAME + " "
                            + REPLACE(ttRelation.WhereString,"="," = ")
               iocBuffList  = iocBuffList
                            + ","
                            + STRING(ttRelation.ChildBuff).
        /* Check for further layers of nesting */
        RUN NestBuffer(INPUT ttRelation.ChildBuff,
                       INPUT-OUTPUT iocString,
                       INPUT-OUTPUT iocBuffList).
    END.
END PROCEDURE.
 
Top