Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
basic_connection.cpp File Reference

Demonstrates basic database connection and simple query execution. More...

#include <iostream>
#include <memory>
#include <string>
#include <variant>
#include "database/database_manager.h"
#include "database/core/database_context.h"
Include dependency graph for basic_connection.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

Demonstrates basic database connection and simple query execution.

This example shows how to:

  • Create a database_context and database_manager
  • Configure the database backend (PostgreSQL)
  • Connect to a database using a connection string
  • Execute a simple SQL query
  • Retrieve and display results
  • Disconnect cleanly

Prerequisites:

  • A running PostgreSQL server (or adjust for your backend)
  • Update the connection string below with valid credentials

Definition in file basic_connection.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 32 of file basic_connection.cpp.

33{
34 std::cout << "=== basic_connection example ===" << std::endl;
35
36 // Step 1: Create a dependency injection context.
37 // database_context manages shared components such as the performance
38 // monitor and entity manager.
39 auto context = std::make_shared<database_context>();
40
41 // Step 2: Create the database manager using the context.
42 auto db_manager = std::make_shared<database_manager>(context);
43
44 // Step 3: Select the database backend.
45 // Available types: database_types::postgres, database_types::sqlite, etc.
46 if (!db_manager->set_mode(database_types::postgres))
47 {
48 std::cerr << "Failed to set database mode to PostgreSQL" << std::endl;
49 return 1;
50 }
51 std::cout << "Database mode set to PostgreSQL" << std::endl;
52
53 // Step 4: Connect to the database.
54 // Adjust the connection string to match your environment.
55 std::string connection_string
56 = "host=localhost port=5432 dbname=example_db user=user password=password";
57
58 std::cout << "Connecting to database..." << std::endl;
59 auto connect_result = db_manager->connect_result(connection_string);
60
61 if (!connect_result.is_ok())
62 {
63 std::cerr << "Connection failed. Make sure PostgreSQL is running and\n"
64 << "the connection string is correct.\n"
65 << "Connection string format:\n"
66 << " host=<host> port=<port> dbname=<db> user=<user> password=<pass>"
67 << std::endl;
68 return 1;
69 }
70 std::cout << "Connected successfully" << std::endl;
71
72 // Step 5: Create a table (DDL query).
73 std::string create_sql = R"(
74 CREATE TABLE IF NOT EXISTS greetings (
75 id SERIAL PRIMARY KEY,
76 message VARCHAR(200) NOT NULL
77 )
78 )";
79
80 auto create_result = db_manager->create_query_result(create_sql);
81 if (create_result.is_ok())
82 {
83 std::cout << "Table 'greetings' is ready" << std::endl;
84 }
85 else
86 {
87 std::cerr << "Failed to create table" << std::endl;
88 }
89
90 // Step 6: Insert a row (DML query).
91 auto insert_result = db_manager->execute_query_result(
92 "INSERT INTO greetings (message) VALUES ('Hello from database_system!')");
93
94 if (insert_result.is_ok())
95 {
96 std::cout << "Row inserted" << std::endl;
97 }
98
99 // Step 7: Select rows and display results.
100 auto select_result
101 = db_manager->select_query_result("SELECT id, message FROM greetings ORDER BY id");
102
103 if (select_result.is_ok())
104 {
105 const auto& rows = select_result.value();
106 std::cout << "Query returned " << rows.size() << " row(s):" << std::endl;
107
108 for (const auto& row : rows)
109 {
110 for (const auto& [column, value] : row)
111 {
112 std::cout << " " << column << " = ";
113 std::visit([](const auto& v) { std::cout << v; }, value);
114 std::cout << std::endl;
115 }
116 }
117 }
118 else
119 {
120 std::cerr << "Select query failed: " << select_result.error().message << std::endl;
121 }
122
123 // Step 8: Disconnect.
124 db_manager->disconnect_result();
125 std::cout << "Disconnected" << std::endl;
126
127 std::cout << "=== basic_connection example completed ===" << std::endl;
128 return 0;
129}