75 std::cout <<
"\n--- Query error handling ---" << std::endl;
78 auto bad_sql = db->execute_query_result(
"THIS IS NOT VALID SQL");
85 auto missing_table = db->select_query_result(
"SELECT * FROM nonexistent_table_xyz");
86 if (!missing_table.is_ok())
88 print_error(
"missing table", missing_table.error());
92 db->create_query_result(R
"(
93 CREATE TABLE IF NOT EXISTS error_demo (
94 id INTEGER PRIMARY KEY,
95 name VARCHAR(50) NOT NULL
98 db->execute_query_result("DELETE FROM error_demo");
99 db->execute_query_result(
"INSERT INTO error_demo (id, name) VALUES (1, 'first')");
101 auto duplicate = db->execute_query_result(
102 "INSERT INTO error_demo (id, name) VALUES (1, 'duplicate')");
103 if (!duplicate.is_ok())
106 std::cout <<
" Constraint violation handled" << std::endl;
115 std::cout <<
"\n--- Chained operations with early return ---" << std::endl;
118 auto step1 = db->create_query_result(R
"(
119 CREATE TABLE IF NOT EXISTS chain_demo (
120 id SERIAL PRIMARY KEY,
121 value INTEGER NOT NULL
126 print_error(
"step 1 (create table)", step1.error());
129 std::cout <<
" Step 1: Table created" << std::endl;
132 auto step2 = db->execute_query_result(
133 "INSERT INTO chain_demo (value) VALUES (42)");
139 std::cout <<
" Step 2: Data inserted" << std::endl;
142 auto step3 = db->select_query_result(
"SELECT id, value FROM chain_demo");
148 std::cout <<
" Step 3: Read " << step3.value().size() <<
" row(s)" << std::endl;
151 if (step3.value().empty())
153 std::cerr <<
" [ERROR] No rows returned" << std::endl;
156 std::cout <<
" Step 4: Data verified" << std::endl;
185 std::cout <<
"=== error_handling example ===" << std::endl;
191 auto context = std::make_shared<database_context>();
192 auto db = std::make_shared<database_manager>(context);
193 db->set_mode(database_types::postgres);
195 std::string connection_string
196 =
"host=localhost port=5432 dbname=example_db user=user password=password";
198 auto connect = db->connect_result(connection_string);
199 if (!connect.is_ok())
201 std::cout <<
"\nSkipping remaining demos (no database connection available)"
203 std::cout <<
"=== error_handling example completed ===" << std::endl;
212 std::cout <<
"\n--- Connection info ---" << std::endl;
213 auto info = db->connection_info();
214 for (
const auto& [key, value] : info)
216 std::cout <<
" " << key <<
" = " << value << std::endl;
219 db->disconnect_result();
220 std::cout <<
"\nDisconnected" << std::endl;
222 std::cout <<
"=== error_handling example completed ===" << std::endl;
Dependency injection container for database system components.