mirror of
https://github.com/EiffelSoftware/eiffel-org.git
synced 2025-12-08 15:52:26 +01:00
Author:halw
Date:2008-12-09T17:39:05.000000Z git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@131 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
@@ -22,8 +22,8 @@ The <eiffel>HANDLE_USE </eiffel>class provides access to the unique <eiffel>HAND
|
||||
|
||||
* Database status information:
|
||||
<code>
|
||||
status: DB_STATUS
|
||||
-- Status of active database
|
||||
status: DB_STATUS
|
||||
-- Status of active database
|
||||
</code>
|
||||
|
||||
|
||||
@@ -35,13 +35,13 @@ Every interface class inherits from the <eiffel>HANDLE_USE</eiffel> class and ca
|
||||
|
||||
The creation procedure for a <eiffel>DB_CHANGE</eiffel> object is for instance:
|
||||
<code>
|
||||
make
|
||||
-- Create an interface object to change active base.
|
||||
do
|
||||
implementation := handle.database.db_change
|
||||
create ht.make (name_table_size)
|
||||
implementation.set_ht (ht)
|
||||
end
|
||||
make
|
||||
-- Create an interface object to change active base.
|
||||
do
|
||||
implementation := handle.database.db_change
|
||||
create ht.make (name_table_size)
|
||||
implementation.set_ht (ht)
|
||||
end
|
||||
</code>
|
||||
|
||||
==Access to the DBMS call interface==
|
||||
@@ -65,16 +65,18 @@ Let us give some details about the set_base feature:
|
||||
|
||||
The corresponding code looks like:
|
||||
<code>
|
||||
session_status: DB_STATUS
|
||||
-- A session management object reference.
|
||||
...
|
||||
set_base
|
||||
...
|
||||
update_handle
|
||||
if session_status = Void then
|
||||
create session_status.make
|
||||
end
|
||||
handle.set_status (session_status)
|
||||
session_status: DB_STATUS
|
||||
-- A session management object reference.
|
||||
|
||||
...
|
||||
|
||||
set_base
|
||||
...
|
||||
update_handle
|
||||
if session_status = Void then
|
||||
create session_status.make
|
||||
end
|
||||
handle.set_status (session_status)
|
||||
...
|
||||
</code>
|
||||
|
||||
|
||||
@@ -23,13 +23,15 @@ Once you have handled your error, for instance by displaying the error_message o
|
||||
|
||||
The following example sum up these capabilities:
|
||||
<code>
|
||||
session_control: DB_CONTROL
|
||||
...
|
||||
session_control.connect
|
||||
if session_control.is_connected then
|
||||
-- Perform your transactions here.
|
||||
session_control.disconnect
|
||||
end
|
||||
session_control: DB_CONTROL
|
||||
|
||||
...
|
||||
|
||||
session_control.connect
|
||||
if session_control.is_connected then
|
||||
-- Perform your transactions here.
|
||||
session_control.disconnect
|
||||
end
|
||||
</code>
|
||||
|
||||
==Committing changes in the database==
|
||||
@@ -40,19 +42,21 @@ With many database systems, modifications you make to the database are usually n
|
||||
|
||||
The following example illustrates the use of these commands:
|
||||
<code>
|
||||
session_control: DB_CONTROL
|
||||
...
|
||||
from
|
||||
until
|
||||
transaction_completed or else not session_control.is_ok
|
||||
loop
|
||||
-- Perform your transaction here.
|
||||
end
|
||||
if session_control.is_ok then
|
||||
session_control.commit
|
||||
else
|
||||
session_control.rollback
|
||||
end
|
||||
session_control: DB_CONTROL
|
||||
|
||||
...
|
||||
|
||||
from
|
||||
until
|
||||
transaction_completed or else not session_control.is_ok
|
||||
loop
|
||||
-- Perform your transaction here.
|
||||
end
|
||||
if session_control.is_ok then
|
||||
session_control.commit
|
||||
else
|
||||
session_control.rollback
|
||||
end
|
||||
</code>
|
||||
|
||||
The loop performs a multi-step transaction. If transaction is not carried out entirely, the database could stay in an invalid state: this code ensures that database remains in a valid state.
|
||||
|
||||
@@ -15,11 +15,13 @@ Template queries are parsed to replace each variable by its bound value. To crea
|
||||
|
||||
Variables syntax is simple: the ':' special character followed by the variable name.
|
||||
<code>
|
||||
selection: DB_SELECTION
|
||||
Bind_var: STRING = "firstname"
|
||||
...
|
||||
create selection.make
|
||||
selection.set_query ("Select * from CONTACTS where Firstname = ':" + Bind_var + "'")
|
||||
selection: DB_SELECTION
|
||||
Bind_var: STRING = "firstname"
|
||||
|
||||
...
|
||||
|
||||
create selection.make
|
||||
selection.set_query ("Select * from CONTACTS where Firstname = ':" + Bind_var + "'")
|
||||
</code>
|
||||
|
||||
{{note|The code example shows how to bind variables to a [[ref:/libraries/store/reference/db_selection_chart|DB_SELECTION]] object but the mechanism is exactly the same for [[ref:/libraries/store/reference/db_change_chart|DB_CHANGE]] objects. }}
|
||||
@@ -28,16 +30,18 @@ Variables syntax is simple: the ':' special character followed by the variable n
|
||||
|
||||
Once you have created your query, you can map variable names to values and execute the query:
|
||||
<code>
|
||||
selection: DB_SELECTION
|
||||
Bind_var: STRING is "firstname"
|
||||
...
|
||||
loop
|
||||
io.read_line
|
||||
selection.set_map_name (io.laststring, Bind_var)
|
||||
selection.execute_query
|
||||
...
|
||||
selection.unset_map_name (Bind_var)
|
||||
end
|
||||
selection: DB_SELECTION
|
||||
Bind_var: STRING is "firstname"
|
||||
|
||||
...
|
||||
|
||||
loop
|
||||
io.read_line
|
||||
selection.set_map_name (io.laststring, Bind_var)
|
||||
selection.execute_query
|
||||
...
|
||||
selection.unset_map_name (Bind_var)
|
||||
end
|
||||
</code>
|
||||
|
||||
{{seealso|<br/>
|
||||
|
||||
@@ -12,13 +12,15 @@ EiffelStore lets you use stored procedures with [[ref:/libraries/store/reference
|
||||
To execute a stored procedure:
|
||||
* Create a [[ref:/libraries/store/reference/db_proc_flatshort|DB_PROC]] object and load the stored procedure you want to use:
|
||||
<code>
|
||||
procedure: DB_PROC
|
||||
...
|
||||
create procedure.make ("UPDATE")
|
||||
procedure.load
|
||||
if procedure.exists then
|
||||
...
|
||||
end
|
||||
procedure: DB_PROC
|
||||
|
||||
...
|
||||
|
||||
create procedure.make ("UPDATE")
|
||||
procedure.load
|
||||
if procedure.exists then
|
||||
...
|
||||
end
|
||||
</code>
|
||||
|
||||
* Execute the procedure through a [[ref:/libraries/store/reference/db_selection_chart|DB_SELECTION]] (if a result is expected) or a [[ref:/libraries/store/reference/db_change_chart|DB_CHANGE ]] object (otherwise).
|
||||
@@ -33,11 +35,13 @@ You can execute your request mostly like a basic one:
|
||||
|
||||
** Execute the query through the DB_PROC object.
|
||||
<code>
|
||||
procedure: DB_PROC
|
||||
expr: DB_CHANGE
|
||||
...
|
||||
procedure.execute (expr)
|
||||
expr.clear_all
|
||||
procedure: DB_PROC
|
||||
expr: DB_CHANGE
|
||||
|
||||
...
|
||||
|
||||
procedure.execute (expr)
|
||||
expr.clear_all
|
||||
</code>
|
||||
|
||||
** Check for errors and load result if any.
|
||||
@@ -50,19 +54,20 @@ DB_PROC also enables you to create or drop stored procedures:
|
||||
* Use drop to delete one.
|
||||
The following example shows how to overwrite a procedure in the database:
|
||||
<code>
|
||||
procedure: DB_PROC
|
||||
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
|
||||
...
|
||||
|
||||
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
|
||||
</code>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user