/* query_noundo.p

2001 by Judy Green, Joanju Limited

Find DEFINE... VARIABLE statements within current node which do not have
a "NO-UNDO". 
A query like this might be used to assist with performance tuning.

In practice, you would want to change this so that it
reports each include file only once. ie: Create a temp-table
storing each filename/line-number combination only once, and then
report on that.

Children of a DEFINE node: we have an optional node for a SHARED
phrase, followed by the node for the type of DEFINE.
So, we watch for numChildren >= 2, and if we haven't hit a VARIABLE
node by then, we're done with that DEFINE statement.
*/

DEFINE INPUT PARAMETER parser AS HANDLE NO-UNDO.
DEFINE INPUT PARAMETER nodehandle AS INTEGER NO-UNDO.
DEFINE INPUT PARAMETER programName AS CHARACTER NO-UNDO.

{proparse/api/proparse.i parser}

DEFINE VARIABLE nodeType    AS CHARACTER NO-UNDO.
DEFINE VARIABLE outfile     AS CHARACTER NO-UNDO.
DEFINE VARIABLE child       AS INTEGER   NO-UNDO.
DEFINE VARIABLE i1          AS INTEGER   NO-UNDO.
DEFINE VARIABLE i2          AS INTEGER   NO-UNDO.
DEFINE VARIABLE numChildren AS INTEGER   NO-UNDO.
DEFINE VARIABLE numResults  AS INTEGER   NO-UNDO.
DEFINE VARIABLE numResults2 AS INTEGER   NO-UNDO.
DEFINE VARIABLE result      AS INTEGER   NO-UNDO.
DEFINE VARIABLE result2     AS INTEGER   NO-UNDO.

DEFINE VARIABLE havevar     AS LOGICAL NO-UNDO.
DEFINE VARIABLE havenoundo  AS LOGICAL NO-UNDO.
DEFINE VARIABLE lastfiles   AS CHARACTER INITIAL "" NO-UNDO.
DEFINE VARIABLE nodefile    AS CHARACTER NO-UNDO.

ASSIGN outfile = SESSION:TEMP-DIRECTORY + "/joanju_qry_undo.out".

ASSIGN
  result = parserGetHandle()
  result2 = parserGetHandle()
  child = parserGetHandle().

ASSIGN numResults = parserQueryCreate(nodehandle, "query_undo", "DEFINE").

OUTPUT TO VALUE(outfile) APPEND.

DO i1 = 1 TO numResults:
  parserQueryGetResult("query_undo", i1, result).
  
  ASSIGN
    nodeType = parserNodeFirstChild(result,child)
    numChildren = 0
    havevar = FALSE
    havenoundo = FALSE.
  DO WHILE nodeType <> "":
    ASSIGN numChildren = numChildren + 1.
    IF nodeType = "VARIABLE" THEN ASSIGN havevar = TRUE.
    IF numChildren >= 2 AND havevar = FALSE THEN LEAVE. 
    IF nodeType = "NOUNDO" THEN ASSIGN havenoundo = TRUE.
    ASSIGN nodeType = parserNodeNextSibling(child,child).
  END.

  /* If we got a VARIABLE, and didn't get a NO-UNDO, then report it. */
  IF havevar = TRUE AND havenoundo = FALSE THEN DO:
    ASSIGN nodefile = parserGetNodeFilename(result).
    IF lastfiles <> programName + nodefile THEN DO:
      PUT UNFORMATTED
        SKIP(1)
        "From " programName " : "
        nodefile
        " : ".
      ASSIGN lastfiles = programName + nodefile. 
    END.
    PUT UNFORMATTED
      SKIP
      parserGetNodeLine(result) 
      FILL(" ",7 - LENGTH(STRING(parserGetNodeLine(result)))).
    /* Use an "unfiltered query" for displaying the DEFINE node's text.
     * I use LEFT-TRIM as a trick: if the node has no text, then
     * we don't display the extra space.
     */
    numResults2 = parserQueryCreate(result, "q2", "").
    DO i2 = 1 TO numResults2:
      parserQueryGetResult("q2", i2, result2).
      PUT UNFORMATTED LEFT-TRIM(parserGetNodeText(result2) + " ").
    END.
  END.

END.

OUTPUT CLOSE.

RETURN.

