Kbase P180865: How to traverse data in a dynamic proDataSet
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  03/05/2011 |
|
Status: Verified
GOAL:
How to traverse data in a dynamic proDataSet
GOAL:
How to read data from the buffers of a dynamic DATASET
FACT(s) (Environment):
All Supported Operating Systems
OpenEdge 10.x
FIX:
Buffers in a proDataSet object can be accessed and traversed using a Dynamic Query object. There are a number of attributes and methods associated with the proDataSet and its children for supporting such access. The first thing you need to establish is whether you need to access these data hierarchically or if it's good enough just to identify the buffer you need and read its data.
Below are two possible examples which address these two potential needs:
Solution #1 (Accessing data in individual buffers without hierarchy from dataset):
DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO EXTENT 40.
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO EXTENT 40.
DEFINE VARIABLE hField AS HANDLE NO-UNDO.
DEFINE VARIABLE iBuffer AS INTEGER NO-UNDO.
DEFINE VARIABLE iField AS INTEGER NO-UNDO.
DEFINE VARIABLE cFieldName AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFieldValue AS CHARACTER NO-UNDO.
/* Loop through dataset buffers */
DO iBuffer = 1 TO hDataset:NUM-BUFFERS:
hBuffer[iBuffer] = hDataset:GET-BUFFER-HANDLE(iBuffer).
CREATE QUERY hQuery[iBuffer].
hQuery[iBuffer]:SET-BUFFERS(hBuffer[iBuffer]).
hQuery[iBuffer]:QUERY-PREPARE("FOR EACH " + hBuffer[iBuffer]:NAME).
hQuery[iBuffer]:QUERY-OPEN().
hQuery[iBuffer]:GET-FIRST().
/* Loop through the records in each buffer */
DO WHILE NOT hQuery[iBuffer]:QUERY-OFF-END:
DO iField = 1 TO hBuffer[iBuffer]:NUM-FIELDS:
hField = hBuffer[iBuffer]:BUFFER-FIELD(iField).
ASSIGN cFieldName = hField:NAME
cFieldValue = hField:BUFFER-VALUE.
END.
hQuery[iBuffer]:GET-NEXT().
END.
END.
Solution #2 (Accessing data hierarchally from dataset):
PROCEDURE readDataSet:
DEFINE INPUT PARAMETER phDataSet AS HANDLE NO-UNDO.
DEFINE VARIABLE hTopBuffer AS HANDLE NO-UNDO.
DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO.
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO EXTENT 40.
DEFINE VARIABLE hTopQuery AS HANDLE NO-UNDO EXTENT 40.
DEFINE VARIABLE hDataRelation AS HANDLE NO-UNDO.
DEFINE VARIABLE hField AS HANDLE NO-UNDO.
DEFINE VARIABLE iTopBuffer AS INTEGER NO-UNDO.
DEFINE VARIABLE iBuffer AS INTEGER NO-UNDO.
DEFINE VARIABLE iField AS INTEGER NO-UNDO.
DEFINE VARIABLE iRelationField AS INTEGER NO-UNDO.
DEFINE VARIABLE cPrepareString AS CHARACTER NO-UNDO.
DEFINE VARIABLE cRelationFields AS CHARACTER NO-UNDO.
DEFINE VARIABLE cPredicate AS CHARACTER NO-UNDO.
/* Start with the top-buffers in the dataset. These are top-level buffers that have no
parent relation in the d.ataset. */
DO iTopBuffer = 1 TO phDataSet:NUM-TOP-BUFFERS:
/* We need the query handle associated with this buffer as well as the buffer handle itself */
ASSIGN hQuery[iTopBuffer] = phDataSet:TOP-NAV-QUERY(iTopBuffer).
hTopBuffer = hQuery[iTopBuffer]:GET-BUFFER-HANDLE(1).
/* Create a query for walking hierarchically through the data in the current chain of related buffers */
CREATE QUERY hTopQuery[iTopBuffer].
hTopQuery[iTopBuffer]:ADD-BUFFER(hTopBuffer).
cPrepareString = hQuery[iTopBuffer]:PREPARE-STRING.
RUN addChildRelations ( INPUT hTopBuffer,
INPUT-OUTPUT hTopQuery[iTopBuffer],
INPUT-OUTPUT cPrepareString ).
hTopQuery[iTopBuffer]:QUERY-PREPARE(cPrepareString).
hTopQuery[iTopBuffer]:QUERY-OPEN().
hTopQuery[iTopBuffer]:GET-FIRST().
/* Now that we've built the queries and prepared them, loop through and extract the values */
DO WHILE NOT hTopQuery[iTopBuffer]:QUERY-OFF-END:
DO iBuffer = 1 TO hTopQuery[iTopBuffer]:NUM-BUFFERS:
hBuffer = hTopQuery[iTopBuffer]:GET-BUFFER-HANDLE(iBuffer).
DO iField = 1 TO hBuffer:NUM-FIELDS:
hField = hBuffer:BUFFER-FIELD(iField).
DISPLAY hBuffer:NAME + "." + hField:NAME + ": " +
STRING(hField:BUFFER-VALUE) FORMAT "x(60)".
END.
END.
hTopQuery[iTopBuffer]:GET-NEXT().
END.
END.
END PROCEDURE.
PROCEDURE addChildRelations:
DEFINE INPUT PARAMETER phTopBuffer AS HANDLE NO-UNDO.
DEFINE INPUT-OUTPUT PARAMETER phTopQuery AS HANDLE NO-UNDO.
DEFINE INPUT-OUTPUT PARAMETER pcPrepareString AS CHARACTER NO-UNDO.
DEFINE VARIABLE hBuffer AS HANDLE NO-UNDO.
DEFINE VARIABLE hDataRelation AS HANDLE NO-UNDO.
DEFINE VARIABLE iBuffer AS INTEGER NO-UNDO.
DEFINE VARIABLE iRelationField AS INTEGER NO-UNDO.
DEFINE VARIABLE cRelationFields AS CHARACTER NO-UNDO.
DEFINE VARIABLE cPredicate AS CHARACTER NO-UNDO.
&nb.sp; /* Loop through the child relations and add them to the query and prepare string. */
DO iBuffer = 1 TO phTopBuffer:NUM-CHILD-RELATIONS:
ASSIGN hDataRelation = phTopBuffer:GET-CHILD-RELATION(iBuffer)
hBuffer = hDataRelation:CHILD-BUFFER
cRelationFields = hDataRelation:RELATION-FIELDS
pcPrepareString = pcPrepareString + ", EACH " +
hBuffer:NAME + " WHERE "
cPredicate = "".
phTopQuery:ADD-BUFFER(hBuffer).
/* Build the query predicate for each relationship/buffer */
DO iRelationField = 1 TO NUM-ENTRIES(cRelationFields) BY 2:
cPredicate = (IF (cPredicate GT "") EQ TRUE THEN " AND " ELSE "") +
hBuffer:NAME + "." +
ENTRY(iRelationField + 1,cRelationFields) +
" = " + phTopBuffer:NAME + "." +
ENTRY(iRelationField,cRelationFields).
END.
pcPrepareString = pcPrepareString + cPredicate.
/* Often the buffer may have its own child relationship. Recursively call this same routine
until we reach the end. */
IF hBuffer:NUM-CHILD-RELATIONS GT 0 THEN
RUN addChildRelations ( INPUT hBuffer,
INPUT-OUTPUT phTopQuery,
INPUT-OUTPUT pcPrepareString ).
END.
END PROCEDURE.
.