1105-1107

Previous Table of Contents Next

Page 1105

The TQuery object should then set its DataSource property equal to the name of the TDataSource object. When the detail TQuery is opened, it receives the value of ACCT_NO for the current summary record as the parameter to its SQL with no intervention at runtime. The TDataSource object is particularly useful in creating this kind of master-detail relationship.

Delphi also provides support for stored procedures through its implementation of the TStoredProc component. As with the other descendants of TResultSet, the TStoredProc object requires the name of a connected TDatabase object in its Database property. You should select the name of the stored procedure in the database from the drop-down list provided for the StoredProcName property. You use the ParamBindMode to indicate how parameters are bound at runtime, by name or by index. Set the Active property to False unless the stored procedure returns a result set. If this is the case, setting the Active property to True causes the stored procedure to fire.

The TStoredProc component is ideal for applying parametered transactions on data-entry forms. Stored procedures are executed in much the same way as SQL transactions applied through TQuery objects. Listing 49.3 provides an example of using a stored procedure to insert records from a simple data-entry form.

Listing 49.3. Demonstrating the use of the TStoredProc component.

 {This code should only be executed once,  perhaps in the form's constructor       } spInsIndiv.Prepare; {This code will be executed for each transaction} spInsIndiv.ParamByName(`Last').AsString := txtLast.Text; spInsIndiv.ParamByName(`First').AsString := txtFirst.Text; spInsIndiv.ParamByName(`DOB').AsString := txtDOB.Text; spInsIndiv.ParamByName(`Notes').AsString := txtNotes.Text; try     spInsIndiv.ExecProc;     Application.MessageBox(`A new record was added.',                                `Oracle Unleashed', mb_OK) except     Application.MessageBox(`Unable to add the current record.',                                `Oracle Unleashed', mb_OK); end; 

Note that the method for executing a stored procedure, ExecProc, is different from the method used to execute a query. As with dynamic SQL queries, you need to prepare the procedure before parameters are bound, but only once. Assigning values to parameters using the ParamByName method works in much the same way as accessing the array of TParams by index. Using ParamByName is probably a better choice because it results in more readable code.

Page 1106

TIP
You can view the parameters to stored procedures at design time by right-clicking the TStoredProc object and selecting Define Parameters from the pop-up menu.

When calling stored procedures with output parameters, you must allocate sufficient space for variables that are bound to the output parameters. This is a potential problem when using string variables . For example, the following lines of code result in a general protection fault:

 var     PChar:  szOut; begin     spGetNotes.Prepare;     spGetNotes.ParamByName(`NotesOut').AsString := szOut; 

The next code segment should work ( assuming that the length of the string returned from the database never exceeds 255 bytes):

 var     String:  szOut[255]; begin     spGetNotes.Prepare;     spGetNotes.ParamByName(`NotesOut').AsString := szOut; 

When using stored procedures, explicit transaction control might not be necessary. In most cases, any commits or rollbacks should be handled within the stored procedure. This is the safest way to process transactions, and it simplifies the code that applies transactions from the client side.

The methods of communicating with the database described in this section are a small subset of all the options provided by Delphi. Additional database components include the TBatchMove object and the TReport object, which lets you integrate ReportSmith reports with Delphi applications.

Summary

As mentioned in the introduction to this chapter, the object-oriented features, support for pointers and complex datatypes, and language structure provided by Object Pascal are among Delphi's greatest strengths.

In addition to the standard datatypes, Object Pascal allows the creation of any type of pointer, including pointers to user -defined classes and structures (records, in Pascal terminology). Pointers are allowed within complex datatypes and class definitions as well.

Class declarations in Object Pascal are very similar to C++ declarations. The capability to declare member data elements and private functions improves encapsulation and allows for information hiding. Also in the C++ tradition, Delphi's model of inheritance allows base class

Page 1107

functions to be overridden by descendants, allowing polymorphic behavior in user-defined classes. Unfortunately, support for overloaded member functions seems to be lacking.

Another strong feature of Object Pascal is its support for structured exception handling. Listings 49.1 and 49.3 provide examples of the try...except blocks used to handle exceptions in Delphi. You can use the finally keyword to create a resource protection block that frees memory without handling the exception. Listings 49.1 and 49.3 do not actually handle the exceptions either. Delphi provides a fairly comprehensive list of predefined exceptions that can be handled using the syntax

 on exception do statement 

An exception instance is never destroyed until it is handled. User-defined exceptions can also be declared and thrown.

Delphi's weaknesses are few, considering that it is a first version of a fairly complicated development tool. Aside from the occasional UAE and some bugs in the security features of the local database server, Delphi seems to be a stable and solid product. The database objects, although easy to use, do seem to be geared more to desktop applications than to client/server development, with heavy emphasis on bound controls. However, SQL traces show that it makes very efficient use of the ODBC API in certain situations. This is because it maps to the API more cleanly than most Windows development tools' ODBC layers , which typically don't provide methods for using prepared execution. The close relationship to ODBC is somewhat surprising considering that IDAPI drivers are shipped with the product. For developers who are already familiar with the ODBC API, this should make a switch to IDAPI nearly transparent. For Java connectivity, refer to Chapter 53, "Web Database Connectivity."

Overall, Delphi goes a long way toward living up to its billing as "the radical performance tool." The IDE is intuitive, the object hierarchy well designed, and the Object Pascal programming language is arguably the best among the desktop development tools. The easy-to-configure ODBC connectivity and support for stored procedures make it a good choice for developing client applications to interface with Oracle.

Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net