Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

4.11 Cursors

Class RWDBCursor is the DBTools.h++ encapsulation of database cursors. Class RWDBCursor is a relatively low-level construct that maps directly onto a database cursor. It supports the cursor operations of bind, fetch, insert, update, and delete.

Despite the efforts of various standards bodies, cursor capabilities vary widely among database vendors.


NOTE: DBTools.h++ won't emulate functionality not supported by an underlying database engine.

For example, if a database vendor's implementation does not support scrollable cursors, an application requesting a scrollable RWDBCursor from that RWDBDatabase receives an RWDBCursor with a status of RWDBStatus::notSupported. For this reason, using RWDBCursor makes programs less portable than they might otherwise be.


NOTE: See your DBTools.h++ access library documentation for details concerning RWDBCursor restrictions for your particular database.

RWDBCursor captures common features of database cursors. Specifically:

Instances of RWDBCursor are created in much the same manner as RWDBSelector instances. The cursor() member function of the RWDBDatabase class produces an instance of RWDBCursor. There are also cursor() member functions for RWDBTable and RWDBSelector, each forwarding the actual creation of the cursor to its associated database. Only RWDBTable instances representing database tables produce usable RWDBCursor instances. In particular, RWDBMemTable and RWDBTPtrMemTable produce invalid cursors. The following lines create RWDBCursor instances:

Cursors can also be created with alternative access attributes. Some databases support scrollable cursors. These cursors need not access data linearly; they can jump around. Scrollable cursors are created with the special flag:

The insertion operator << is used to implement the cursor bind operation. When you use instances of RWDBCursor, you must provide the addresses of variables that will hold the data arriving from the database. In the following example, the cursor object returns three columns of data. Therefore, three variables are bound to hold the incoming data:

The addresses of the variables generally get passed all the way down to the level of the database vendor's API. Of course, in the example above, no vendor's API can understand how to insert data into an RWCString instance. DBTools.h++ recognizes this, so it allocates a buffer of the appropriate size and hands it down to the database level. As rows are fetched, DBTools.h++ arranges for results to be copied into the correct variables.

It is important to note that an application continues to own the memory pointed to and supplied to an RWDBCursor instance. It is the application's responsibility to ensure that a pointer remains valid for as long as the RWDBCursor requires it. In the following contrived example, a function is defined to produce an instance of RWDBCursor. However, the local variables bound to the cursor have gone out of scope when the function returns the RWDBCursor instance.

When the addresses of variables are inserted for binding, they are associated in order, beginning with the first column (index 0). The indexing operator
operator[] can set the position to a specified index, column, or column name. This allows you to bind variables out of order. Any of the following forms may be used:

The insertion on //1 binds the addresses of the variables x and y to the first and second columns of the cursor's result set, assuming that these are the first insertions since the construction or execution of the cursor. On //2, the variables are bound to columns numbered 7 and 5, respectively, where column indexing begins with 0. Line //3 binds the variables to the columns named col1 and col2. Finally, //4 binds to the columns in the cursor's result set which represent the columns table1.col1 and table2.col2 in the database.

Once the binding process is complete, the cursor is ready to fetch data. Invoking the member function fetchRow() accomplishes this. Using the addresses given to the cursor in the bind stage, one row of data is retrieved from the database. The following is an example of using a cursor for fetching data:

Instances of RWDBCursor can also be used to delete rows from a table. This use is an encapsulation of the SQL statement:

The following example allows users to delete names from a table:

Instances of RWDBCursor are also useful for updating rows in a table. This use of RWDBCursor is an encapsulation of the SQL statement:

The following example allows a user to correct names in a table:


Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.