version 1.5
Until now, you have seen how data is loaded into 4th Dimension, but you have not seen how to update data on the Oracle server. You will now see how to modify the SCOTT.EMP table on the server. You will use three methods in your example database.
Activating a Context
The Activate method activates a context and displays the selected rows in an output form:
If (OD Login state (ID_Login)>0) If (OD Context state (ID_Context)>0) $rc:=OD Activate context (ID_Login;ID_Context) ALL RECORDS ([Employees]) DELETE SELECTION ([Employees]) $n:=OD Load rows context (ID_Context) ALL RECORDS ([Employees]) ORDER BY ([Employees];[Employees]Num;>) End if End if
Deactivating a Context
The Deactivate method deactivates the context after checking its validity:
If (OD Context state (ID_Context)>1) OD DEACTIVATE CONTEXT (ID_Context) End if
Creating and Modifying Records
The ManageContext method allows you to modify or create records.
If (OD Context state (ID_Context)>1) `Tests whether the context is valid Case of :(Before) fCreation:=Modified record ([Employees]) If (Not(fCreation)) If (Selected record number ([Employees])<=OD Records in context (ID_Context)) $rc:=OD Goto in context (ID_Context;Selected record number([Employees])) End if End if :(After) If (fCreation) $rc:=OD Insert in context (ID_Context) Else If(Selected record number ([Employees])=OD Number in context(ID_Context)) $rc:=OD Update in context (ID_Context) End if End if End case End if
This method tests whether the context is valid and active using the OD Context state function.
When a record is accepted, one of the following three cases can occur:
Record creation: When a record has been created, the Modified record function returns True in the Before phase. To verify that the record has been accepted, you test the fCreation flag in the After phase. You then call OD Insert in context to create the record on the server.
Context record modification: The method verifies that the record is part of the context by comparing the record selection number with the number of records defined in the context. It then moves to the correct Oracle row by calling OD Goto in context in the Before phase. To update the row, you call OD Update in context in the After phase.
Modification of a record not in the context: 4th Dimension validates the record.
To keep the tutorial simple, the ManageContext method uses selection numbers to determine which row should be updated on the server based on the order of the 4th Dimension record in the current selection. In your own applications, you should not use selection numbers to update data. Instead, you should construct your context definitions so that there is a primary key that uniquely identifies each row.
To create and manage records using a context
The ManageContext method is called in the table method of the [Employees] table.
To see the table method:
1. Enter the Design environment, open the Method editor, select the [Employees] table, and click Open.
By using a table method, you ensure that the updates will occur regardless of the form you are using.
2. Return to the User environment and execute the Activate method.
The following screen appears:
3. Double-click the record for SMITH.
The record appears in an input form.
4. Change SMITH's salary to $1,500 and click OK.
The corresponding row is updated on the server.
5. Create a new record by choosing New Record from the Enter menu.
6. Enter information into the record and click OK.
A new row is created on the server.
To verify that the changes were made on the server, execute the Deactivate and Activate methods. This removes the 4th Dimension records from the output form and loads an updated set of records from the server.
It is important to note that the records being removed were never saved in 4th Dimensionthey were kept in temporary memory for the purpose of displaying the data to the user. Saving a copy of the records to a 4th Dimension data file is unnecessary, because the data is stored on the Oracle server.