The odbc_resultset module provides a modern Fortran interface for handling ODBC query results in an object-oriented manner. It works in conjunction with the odbc_connection module to manage and navigate database query results, retrieve metadata, and access column data in various formats. The module encapsulates the complexity of the ODBC API, offering a simplified interface for iterating over rows and accessing column values by index or name.
This documentation describes the resultset type, its methods, and how to use it to process query results.
odbc_connection, odbc_columnset, sql, odbc_constants, and iso_c_binding modules, as well as the iso_fortran_env intrinsic module.odbc_connection module.resultsetThe resultset type represents a set of query results returned by an ODBC query. It contains private data members for the ODBC statement handle (stmt), row and column metadata, and error handling information. It also includes a columnset object to manage column metadata and data.
The resultset type provides the following public methods:
next(): Moves the cursor to the next row in the result set. Returns .true. if successful, .false. if no more rows are available.previous(): Moves the cursor to the previous row in the result set. Returns .true. if successful, .false. if no previous rows are available or if the cursor is not scrollable.first(): Moves the cursor to the first row in the result set. Returns .true. if successful, .false. if the result set is empty or not scrollable.last(): Moves the cursor to the last row in the result set. Returns .true. if successful, .false. if the result set is empty or not scrollable.nrows(): Returns the number of rows fetched in the current fetch operation.ncolumns(): Returns the number of columns in the result set.get_integer(col): Retrieves the value of the specified column (by index) as an integer.get_integer(name): Retrieves the value of the specified column (by name) as an integer.get_real(col): Retrieves the value of the specified column (by index) as a 32-bit real.get_real(name): Retrieves the value of the specified column (by name) as a 32-bit real.get_double(col): Retrieves the value of the specified column (by index) as a 64-bit real.get_double(name): Retrieves the value of the specified column (by name) as a 64-bit real.get_string(col): Retrieves the value of the specified column (by index) as a character string.get_string(name): Retrieves the value of the specified column (by name) as a character string.new(resultset, stmt): Initializes a resultset object with an ODBC statement handle (stmt) obtained from a connection object after executing a query.Errors are handled by the handle_errors subroutine, which retrieves diagnostic information from the ODBC API and prints an error message to the standard output, terminating the program with the error code.
A resultset object is created by calling the execute_query method of a connection object (from the odbc_connection module). The new subroutine is then called internally to initialize the resultset with the ODBC statement handle.
Use the navigation methods (next, previous, first, last) to move through the result set. The next method is typically used in a loop to process rows sequentially.
For scrollable cursors (specified in execute_query with scrollable=.true.), you can use previous, first, or last:
Retrieve column values using the get_integer, get_real, get_double, or get_string methods, either by column index (1-based) or column name. The module converts column data to the requested type by reading from a string representation.
The nrows and ncolumns methods provide metadata about the result set:
Note that nrows returns the number of rows fetched in the current fetch operation, which may depend on the ODBC driver's buffering.
Here is a complete example demonstrating the usage of the odbc_resultset module with the odbc_connection module:
If an ODBC operation fails, the handle_errors subroutine retrieves diagnostic information and prints an error message to the standard output, terminating the program. For example:
Ensure your query and database configuration are correct to avoid errors. Common issues include invalid column names, unsupported cursor types, or attempting to navigate a non-scrollable result set.
Data Types | |
| interface | new |
| Constructor interface for initializing a resultset object with an ODBC statement handle. More... | |
| procedure, pass, public first | ( | class(resultset), intent(inout) | this | ) |
Moves the cursor to the first row in the resultset, if the cursor is scrollable.
| [in,out] | this | The resultset object. |
Definition at line 36 of file resultset.f90.
| generic, public get_double | ( | class(resultset), intent(inout) | this, |
| integer, intent(in) | col ) |
Retrieves a odbc_columnset::column value as a 64-bit real by column index (1-based) from the resultset.
| [in,out] | this | The resultset object. |
| [in] | col | The column index (1-based). |
Definition at line 52 of file resultset.f90.
| generic, public get_double | ( | class(resultset), intent(inout) | this, |
| character(*), intent(in) | name ) |
Retrieves a odbc_columnset::column value as a 64-bit real by column name from the resultset.
| [in,out] | this | The resultset object. |
| [in] | name | The column name. |
Definition at line 52 of file resultset.f90.
| generic, public get_integer | ( | class(resultset), intent(inout) | this, |
| integer, intent(in) | col ) |
Retrieves a odbc_columnset::column value as an integer by column index (1-based) from the resultset.
| [in,out] | this | The resultset object. |
| [in] | col | The column index (1-based). |
Definition at line 48 of file resultset.f90.
| generic, public get_integer | ( | class(resultset), intent(inout) | this, |
| character(*), intent(in) | name ) |
Retrieves a odbc_columnset::column value as an integer by column name from the resultset.
| [in,out] | this | The resultset object. |
| [in] | name | The column name. |
Definition at line 48 of file resultset.f90.
| generic, public get_real | ( | class(resultset), intent(inout) | this, |
| integer, intent(in) | col ) |
Retrieves a odbc_columnset::column value as a 32-bit real by column index (1-based) from the resultset.
| [in,out] | this | The resultset object. |
| [in] | col | The column index (1-based). |
Definition at line 50 of file resultset.f90.
| generic, public get_real | ( | class(resultset), intent(inout) | this, |
| character(*), intent(in) | name ) |
Retrieves a odbc_columnset::column value as a 32-bit real by column name from the resultset.
| [in,out] | this | The resultset object. |
| [in] | name | The column name. |
Definition at line 50 of file resultset.f90.
| generic, public get_string | ( | class(resultset), intent(inout) | this, |
| integer, intent(in) | col ) |
Retrieves a odbc_columnset::column value as a string by column index (1-based) from the resultset.
| [in,out] | this | The resultset object. |
| [in] | col | The column index (1-based). |
Definition at line 54 of file resultset.f90.
| generic, public get_string | ( | class(resultset), intent(inout) | this, |
| character(*), intent(in) | name ) |
Retrieves a odbc_columnset::column value as a string by column name from the resultset.
| [in,out] | this | The resultset object. |
| [in] | name | The column name. |
Definition at line 54 of file resultset.f90.
| procedure, pass(this) handle_errors | ( | class(resultset), intent(inout), target | this | ) |
Definition at line 56 of file resultset.f90.
| procedure, pass, public last | ( | class(resultset), intent(inout) | this | ) |
Moves the cursor to the last row in the resultset, if the cursor is scrollable.
| [in,out] | this | The resultset object. |
Definition at line 37 of file resultset.f90.
| procedure, pass, public ncolumns | ( | class(resultset), intent(in) | this | ) |
Gets the number of odbc_columnset::column objects in the resultset.
| [in] | this | The resultset object. |
Definition at line 39 of file resultset.f90.
| procedure, pass, public next | ( | class(resultset), intent(inout) | this | ) |
Moves the cursor to the next row in the resultset.
| [in,out] | this | The resultset object. |
Definition at line 34 of file resultset.f90.
| procedure, pass, public nrows | ( | class(resultset), intent(in) | this | ) |
Gets the number of rows fetched in the current fetch operation of the resultset.
| [in] | this | The resultset object. |
Definition at line 38 of file resultset.f90.
| procedure, pass, public previous | ( | class(resultset), intent(inout) | this | ) |
Moves the cursor to the previous row in the resultset, if the cursor is scrollable.
| [in,out] | this | The resultset object. |
Definition at line 35 of file resultset.f90.
| subroutine resultset_new | ( | type(resultset), target | that, |
| type(sqlhstmt), intent(in) | stmt ) |
Initializes a resultset object with an ODBC statement handle, setting up row status and odbc_columnset::columnset metadata.
| [out] | that | The resultset object to initialize. |
| [in] | stmt | The ODBC statement handle. |
Definition at line 74 of file resultset.f90.