Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


SQL or Stored Procedures
SQL or Stored Procedures

When we last left off, our hero was perched precariously on the edge of a cliff...oops, wrong article.

When we last left off, we had a functional shell of an application. We had a menu that would open our five different sheets (Controls, Users, Messages, Menus, and Contacts); we had the shell of those five sheets themselves; we had some basic shared functionality with regards to managing sheets (arranging sheets, closing all open sheets); we had some basic shared DataWindow/DataStore error handling in the DBError event of our base-class u_dw and n_ds objects; we had some basic transaction management hooks in place in our base-class n_tr transaction object.

The list of the remaining steps is still quite long. We'll need to make some decisions regarding security (login and functional access), our transaction model, and our data access model. We'll have to design our DataWindows and make some decisions regarding the rest of the look-and-feel of the application sheets.

This month, I'd like to plow forward and define our data access model.

When I refer to a data access model, I'm referring to our methodology for selecting and updating the data the application will present. Although selection and update models can be different, it's usually more straightforward and certainly more consistent to use the same method for both. Assuming your application will be getting its data from a database (not always the case), you have two basic choices. As an aside, I'll simply add that, generally speaking and in my (humble) opinion, the fewer the choices the better - you can simply get down to designing and coding that much faster.

The two main choices for getting data from a database are SQL and Stored Procedures. As with everything else, there are advantages and disadvantages to each. Both can be used as a DataWindow object's data source. If you use SQL as the data source, the SQL code is stored with the DataWindow object. This makes its reuse as a data source for another DataWindow object or another application impossible. Any changes to the logic of the select must be made in the DataWindow object, which necessitates a new application release. In most cases, this may be a minor issue because you're probably making application changes anyway. But there may be times when you really only need to update a where clause or add an order by clause. In this case you still have to deploy the application and application deployment is not always easy.

The other choice is the use of stored procedures. Stored procedures have several advantages over using SQL as the DataWindow object data source. Firstly, there's a slight performance boost since the SQL has already been compiled and the query plan has already been generated. The database server simply executes the query plan to get the data. Depending on the nature of your data, however, it's very important to have the stored procedure recompiled periodically to ensure that the saved query plan is optimal for the data in the table. You can usually have the DBA group recompile your procedures weekly or at whatever interval is required. Secondly, by moving the data access out of your application, you can reuse the data access logic that's stored in the procedure from other places in your application or other applications. Thirdly, you can employ, as necessary, the more complex logic that may be required to generate the result set. You can use temporary tables, do calculations, invoke other stored procedures, or access data from other tables by using more advanced database features like proxy tables. The sky's the limit with stored procedures.

From a database security standpoint, stored procedures can be secured in ways that SQL can't. To select data using a SQL statement, the user must be given SELECT access to the tables that will be accessed. That means the user, or more importantly, anyone impersonating the user, will be able to see ALL the data in those tables. This may constitute a security violation if the data is highly confidential, for example. The application, on the other hand, may only be allowing view access to SOME of the data.

With stored procedures, a privilege must be granted to the user so the user can execute the stored procedure and have access to the result set. A user can be granted execute privileges WITHOUT being granted any access to the underlying tables accessed in the stored procedure. This is a key differentiator. When I worked for a law firm a number of years ago, the design decision was to withhold direct select, update, insert, and delete access from all tables in the system, including system tables. This way, even if someone could get into the database, they wouldn't be able to query the system tables even to find out the names of the stored procedures, let alone have access to the underlying data.

If your database doesn't support stored procedures, your choice is easy...SQL. If your database does support stored procedures, even if you don't have security constraints or other applications that need to share access to the same select logic, I strongly suggest you use (or begin to use) stored procedures as your access mechanism.

For inserts, updates and deletes, there are also two main choices. You can use the built-in DataWindow object update capabilities or you can use stored procedures. We'll see examples of everything as we start building the actual DataWindows we need for our application.

You can tell Powerbuilder to generate the appropriate insert/update/delete SQL statements during the DataWindow Update() call by invoking the Update Properties dialog (see Figure 1) while in the DataWindow painter using the Rows | Update Properties menu item. You specify the table to update, which columns should be updated, the key columns for the table, how to build the where clause, and what should happen if a key value has been modified. In the case of an insert statement, the where clause and key modification selections are ignored. In the case of a delete, only the where clause choices are used. In the case of an update, potentially all selections are used by Powerbuilder to construct the appropriate update statement. For each new/modified/deleted row in the DataWindow, the appropriate SQL statement is generated from these selections and then executed. Note that all deletes are processed first, followed by the inserts and updates in the order that the modified rows are found in the DataWindow's primary buffer.

Powerbuilder supports DataWindow updates via stored procedures by letting you specify the stored procedures to invoke for inserts, updates, and deletes (see Figure 2). You indicate the stored procedure for each type of update and then map the DataWindow columns to the stored procedure arguments as needed. Arguments required for the stored procedure that aren't column-based can also be specified. Then, during the Update() call, Powerbuilder invokes the appropriate procedure for each inserted, modified, and/or deleted row. In this scenario, as above, deletes will be processed first, followed by the inserts/updates in the order the rows were found in the Primary buffer.

Note that the DataWindow update properties will be used if a stored procedure has not been specified. For example, if you specify only insert and update stored procedures, deletes will be handled via the update properties of the DataWindow. This can become a very confusing situation if you're not careful and consistent. I strongly advise that if you decide to use stored procedures for one kind of update, make sure you define stored procedures for all three kinds and make sure that the Allow Updates checkbox on the Update Properties dialog is unchecked. You don't have to allow updates in the Update Properties dialog if you specify stored procedures for updates. Further, if your application, for example, doesn't allow deletes, you don't have to specify that a stored procedure be invoked for deletes. You should only specify those procedures that you need to support your application functionality. On the other hand, if your application allows row deletions and you don't specify a stored procedure for deletes and you don't specify that the DataWindow is updatable via the Update Properties dialog, the Update() function will fail because no delete criteria has been specified. This can quickly become very confusing so, again, choose one way to handle your updates consistently and be sure the other method isn't available to get in the way.

For purposes of example and explanation we'll actually develop our DataWindows with a combination of stored procedures and SQL. Normally you'd never catch me doing this.

So, for our five sheets, let's map our data access as indicated in Table 1.


About Steve Katz
Steve Katz is a senior developer at HSBC Bank USA and has extensive experience developing applications utilizing PowerBuilder, Java, and other technologies. He has used PowerBuilder since v2.0a, taught at Techwave, and even wrote some articles about PowerBuilder a very long time ago.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

This article states the following:
"Depending on the nature of your data, however, it's very important to have the stored procedure recompiled periodically to ensure that the saved query plan is optimal for the data in the table. You can usually have the DBA group recompile your procedures weekly or at whatever interval is required."

Based on the DBMS, the recompilation of your stored procedures periodically is not necessary. ORACLE, for instance, determines the execution plan each and every time the procedure is executed. SQL*Server, on the other hand, does not.

When we last left off, we had a functional shell of an application. We had a menu that would open our five different sheets (Controls, Users, Messages, Menus, and Contacts); we had the shell of those five sheets themselves; we had some basic shared functionality with regards to managing sheets (arranging sheets, closing all open sheets); we had some basic shared DataWindow/DataStore error handling in the DBError event of our base-class u_dw and n_ds objects; we had some basic transaction management hooks in place in our base-class n_tr transaction object.


Your Feedback
Sandy Turner wrote: This article states the following: "Depending on the nature of your data, however, it's very important to have the stored procedure recompiled periodically to ensure that the saved query plan is optimal for the data in the table. You can usually have the DBA group recompile your procedures weekly or at whatever interval is required." Based on the DBMS, the recompilation of your stored procedures periodically is not necessary. ORACLE, for instance, determines the execution plan each and every time the procedure is executed. SQL*Server, on the other hand, does not.
SYS-CON India News Desk wrote: When we last left off, we had a functional shell of an application. We had a menu that would open our five different sheets (Controls, Users, Messages, Menus, and Contacts); we had the shell of those five sheets themselves; we had some basic shared functionality with regards to managing sheets (arranging sheets, closing all open sheets); we had some basic shared DataWindow/DataStore error handling in the DBError event of our base-class u_dw and n_ds objects; we had some basic transaction management hooks in place in our base-class n_tr transaction object.
Enterprise Open Source Magazine Latest Stories . . .
Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP). The...
With Cloud Expo 2012 New York (10th Cloud Expo) just four months away, what better time to start introducing you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference... We have technical and st...
AMD said late Tuesday that its chief sales officer Emilio Ghilardi had left the company and that CEO and president Rory Read is going to do his job while a replacement is sought. AMD didn’t say why Ghilardi left but it’s assumed Read wants his own people. Read is relatively new to th...
During the lifespan of M3 (Monitis Monitor Manager) there has always been something lacking – timers. M3 execution procedure was outlined in this previous article. The execution mentioned in the latter was a one-time-execution, whereas server monitoring requires periodic invocati...
Red Hat is putting its bought-in Gluster scale-out NAS storage technology, acquired in October, on the Amazon cloud. It’s styled Red Hat Virtual Storage Appliance for Amazon Web Services and other clouds are supposed to follow in short order.
A new episode of the screencast series is now available at the OpenNebula YouTube Channel. This screencast demonstrates the new easily-customizable self-service portal for cloud consumers. Its aim is to offer a simplified access to shared infrastructure for non-IT end users. The scree...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE