Data Access Library for Eiffel contents structure

DATABASE

Following the whole schema, to execute a query and, probably to obtain the results, you have to open a database in the context of that session. You already have got the first acquaintance with the DATABASE class in the "Hello, WORLD!" sample. Now lets consider the basic interface of this class:

To attach to a SESSION you can create DATABASE using attach as a creation procedure. As a rule, however, the only one default session is quite enough. Therefore, you can use the make creation procedure to open a default session and attach to it.

What you need to concentrate on is how to establish a connection to the data source. You can choose one of two features for that purpose. The first one, connect, is the simplest one. It allows to connect to a data source whose name is already known and when no need in an additional parameters except of user name and password. This way was chosen in our "Hello, WORLD!" example in Chapter I.

Sometimes, however, you need something more. For example, you want to choose data source from the prompt list or specify some additional connecting information. The DATABASE class contains the driver_connect feature for all those purposes. For example, to choose data source from prompt list you can code:

database.prompt_connect

Anothor example. Lets suppose you need to connect to the pubs database in MS SQL Server. The following code does that:

database.driver_connect (
	0,
	"DSN=Pubs;UID=sa;PWD=;DATABASE=pubs;",
	const.SQL_DRIVER_NOPROMPT)

The emphasized text is necessary there to automatically switch context to pubs. Otherwise, you probably need to send the USE pubs SQL query after connection.

Note, that three features above are prototypes of the corresponding ODBC API functions so you can slip into ODBC API manual if necessary. The same is true for many features of various classes whose descriptions are in this Guide.

There is a group of features for transaction management. In simpler cases you can make use of the begin_transaction, end_transaction and break_transaction features. Note, that begin_transaction turns off autocommitting mode if any, and the end_transaction restores this mode.

If this functionality is not enough you can take advantage of the TRANSACTION class via the transaction feature. Note, that this attribute is Void if transactions are not supported.

Another group of features is presented by the execute, recordset, call_procedure and call_function. The first one is the simplest way to allows to execute an arbitrary SQL query how it is demonstrated in the ADMIN example. If some results are expecting you can obtain it via the recordset feature. Next two features simplify a call of the stored procedure which doesn't return any result value (call_procedure) or if such a value is expected (call_function). See the QUERY, RECORDSET and DB_PROCEDURE for more explanations.

Sometimes you need to find out the contents about the database: enumerate existing tables, stored procedures, data types. For this purpose the DATABASE class offers the tables, procedures and types features respectively. See the TABLES, PROCEDURES and TYPES classes for details.

Very often you need to open a table in the database to manipulate its contents. As you will see reading the DB_TABLE's description, you can make use of its creation procedure. There is however more elegant way - to take advantage of the table feature. This feature accepts a table's name as an argument and returns the DB_TABLE object if such a table exists or Void otherwise. The same is true for the procedure feature.


Copyright © Object Tools -- info@object-tools.com
Last updated: 2005/02/02 11:51:24