ABL provides you with one default buffer for each table or temp-table that you use in a procedure or class. ABL uses that buffer to store one record at a time from the table as the records are needed during the procedure or class. If you need more than one record or buffer at a time for a table, you can use this statement to define alternate buffers that are created at compile time for use in one or more procedures, or within a single class or class hierarchy.
DEFINE {[[ NEW ] SHARED ] | [ PRIVATE | PROTECTED ] [ STATIC ]} BUFFER buffer-name FOR [ TEMP-TABLE ]table-name [ PRESELECT ][ LABEL label-name] [ NAMESPACE-URI namespace][ NAMESPACE-PREFIX prefix] [ XML-NODE-NAME node-name] |
PRIVATE buffer data members can be accessed by the defining class. An instance can access the private buffer data members of another instance if both instances are of the same class. PROTECTED buffer data members can be accessed by the defining class and any of its derived classes. An instance can access protected buffer data members of a second instance that is at the same level or higher in the class hierarchy. The default access mode is PRIVATE. When you reference a buffer from another data member definition (such as a query) defined in the same class or class hierarchy, the access mode of the buffer cannot be more restrictive than the access mode of the referencing data member.
A buffer defined with the STATIC option is a static data member of the class type for which it is defined and is scoped to the ABL session where it is referenced. ABL creates one copy of the specified class static buffer on first reference to the class type, and ABL creates only one such copy for any number of instances of the class that you create. You can directly reference an accessible static buffer data member from any other static or instance class member defined in the same class or class hierarchy.
Without the STATIC option, ABL creates an instance buffer data member that is scoped to a single instance of the class where it is defined. ABL creates one copy of the specified instance buffer for each such class instance that you create. You cannot directly reference an instance buffer data member from a STATIC class member definition defined within the same class or class hierarchy.
Within a class, ABL defines the default buffer for any database table as a PRIVATE instance buffer. Thus, for example, you can only access the default buffer for the Customer table of the sports2000 database wherever a PRIVATE instance buffer can be accessed. Otherwise, you must define an alternate buffer data member for the table with an appropriate access mode and scope.
For more information on accessing buffers of different access modes and scopes, see the reference entry for Class-based data member access.
For more information on where and how to define data members in a class, see the CLASS statement reference entry.
To define a buffer for a table defined for multiple databases, you might have to qualify the table name with the database name. See the Record phrase reference entry for more information.
Use the TEMP-TABLE option to define a buffer for a temp-table when the temp-table has the same name as a database table. Otherwise, ABL associates the buffer with the database table by default.
If you define the buffer as static, and table-name is the name of a temp-table, the temp-table must also be defined as a static member of a class.
This procedure allows the user to create a new Customer record. Initially, the City, State, and Country fields are not shown. After the user enters a PostalCode value, the procedure searches for an existing Customer with the same postal code. If such a Customer is found, the City, State, and Country values from that record are displayed in the fields for the new record. The user can then update those fields.
r-defb.p
DEFINE BUFFER other-cust FOR Customer. FORM Customer WITH FRAME cre-cust. ON LEAVE OF Customer.PostalCode DO: FIND FIRST other-cust WHERE other-cust.PostalCode = Customer.PostalCode:SCREEN-VALUE AND other-cust.CustNum <> Customer.CustNum NO-ERROR. IF AVAILABLE other-cust THEN DISPLAY other-cust.City @ Customer.City other-cust.State @ Customer.State other-cust.Country @ Customer.Country WITH FRAME cre-cust. ENABLE Customer.City Customer.State Customer.Country WITH FRAME cre-cust. END. CREATE Customer. UPDATE Customer EXCEPT Customer.City Customer.State Customer.Country WITH FRAME cre-cust. |
The following gather a group of records so that the user can enter any table name and any set of record selection criteria and then look at the records in the table that meet those criteria:
r-defb2.p
DEFINE VARIABLE fname AS CHARACTER NO-UNDO FORMAT "x(12)" LABEL "Table name". DEFINE VARIABLE conditions AS CHARACTER NO-UNDO FORMAT "x(60)" LABEL "Conditions". REPEAT: /* Get the name of a table and, optionally, some record selection criteria */ UPDATE fname COLON 12 conditions COLON 12 WITH SIDE-LABELS 1 DOWN. HIDE ALL. IF conditions <> "" THEN /* Pass the table name and the record selection criteria as parameters */ RUN r-defb3.p fname "WHERE" conditions. ELSE RUN r-defb3.p fname. END. |
The r-defb2.p procedure gets the name of a table (such as Customer) and a condition (such as CreditLimit > 4000) and passes them as arguments to the r-defb3.p procedure:
r-defb3.p
DEFINE NEW SHARED BUFFER rec FOR {1} PRESELECT. DEFINE VARIABLE flist AS CHARACTER NO-UNDO EXTENT 12. DEFINE VARIABLE ix AS INTEGER NO-UNDO. /* Look in _File for the table named in the fname variable */ FIND _File "{1}". /* Store the table's field names in the first array */ FOR EACH _Field OF _File USE-INDEX _Field-Posit: IF i >= 12 THEN LEAVE. i = i + 1. flist[i] = _Field._Field-Name. END. /* Preselect records */ DO PRESELECT EACH rec {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}: /* Pass the filenames and all field names to r-defb4.p */ RUN r-defb4.p "{1}" flist[1] flist[2] flist[3] flist[4] flist[5] flist[6] flist[7] flist[8] flist[9] flist[10] flist[11] flist[12]. END. |
The r-defb3.p procedure:
The r-defb4.p procedure has access to the rec buffer (and through it to the set of preselected records). This connection is made by using PRESELECT on the DEFINE SHARED BUFFER statement. The r-defb4.p procedure displays those records.
r-defb4.p
DEFINE SHARED BUFFER rec FOR {1} PRESELECT. REPEAT: FIND NEXT rec. DISPLAY {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12} {13} WITH 1 COLUMN 1 DOWN. END. |
Because r-defb3.p and r-defb4.p use run-time argument passing, they cannot be precompiled. Having separate versions of r-defb4.p for each table and running the appropriate one in r-defb3.p, should improve response time. This approach is worthwhile if there are many lines of code in r-defb4.p a procedure.
If you define a NEW SHARED BUFFER in a procedure, then call a subprocedure that puts a record into that buffer, and display the buffer in the main procedure, the AVM displays this message:
This message is displayed when the FIND statement is not in the main procedure:
To avoid this, explicitly scope the Customer record to the main procedure block. For example:
/* Main procedure */ DEFINE NEW SHARED BUFFER x FOR Customer. RUN proc2.p. DO FOR x: DISPLAY x. END. |
For examples of instance and static buffer data member definitions, see the descriptions of r-CustObj.cls, r-CustObjStatic.cls, and r-CustObjAbstract.cls in the CLASS statement reference entry.
If a trigger or internal procedure of a persistent procedure executes an external subprocedure that defines a SHARED buffer, ABL includes the persistent procedure in the resolution of the corresponding NEW SHARED buffer as though the procedure were on the procedure call stack.