/* ============================================================================
   file    : proparse/api/comparecfg.p
   purpose : determine if we need to load schema again.
             This procedure is ADE-persistent (unlike proparse.p) and stores
             a list of connected databases. This list is compared to the
             list of currently connected databases.
   author  : Jurjen Dijkstra, May 2002
   ============================================================================ */


DEFINE TEMP-TABLE old_dblist NO-UNDO
    FIELD lname      AS CHARACTER  /* LDBNAME */
    FIELD connparams AS CHARACTER. /* DBPARAM */

/* set private-data so we can find this procedure easily */
THIS-PROCEDURE:PRIVATE-DATA = "proparse/api/comparecfg.p".
RETURN.

PROCEDURE ADEPersistent:
/* ----------------------------------------------------------------------------
   purpose : make sure this persistent procedure is not deleted by ADE
   ---------------------------------------------------------------------------- */
    RETURN "OK".
END.    


PROCEDURE CompareConfiguration :
/* ---------------------------------------------------------------------------
   purpose : Try to determine if database schemas need to be loaded (again).
             For example when:
                - propath has changed
                - databases are disconnected and other databases are connected.
             We can't tell if the actual schema (.df) has changed
   --------------------------------------------------------------------------- */
   DEFINE INPUT  PARAMETER hParser AS HANDLE  NO-UNDO.

   DEFINE VARIABLE i               AS INTEGER  NO-UNDO.
   DEFINE VARIABLE ptr             AS MEMPTR  NO-UNDO.
   DEFINE VARIABLE old_propath     AS CHARACTER NO-UNDO.

   RUN configGet in hParser ("propath", OUTPUT ptr).
   old_propath = GET-STRING(ptr, 1).
   /* Pointers returned by proparse.dll are memory managed by proparse.dll.
    * Do not:  SET-SIZE(ptr) = 0.
    */

   IF old_propath <> PROPATH THEN DO:
      RUN InvalidateConfig (hparser).
      RETURN.
   END.

   DO i=1 TO NUM-DBS :
      FIND old_dblist WHERE old_dblist.lname=LDBNAME(i) NO-LOCK NO-ERROR.
      /* if the logical database name is not in old_dblist, then
         you will have to do a schema load (again) */
      IF NOT AVAILABLE old_dblist THEN DO:
         RUN InvalidateConfig(hparser).
         RETURN.
      END.
      /* if the database was disconnected and connected again, probably for a
         different location or host/service, then assume its schema is invalid */
      IF DBPARAM(i) <> old_dblist.connparams THEN DO:
         RUN InvalidateConfig(hparser).
         RETURN.
      END.
   END.

END PROCEDURE.



PROCEDURE InvalidateConfig :
/* -----------------------------------------------------------------------------
   purpose : force a new schema load
   ---------------------------------------------------------------------------- */
   DEFINE INPUT PARAMETER hParser AS HANDLE NO-UNDO.
   DEFINE VARIABLE i AS INTEGER NO-UNDO.

   RUN configSet in hParser ("init", "false", OUTPUT i).
   RUN SchemaClear in hParser (output i).
   
   /* clear obsolete values for connection parameters */
   FOR EACH old_dblist:
       DELETE old_dblist.
   END.

   /* store new values */
   DO i=1 TO NUM-DBS :
      CREATE old_dblist.
      ASSIGN old_dblist.lname      = LDBNAME(i)
             old_dblist.connparams = DBPARAM(i).
   END.

END PROCEDURE.


