GNADE User's Guide: GNADE, The GNat Ada Database Environment; Version 1.5.0; Document Revision $Revision: 1.42 $ | ||
---|---|---|
Prev | Next |
For the communication with the underlying database the application uses a so called connection object. The connection object uses a so called driver handle in order to communication with the database specific driver. The code fragment below shows how to declare a connection to a MySQL data base.
procedure Client is ............. X : Driver.Handle := Driver.MySQL.Create; C : Connection.Object(X); .............
In order to connect to the data base, the Connect method has to be invoked as shown below, where the parameters do depend on the actual data base used.
begin ... Connect(C, "gnade", Password => "", Database => "gnade" );
After the connection has been established statements for execution may be prepared. In order to do so, the method prepare has to be invoked as shown below.
-- prepare a query S := Prepare( C, "select empno, name, firstname from EMPLOYEES " & "where empno > :emp and name = :id ;"); Bind(S, "id", V ); Bind(S, "emp", E );
This method analysis the given statements for so called hostvariables and invokes the underlying data base in order to evaluate an execution plan.
The following Bind commands connect the hostvariables V and E with the host variable names "id" and "emp". The variables V,E are input parameters for the query.
In order to execute the query, the following code fragment is used:
-- execute a query Value( V, "Bundy" ); Value( E, 50 ); R := Execute(C, S );
This fragment sets the values of the two hostviariables V and E and executes the statement denoted by S on the connection C. The result of this execution is the result set denoted by the variable R.
A result set denotes all all records which are generated by the execution of an SQL query statement.
-- retrieve the result declare package D_Empname is new String_Domain( Size => Name_Type'Length, Result => R, Name => "name"); package D_Firstname is new String_Domain( Size => Name_Type'Length, Result => R, Name => "firstname"); package D_Empno is new Integer_Domain( Number_Type => Empno_Type, Result => R, Name => "empno"); begin while not End_Of_Result(R) loop Fetch( R ); Put_Line(Integer'Image(D_Empno.Value) & " " & D_Empname.Value & D_Firstname.Value ); end loop; end ; Deallocate( R );