![]() | ![]() |
Home |
|
|
DataWindow Programmer's Guide |
|
| Chapter 2: Using DataWindow Objects |
|
| Accessing the database |
Before you can display data in a DataWindow control, you must get the data stored in the data source into that control. The most common way to get the data is to access a database.
An application goes through several steps in accessing a database:
Set the appropriate values for the transaction object.
Connect to the database.
Set the transaction object for the DataWindow control.
Retrieve and update data.
Disconnect from the database.
To learn more about setting values for the transaction object, connecting to the database, and disconnecting from the database, see:
PowerBuilder
Application Techniques, "Using Transaction Objects"
Web DataWindow
Web ActiveX
There are two ways to handle database connections and transactions for the DataWindow control. You can use:
Internal transaction management
A separate transaction object
The two methods provide different levels of control over database transactions.
If you are displaying a PSR file in the control
You do not need to use a transaction object or make a database connection if you are displaying a PSR file in the DataWindow control.
If you change the DataWindow object
If you change the DataWindow object associated with a DataWindow control during execution, you may need to call the SetTrans or SetTransObject method again.
PowerBuilder
You always need to call one of the methods to set the transaction object.
Web ActiveX
You need to call SetTransObject again only when you are using a separate transaction object.
These options are described in this section.
Internal transaction managementWhen the DataWindow control uses internal transaction management, it handles connecting, disconnecting, commits, and rollbacks. It automatically performs connects and disconnects as needed; any errors that occur cause an automatic rollback.
Whenever the DataWindow needs to access the database (such as when a Retrieve or Update method is executed), the DataWindow issues an internal CONNECT statement, does the appropriate data access, then issues an internal DISCONNECT.
When not to use it
Do not use internal transaction management when:
Your application requires the best possible performance
Internal transaction management is slow and uses considerable system resources because it must connect and disconnect for every database access.
You want control over when a transaction is committed or rolled back
Because internal transaction management must disconnect after a database access, any changes are always committed immediately.
When to use it
If the number of available connections at your site is limited, you might want to use internal transaction management because connections are not held open.
Internal transaction management is appropriate in simple situations when you are doing pure retrievals (such as in reporting) and do not need to hold database locks--when application control over committing or rolling back transactions is not an issue.
PowerBuilder
To use internal transaction management, you specify connection values for a transaction object, which could be the automatically instantiated SQLCA. Then you call the SetTrans method, which copies the values from a specified transaction object to the DataWindow control's internal transaction object.
SQLCA.DBMS = ProfileString("myapp.ini", &"database", "DBMS", " ")
... // Set more connection parameters
dw_employee.SetTrans(SQLCA)
dw_employee.Retrieve( )
Connecting to the database
When you use SetTrans, you do not need to explicitly code a CONNECT or DISCONNECT statement in a script. CONNECT and DISCONNECT statements are automatically issued when needed.
For more information about PowerBuilder transaction objects, see PowerBuilder Application Techniques.
Web ActiveX
To use internal transaction management, set the transaction properties for the DataWindow Web ActiveX control instead of using a DataWindow Transaction Object control. You can set the properties via Param elements or in a script. This example sets the DbParm property and calls Retrieve in a script:
dw_employee.DbParm =
"Driver='com.sybase.jdbc.SybDriver',
URL='jdbc:sybase:Tds:www.domain.com:7373'";
dw_employee.Retrieve( );
For internal transaction management, you do not call SetTransObject. If you change the DataWindow object during execution, the connection information is still available and the DataWindow connects as needed. You can change the connection information by changing the value of the DbParm property.
Transaction management with a separate transaction objectWhen you use a separate transaction object, you control the duration of the database transaction. Your scripts explicitly connect to and disconnect from the database. If the transaction object's AutoCommit property is set to False, you also program when an update is committed or rolled back.
Typically, a script for data retrieval or update involves these statements:
Connect
SetTransObject
Retrieve or Update
Commit or Rollback
Disconnect
In PowerBuilder, you use embedded SQL for connecting and committing. For the Web ActiveX, the transaction object has methods that perform these actions.
The transaction object also stores error messages returned from the database in its properties. You can use the error information to determine whether to commit or roll back database changes.
When the DataWindow control uses a separate transaction object, you have more control of the database processing and are responsible for managing the database transaction.
There are several reasons to use a separate transaction object:
You have several DataWindow controls that connect to the same database and you want to make one database connection for all of them, saving the overhead of multiple connections
You want to control transaction processing
You require the improved performance provided by keeping database connections open
PowerBuilder
The SetTransObject method associates a transaction object with the DataWindow control. PowerBuilder has a default transaction object called SQLCA that is automatically instantiated. You can set its connection properties, connect, and assign it to the DataWindow control.
The following statement uses SetTransObject to associate the DataWindow control dw_emp with the default transaction object (SQLCA):
// Set connection parameters in the transaction object
SQLCA.DBMS = ...
SQLCA.database = ...
CONNECT USING SQLCA;
dw_emp.SetTransObject(SQLCA)
dw_emp.Retrieve( )
Instead of or in addition to using the predefined SQLCA transaction object, you can define your own transaction object in a script. This is necessary if your application needs to connect to more than one database at the same time.
The following statement uses SetTransObject to associate dw_customer with a programmer-created transaction object (trans_customer):
transaction trans_customer
trans_customer = CREATE transaction
// Set connection parameters in the transaction object
trans_customer.DBMS = ...
trans_customer.database = ...
CONNECT USING trans_customer;
dw_customer.SetTransObject(trans_customer)
dw_customer.Retrieve( )
Web ActiveX
To use a separate transaction object for the Web ActiveX, you add an OBJECT element for the Sybase DataWindow Transaction Object control to the Web page. You can set its connection properties using Param elements or a script.
A script that connects and retrieves data would have statements like these:
trans_1.Connect( );
dw_employee.SetTransObject( trans_1 );
dw_employee.Retrieve( );
trans_1.Disconnect( );
For more information about database transaction processing:
PowerBuilder
See the chapter on using transaction objects in Application Techniques
Web ActiveX
SeeChapter 8, "Using the DataWindow Web Control for ActiveX"
For more information about SetTrans and SetTransObject methods, see the DataWindow Reference.
You call the following two methods to access a database through a DataWindow control:
Retrieve
Update
After you have set the transaction object for your DataWindow control, you can use the Retrieve method to retrieve data from the database into that control:
dw_emp.Retrieve( )
The Web DataWindow server component has a second form of the method, RetrieveEx, for use when the method requires arguments. For more information about retrieving data with the Web DataWindow, see "Specifying the database connection and retrieving data" and "Passing page-specific data to the reloaded page".
Using retrieval argumentsRetrieval arguments qualify the SELECT statement associated with the DataWindow object, reducing the rows retrieved according to some criteria. For example, in the following SELECT statement, Salary is a retrieval argument defined in the DataWindow painter:
SELECT Name, emp.sal FROM Employee
WHERE emp.sal > :Salary
When you call the Retrieve method, you supply a value for Salary. In PowerBuilder, the code looks like this:
dw_emp.Retrieve( 50000 )
Special considerations for each environment are explained below.
When coding Retrieve with arguments, specify them in the order in which they are defined in the DataWindow object. Your Retrieve method can provide more arguments than a particular DataWindow object expects. Any extra arguments are ignored. This allows you to write a generic Retrieve that works with several different DataWindow objects.
Omitting retrieval arguments
If your DataWindow object takes retrieval arguments but you do not pass them in the Retrieve method, the DataWindow control prompts the user for them when Retrieve is called.
The Retrieve method is limited to 16 arguments in some environments.
PowerBuilder
You can specify any number of retrieval arguments.
Web DataWindow
You can specify a maximum of 16 arguments using the RetrieveEx method.
Web ActiveX
You can specify a maximum of 16 arguments for Retrieve. If you need to specify more, use the RetrieveEx method for the Web ActiveX and pass an array where each array element is a retrieval argument.
Updating dataAfter users have made changes to data in a DataWindow control, you can use the Update method to save those changes in the database.
In PowerBuilder, the code looks like this:
dw_emp.Update()
Update sends to the database all inserts, changes, and deletions made in the DataWindow control since the last Update method. When you are using an external transaction object, you can then commit (or roll back) those database updates. In PowerBuilder, you use SQL statements. In the Web ActiveX, you use methods and properties of the transaction object. In the Web DataWindow client control, update requests call the update method in the server component, which handles the commit or rollback.
For more specifics on how a DataWindow control updates the database (that is, which SQL statements are sent in which situations), see "Updating the database".
The following example shows code that connects, retrieves, updates, commits or rolls back, and disconnects from the database.
Although the example shows all database operations in a single script or function, most applications separate these operations. In a PowerBuilder application, for example, an application could connect to the database in the application Open event, retrieve and update data in one or more window scripts, and disconnect from the database in the application Close event.
PowerBuilder
The following statements retrieve and update data using the transaction object EmpSQL and the DataWindow control dw_emp:
// Connect to the database specified in the
// transaction object EmpSQL
CONNECT USING EmpSQL;
// Set EmpSQL as the transaction object for dw_emp
dw_emp.SetTransObject(EmpSQL)
// Retrieve data from the database specified in
// EmpSQL into dw_emp
dw_emp.Retrieve()
// Make changes to the data...
...
// Update the database
IF dw_emp.Update() > 0 THEN
COMMIT USING EmpSQL;
ELSE
ROLLBACK USING EmpSQL;
END IF
// Disconnect from the database
DISCONNECT USING EmpSQL;
Web ActiveX
The following JavaScript statements retrieve and update data using the transaction object EmpSQL and the DataWindow control dw_emp.
// Connect to the database specified in the
// transaction object EmpSQL
EmpSQL.Connect( );
// Set EmpSQL as the transaction object for dw_emp
dw_emp.SetTransObject(EmpSQL);
// Retrieve data from the database specified in
// EmpSQL into dw_emp
dw_emp.Retrieve();
// Make changes to the data
...
// Update the database
if (dw_emp.Update() > 0) {EmpSQL.Commit( );
} else {EmpSQL.Rollback( );
}
// Disconnect from the database
EmpSQL.Disconnect( );
A production application should include error tests after each database operation.
For more about checking for errors, see "Handling DataWindow errors".
|
|