next up previous contents
Next: Binding and Defining Up: Database access Previous: Caveats to using KeyPtr's   Contents

Using SQL directly

One can use SQL directly, if one understands the structure of the database itself (discussion of which is currently beyond the scope of this document). One uses the interface supplied by three abstract base classes to SQL: TPhSQLDatabase, TPhSQLConnection, and TPhSQLStatement.

In order to execute a statement, one must follow several steps. First, one must initialize the database (once per Phat session). The normal way of doing this is:


	gPhDb = new TPhDatabase();
TPhDatabase uses TPhSQLDatabase, and is aware that the (current) Phobos database uses Oracle.

Second, one must authenticate yourself to this database. You must do this whenever you want to change the user you're logging in to Oracle as. To do this:


	gPhDb->Authenticate("userid","password");

Third, one gets a connection. You should only keep this connection as long as you need to, since otherwise multiple calls to the database can interfere with each other. The usual way of doing this is:


	TPhDatabase::TConn activate(gPhDb);
	TPhSQLConnection *conn = gPhDb.Connection();
The first statement creates a local object that ensures that the connection is released when you leave the scope of the function you are in. It only needs to be done once in a function, even if you refer to the connection more than once in a function. If for some reason you do not want the connection to be released upon leaving the function, you must explicitly use the functions TPhDatabase::Activate and ::Release - and be careful about early returns from your function!

Fourth, one creates a statement. This is obtained from the connection:


	TPhSQLStatement *s = 
	  conn->NewStatement("SELECT X FROM TABLE");
Note that the statement must be deleted by the user.


next up previous contents
Next: Binding and Defining Up: Database access Previous: Caveats to using KeyPtr's   Contents
Gunther Roland
2000-05-05