Selection Access

Once you have selected data from the database, it returns a set of rows containing queried columns values. Each row loaded with DB_SELECTION is stored in a DB_RESULT object. The easiest way to access the data is thus to refer to DB_RESULT objects themselves.

Note: Take a look at the Database/Eiffel objects Coupling to learn advanced data handling features.

To use DB_RESULT, process in 2 steps:

Retrieving DB_RESULT objects

DB_SELECTION class provides different ways to customize result loading:

  • You want to access an unique row: DB_RESULT object is accessible via cursor:

selection: DB_SELECTION my_result: DB_RESULT ... selection.query ("...") if selection.is_ok then selection.load_result my_result := selection.cursor end

  • You want to load a complete list of rows: DB_SELECTION can store DB_RESULT objects in a list. To do this, you have mainly to provide a LIST object to DB_SELECTION with set_container:

selection: DB_SELECTION container: ARRAYED_LIST [DB_RESULT] ... create container.make (Max_results) ... selection.set_container (container) selection.load_result ... from container.start until container.after loop ... end

Tip: Provide DB_SELECTION with the LIST structure convenient for what you need to do with the results.

  • You want to select part of the result set: you can set an action in DB_SELECTION that will be executed each time a row is loaded. This action can for instance manipulate current row and define a stop condition.
    • You need to define a descendant of class ACTION and set it to DB_SELECTION :

class MY_ACTION inherit ACTION redefine execute, found end ... execute do i := i + 1 end ... found: BOOLEAN do Result := i >= Max_result end

selection: DB_SELECTION action: MY_ACTION ... selection.set_action (action) selection.query ("...") if selection.is_ok then selection.load_result end

Accessing content of DB_RESULT

A DB_RESULT object merely carries data retrieved from the database. You have to convert it to a DB_TUPLE to access data within the retrieved row conveniently, i.e. mostly the column values: selection: DB_SELECTION tuple: DB_TUPLE ... create tuple.make_from_cursor (selection.cursor) if tuple.count >= 2 and then tuple.column_name (2).is_equal ("Firstname") then io.putstring (tuple.item (2).out) end

See Also:
Performing a database selection.
Coupling database data and Eiffel objects.

3b4fdde3-d903-55c8-0068-cee2407db280
cached: 09/23/2017 9:17:55.000 AM