This example demonstrates C++20 range otl_view.
#include <iostream>
#include <string>
#include <vector>
#include <ranges> // C++20 Ranges
#include <algorithm> // for std::ranges::for_each
#define OTL_ODBC_ALTERNATE_RPC
#if !defined(_WIN32) && !defined(_WIN64)
#define OTL_ODBC
#else
#define OTL_ODBC_POSTGRESQL // required with PG ODBC on Windows
#endif
#define OTL_VIEW
#define OTL_STREAM_WITH_STD_OPTIONAL_ON
#include "otlv4.h"
using namespace std;
// Database connection object
otl_connect db;
void insert()
{
// Basic OTL insert stream
otl_stream o(10, "insert into test_tab values(:f1<int>, :f2<char[31]>, :f3<double>)", db);
// Normal values are streamed directly; std::optional is used only when entering NULLs
o << 1 << "James Bond" << 5500.50;
o << 2 << "Sherlock Holmes" << 6200.00;
o << 3 << "Tony Stark" << optional<double>(nullopt); // NULL salary entered
o << 4 << optional<string>(nullopt) << 9900.00; // NULL name entered
o << 5 << "Peter Parker" << 1200.25;
}
void select_with_ranges()
{
// Create an OTL stream for a query
otl_stream s(100, "select id, name, salary from test_tab where id >= :id<int>", db);
s << 1; // Bind parameter
cout << "--- Standard C++20 Range Loop ---" << endl;
// Inefficiency removed: using const auto& to prevent copying tuple components & std::string on each iteration
for (const auto& [id, name, salary] : otl_view<int, optional<string>, optional<double>>(s)) {
cout << "ID: " << id
<< " | Name: " << (name.has_value() ? *name : "NULL")
<< " | Salary: " << (salary.has_value() ? "$" + to_string(*salary) : "NULL") << endl;
}
// Rewind for the next demonstration (if supported by your driver)
s.rewind();
cout << "\n--- Range Composition (Pipes) ---" << endl;
auto modern_pipeline = otl_view<int, optional<string>, optional<double>>(s)
| views::filter([](auto&& row) {
const auto& salary = get<2>(row);
return salary.has_value() && *salary > 6000.0;
})
| views::take(2)
| views::transform([](auto&& row) {
const auto& name = get<1>(row);
return name.value_or("Unknown Agent");
});
for (const string& name : modern_pipeline) {
cout << "High Earner Name: " << name << endl;
}
}
int main()
{
// Initialize OTL environment
otl_connect::otl_initialize();
try {
// Connect to database
db.rlogon("scott/tiger@postgresql");
// Create table
otl_cursor::direct_exec(db, "drop table test_tab", otl_exception::disabled);
otl_cursor::direct_exec(db, "create table test_tab(id int, name varchar(30), salary float)");
insert(); // Populate table with mixed regular & NULL values
select_with_ranges(); // Show magic
} catch(otl_exception& p) {
// Standard OTL exception handling
cerr << "Error: " << p.msg << endl;
cerr << "SQL: " << p.stm_text << endl;
}
db.logoff(); // Disconnect
return 0;
}
Output
--- Standard C++20 Range Loop (Structured Bindings) ---
ID: 1 | Name: James Bond | Salary: $5500.5
ID: 2 | Name: Sherlock Holmes | Salary: $6200
ID: 3 | Name: Tony Stark | Salary: NULL
ID: 4 | Name: NULL | Salary: $9900
--- Range Composition (Pipes) ---
High Earner Name: Sherlock Holmes
High Earner Name: Unknown Agent
Copyright © 1996-2026, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com .
Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.