|
Comments
|
Today's Top SOA Links
DataWindows Using the PFC Multitable Update Service
Using the PFC Multitable Update Service
By: Bob Hendry
Feb. 1, 1999 12:00 AM
In this month's column I'll show you how to use the PFC multitable update service. This service, as you might expect, allows the DataWindow to update more than one table. Multitable update, one of the least used PFC services, has been around since version 5.0. I'll demonstrate how you can use it, how it works and pitfalls you may find along the way.
How the DataWindow Updates
Allow Updates
Where Clause for Update/Delete
Where Clause Criteria - Key Columns
In Figure 2, Users 1 and 2 retrieve a row at the same time and both intend to modify the last name in the orders table. If User 1 makes the change, and User 2 is unaware that the data has already been updated, any changes from User 2 will overwrite the change made by User 1 - in other words, the database update will be successful...and never mind that the changes made by User 1 are overwritten. If it so happened that Users 1 and 2 retrieved the data at the same time and one of them updated the key column (in this case the order number), the next attempt to update would fail because the key column has changed. This technique is commonly used in single-user applications or when dealing with many-to-many tables where all columns make up the primary key.
Where Clause Criteria - Key and Modified Columns
Users 1 and 2 retrieve a row at the same time, both intending to modify the ship name. User 1 changes the ship name from Bates to Foy. The update is successful because both the key and the modified columns match their original values. However, when User 2 attempts to change the ship name to Hendry, the update will fail (see Figure 3). Since the ship name is now Foy, while on User 2's end it was expected to be Bates, User 2's entry won't overwrite the change by User 1. The database column ship_name no longer matches its original value. This update technique has a distinct advantage. If a user has updated a value in the database, this value can't be written over by another user. However, the technique also has a drawback (doesn't everything?). Consider the following scenario. Users 1 and 2 retrieve the same row at the same time. User 1 changes the ship name from Bates to Foy, while User 2 changes the ship city from Boogers Holler to Wayne. Both updates would be successful, but neither user would be aware that the other had made changes. The result would be inconsistent data (see Figure 3A).
Where Clause Criteria - Key and Updatable Columns
Optimistic Locking Errors
Table to Update
Before the DataWindow multitable update service was available, programmers were forced to write many DataWindow modify functions to change the DataWindow.Table.properties of the DataWindow. For those who actually want to try this, look up the UpdateTable, UpdateWhere and UpdateKeyInPlace properties. But believe me, trying to programmatically change the Update Properties of a DataWindow will make you want to throw your PC down the stairs.
Multitable Update Service
Adding the Code
The next step is to determine what tables we need to update, what identifies a record in a table as a primary key, what columns we'll allow the user to update, and what Update Properties (as discussed above) the DataWindow will use. When we've gathered all of that information, we'll put the code behind the Update command button. The first table we want to update is the sales order table. The primary key of the sales order table is the sales_order_id column. Believe it or not, this is all you need to start coding. Put the following code behind the command button Clicked event: String ls_tableThe most important part of this block of code is the of_register function. This function informs the multitable service that any update properties specified in the DataWindow painter will be overridden with those supplied in our PowerScript. In the example above, we're saying that we want to update the sales order table and the key is sales_order_id. The of_register function has three signatures. Signature 1: of_register(string as_table, string as_key_cols[]).In this signature, the programmer supplies the table to be updated and the key column(s) for the table. By default, all columns for that table are considered updatable. Also, the service assumes that you want to use Key and Updatable for the DataWindow Update Properties when building the Where clause. Finally, the service assumes that when performing the update, the row will first be deleted with a DELETE SQL statement, then reinserted with an INSERT SQL statement. In our examples we'll use this signature. Signature 2: of_register(string as_table, string as_key_cols[], string as_updatable_cols[]).This signature is similar to the first. The only difference is that specific updatable columns are being passed to the service. Use this signature when you don't want all of the columns in a table to be updatable. Signature 3: of_register(string as_table, string as_key_cols[], string as_updatable_cols[],boolean Use this signature when you want to supply every detail about how the table will be updated. Table 2 provides a more detailed look at the syntax.
The Rest of the Code
About the Code
When we're updating the product table, the array that holds the key columns needs to be reset. That's because the product table has only one key while the previous table has two. Just before the event is over, changes are saved to the database and the pfc_update event is used. When this event is fired, the update logic is redirected to the multitable update service. A regular DataWindow update function simply won't work. While we're talking about the PFC... remember, the use of most PFC services requires the entire application to be PFC-based. Mixing PFC components with non-PFC components, with few exceptions, is a recipe for disaster. To use the DataWindow multitable service, your entire application should be built on PFC components.
Final Note
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||