Stored Procedures
When sending requests to the database, the request is first parsed then executed. Instead of parsing many times the same requests, i.e. with only changes in expression values, most of RDBMS enable to precompile requests. These requests can then be executed as routines and are identified by a name and a signature.
EiffelStore lets you use stored procedures with DB_PROC class to:
Executing a stored procedure
To execute a stored procedure:
- Create a DB_PROC object and load the stored procedure you want to use:
procedure: DB_PROC
...
create procedure.make ("UPDATE")
procedure.load
if procedure.exists then
...
end
- Execute the procedure through a DB_SELECTION (if a result is expected) or a DB_CHANGE object (otherwise).
You can execute your request mostly like a basic one:
- Create your request.
- Bind request variables. Variables are stored procedure arguments.
- Execute the query through the DB_PROC object.
procedure: DB_PROC
expr: DB_CHANGE
...
procedure.execute (expr)
expr.clear_all
- Check for errors and load result if any.
Creating a stored procedure
DB_PROC also enables you to create or drop stored procedures:
- Use store to create a procedure.
- Use drop to delete one.
The following example shows how to overwrite a procedure in the database: procedure: DB_PROC
...
create procedure.make ("NEW_PROCEDURE")
procedure.load
if procedure.exists then
procedure.drop
end
procedure.load
if not procedure.exists then
procedure.set_arguments (<<"one_arg">>, <<"">>)
procedure.store ("update contacts set firstname = one_arg where contactid = 1")
end