[[Property:title|Data Object Coupling]]
[[Property:weight|4]]
[[Property:uuid|c51311cd-8782-0bc3-3ef7-000ea6eee37c]]
A smart way to work with relational databases is to have Eiffel objects directly mapping relational tables. Three EiffelStore classes enable this coupling:
* [[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]]  objects [[#describe|describe a relational table]]  and allow Eiffel to create objects mapping database tables. 
* [[ref:/libraries/store/reference/db_store_flatshort|DB_STORE]]  works directly with [[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]]  objects to [[#insert|insert data into relational tables]] . 
* [[ref:/libraries/store/reference/db_selection_flatshort|DB_SELECTION]]  can [[#map|map a database query result into Eiffel objects]] . 

==Describing relational tables with DB_REPOSITORY==

A [[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]]  object stores available information about a table. To access this information, you mainly have to give the table name and load the table description:
<code>
    repository: DB_REPOSITORY

                ...

            create repository.make ("CONTACTS")
            repository.load
            if repository.exists then
                ...
            end
</code>


{{tip|Loading a table description is often a costly operation: table has to be fetched among existing tables then every table column description must be loaded. Hence it is better to store and reuse a repository (maybe with a HASH_TABLE) once it has been loaded. }}


Using the table information, [[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]]  then helps generating Eiffel classes mapping relational tables: 
* You can directly use {[[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]] }.generate_class. Generated class may look like:
<code>
class CONTACTS

feature -- Access

    id: INTEGER
	...
feature -- Settings

    set_id (an_id: INTEGER) 
            -- Set an_id to id.
        do
            id := an_id
        end
            ...
</code>


{{note|The EiffelStore Wizard uses the generation.generator cluster to generate the classes mapped to your database. }}


==Inserting data in the database==

[[ref:/libraries/store/reference/db_store_flatshort|DB_STORE]]  lets you easily insert rows into a table using: 
* the table description provided by [[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]] .
* a class mapping the relational table. 
This is straight-forward since you only have to give [[ref:/libraries/store/reference/db_store_flatshort|DB_STORE]]  the object filled with the table values. Suppose you want to add a contact into your database: 
<code>
    storage: DB_STORE
    contacts_rep: DB_REPOSITORY
    a_contact: CONTACTS

                ...

            create storage.make
                -- contacts_rep is loaded and exists.
            storage.set_repository (contacts_rep)
                -- a_contact carries values to insert into the database.
            storage.put (a_contact)
</code>

==Accessing database content with Eiffel objects==

[[ref:/libraries/store/reference/db_selection_flatshort|DB_SELECTION]]  lets you map data retrieved from the database into Eiffel objects: Result column names must match object attributes names so you can use for instance classes created by [[ref:/libraries/store/reference/db_repository_flatshort|DB_REPOSITORY]] . Class <eiffel>DB_ACTION</eiffel> redefines ACTION and can be used to retrieve Eiffel objects directly into an ARRAYED_LIST:
<code>
    selection: DB_SELECTION
    list_filling: DB_ACTION [CONTACTS]
    contact: CONTACTS

                ...

            selection.object_convert (contact)
            create list_filling.make (selection, contact)
            selection.set_action (list_filling)
                ...
            selection.load_result
            if selection.is_ok then
                Result := list_filling.list
            end
</code>


{{note|You can see how actions are used in [[ref:/libraries/store/reference/db_selection_flatshort|DB_SELECTION]] . }}
<br/>

{{seealso|<br/>
[[Database Selection|Performing a database selection.]]  <br/>
[[Data structures use|Database-specific structures use.]]  <br/>
}}




