Progress
Programming
Handbook


Examples

These examples illustrate several features of persistent procedures. The application returns a customer name from the sports database when you enter a customer number. The main event-driven procedure, p-persp1.p, creates a persistent procedure from p-persp2.p and stores its handle in hdb-tools:

p-persp1.p
DEFINE VARIABLE hdb-tools AS HANDLE. 
DEFINE VARIABLE out-message AS CHARACTER FORMAT "x(60)". 
DEFINE VARIABLE vcust-num LIKE customer.cust-num INITIAL 0. 
DEFINE VARIABLE vname LIKE customer.name. 
DEFINE BUTTON bcancel LABEL "Cancel Database Access". 
DEFINE FRAME CustFrame 
    vcust-num LABEL "Customer Number" 
    vname LABEL "Customer Name" 
    bcancel 
WITH SIDE-LABELS. 
ON RETURN OF vcust-num IN FRAME CustFrame DO: 
    ASSIGN vcust-num. 
    RUN get-cust-name IN hdb-tools (INPUT vcust-num, OUTPUT vname). 
    DISPLAY vname WITH FRAME CustFrame. 
END. 
ON CHOOSE OF bcancel IN FRAME CustFrame DO: 
    DEFINE VARIABLE hcurrent AS HANDLE. 
    DEFINE VARIABLE hnext AS HANDLE. 
        hcurrent = SESSION:FIRST-PROCEDURE. 
        DO WHILE VALID-HANDLE(hcurrent): 
        hnext = hcurrent:NEXT-SIBLING. 
        IF hcurrent:PRIVATE-DATA = "DATABASE" THEN DO: 
            RUN destroy-context IN hcurrent (OUTPUT out-message). 
            MESSAGE out-message. 
        END. 
        hcurrent = hnext. 
    END. 
    APPLY "WINDOW-CLOSE" TO CURRENT-WINDOW. 
END. 
RUN p-persp2.p PERSISTENT SET hdb-tools. 
ENABLE ALL WITH FRAME CustFrame. 
WAIT-FOR WINDOW-CLOSE OF CURRENT-WINDOW. 

p-persp2.p
THIS-PROCEDURE:PRIVATE-DATA = "DATABASE". 
PROCEDURE get-cust-name: 
    DEFINE INPUT PARAMETER pcust-num LIKE customer.cust-num. 
    DEFINE OUTPUT PARAMETER pname LIKE customer.name. 
    FIND customer WHERE customer.cust-num = pcust-num NO-ERROR. 
    IF NOT AVAILABLE(customer) THEN 
        pname = ?. 
    ELSE  
        pname = customer.name. 
END PROCEDURE. 
PROCEDURE destroy-context: 
    DEFINE OUTPUT PARAMETER out-message AS CHARACTER FORMAT "x(60)". 
    DELETE PROCEDURE THIS-PROCEDURE. 
    out-message = THIS-PROCEDURE:PRIVATE-DATA + " module deleted.". 
END PROCEDURE. 

This persistent procedure provides two internal procedures to the application, getcustname and destroycontext. The getcustname procedure performs the database access for the application, and the destroycontext procedure deletes the context of the persistent procedure from the application.

Note that while p-persp1.p controls the application, p-persp2.p provides all the code to manage itself, in this case to delete its own context. If you write an application with many persistent procedures, each one can provide a common set of internal procedures with the same name and interface, such as deletecontext. Thus, the deletecontext in one persistent procedure might operate differently than that in another, but they all can be called in the same context of the controlling procedure.

In fact, p-persp1.p illustrates this generic procedure capability when you choose the bcancel button, by looping through the persistent procedure chain to delete the p-persp2.p context. You can create any number of different persistent procedures with the PRIVATEDATA attribute set to "DATABASE," and with many additional variations on the theme.

NOTE: Persistent procedures that manage their own user interface often create and manage their own widget pools. For more information, see Windows."


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095