Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
postgres_advanced.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
21#include <iostream>
22#include <string>
23#include <memory>
24#include <vector>
25#include <variant>
28
29using namespace database;
30
31int main() {
32 std::cout << "=== Database System - PostgreSQL Advanced Features Example ===" << std::endl;
33
34 try {
35 // Create PostgreSQL manager instance
36 auto pg_manager = std::make_unique<postgres_manager>();
37
38 std::cout << "\n1. Database Connection:" << std::endl;
39
40 // Connection string (modify for your PostgreSQL server)
41 std::string connection_string = "host=localhost port=5432 dbname=testdb user=testuser password=testpass";
42
43 std::cout << "Attempting to connect to PostgreSQL..." << std::endl;
44 auto config = core::connection_config::from_string(connection_string);
45 auto connect_result = pg_manager->initialize(config);
46
47 if (connect_result.is_ok()) {
48 std::cout << "Successfully connected to PostgreSQL database" << std::endl;
49
50 // 2. Table creation with advanced features
51 std::cout << "\n2. Creating Advanced Table:" << std::endl;
52
53 std::string create_table_sql = R"(
54 CREATE TABLE IF NOT EXISTS products (
55 id SERIAL PRIMARY KEY,
56 name VARCHAR(100) NOT NULL,
57 description TEXT,
58 price DECIMAL(10,2),
59 tags TEXT[],
60 metadata JSONB,
61 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
62 )
63 )";
64
65 std::cout << "Creating products table with advanced PostgreSQL features..." << std::endl;
66 auto table_result = pg_manager->execute_query(create_table_sql);
67 if (table_result.is_ok()) {
68 std::cout << "Advanced products table created successfully" << std::endl;
69 } else {
70 std::cout << "Failed to create products table: " << table_result.error().message << std::endl;
71 }
72
73 // 3. Insert sample data
74 std::cout << "\n3. Inserting Sample Data:" << std::endl;
75
76 std::vector<std::string> insert_queries = {
77 R"(INSERT INTO products (name, description, price, tags, metadata) VALUES
78 ('Gaming Laptop', 'High-performance gaming laptop', 1299.99,
79 ARRAY['gaming', 'laptop', 'computer'],
80 '{"brand": "TechCorp", "specs": {"ram": "16GB", "cpu": "Intel i7"}}'::jsonb))",
81
82 R"(INSERT INTO products (name, description, price, tags, metadata) VALUES
83 ('Office Keyboard', 'Mechanical keyboard for office use', 79.99,
84 ARRAY['keyboard', 'office'],
85 '{"brand": "KeyMaster", "type": "mechanical"}'::jsonb))",
86
87 R"(INSERT INTO products (name, description, price, tags, metadata) VALUES
88 ('Gaming Mouse', 'RGB gaming mouse', 49.99,
89 ARRAY['gaming', 'mouse'],
90 '{"brand": "TechCorp", "features": ["RGB", "wireless"]}'::jsonb))"
91 };
92
93 for (const auto& query : insert_queries) {
94 auto insert_result = pg_manager->execute_query(query);
95 if (insert_result.is_ok()) {
96 std::cout << "Product inserted successfully" << std::endl;
97 } else {
98 std::cout << "Failed to insert product (may already exist)" << std::endl;
99 }
100 }
101
102 // 4. Advanced queries
103 std::cout << "\n4. Advanced PostgreSQL Queries:" << std::endl;
104
105 // Array operations
106 std::cout << "\nQuerying products with array operations:" << std::endl;
107 std::string array_query = "SELECT name, tags FROM products WHERE 'gaming' = ANY(tags)";
108 auto gaming_result = pg_manager->select_query(array_query);
109
110 if (gaming_result.is_ok() && !gaming_result.value().empty()) {
111 const auto& gaming_products = gaming_result.value();
112 std::cout << "Products with 'gaming' tag (" << gaming_products.size() << " rows):" << std::endl;
113 for (const auto& row : gaming_products) {
114 for (const auto& [key, value] : row) {
115 std::cout << " " << key << ": ";
116 std::visit([](const auto& v) { std::cout << v; }, value);
117 std::cout << " ";
118 }
119 std::cout << std::endl;
120 }
121 } else {
122 std::cout << "No gaming products found" << std::endl;
123 }
124
125 // JSONB operations
126 std::cout << "\nQuerying products with JSONB operations:" << std::endl;
127 std::string json_query = "SELECT name, metadata->>'brand' as brand FROM products WHERE metadata->>'brand' = 'TechCorp'";
128 auto techcorp_result = pg_manager->select_query(json_query);
129
130 if (techcorp_result.is_ok() && !techcorp_result.value().empty()) {
131 const auto& techcorp_products = techcorp_result.value();
132 std::cout << "TechCorp products (" << techcorp_products.size() << " rows):" << std::endl;
133 for (const auto& row : techcorp_products) {
134 for (const auto& [key, value] : row) {
135 std::cout << " " << key << ": ";
136 std::visit([](const auto& v) { std::cout << v; }, value);
137 std::cout << " ";
138 }
139 std::cout << std::endl;
140 }
141 } else {
142 std::cout << "No TechCorp products found" << std::endl;
143 }
144
145 // 5. Cleanup
146 std::cout << "\n5. Cleanup:" << std::endl;
147
148 // Optionally clean up test data
149 // std::string cleanup_sql = "DELETE FROM products WHERE name LIKE '%Gaming%' OR name LIKE '%Office%'";
150 // auto delete_result = pg_manager->execute_query(cleanup_sql);
151 // if (delete_result.is_ok()) {
152 // std::cout << "Cleaned up test records" << std::endl;
153 // }
154
155 // Shutdown
156 pg_manager->shutdown();
157 std::cout << "Disconnected from PostgreSQL database" << std::endl;
158
159 } else {
160 std::cout << "Failed to connect to PostgreSQL database" << std::endl;
161 std::cout << "Error: " << connect_result.error().message << std::endl;
162 std::cout << "Please ensure:" << std::endl;
163 std::cout << " - PostgreSQL server is running" << std::endl;
164 std::cout << " - Database 'testdb' exists" << std::endl;
165 std::cout << " - User 'testuser' has appropriate permissions" << std::endl;
166 std::cout << " - Connection parameters are correct" << std::endl;
167
168 std::cout << "\nTo test with a real database, update the connection string:" << std::endl;
169 std::cout << " host=your_host port=5432 dbname=your_db user=your_user password=your_pass" << std::endl;
170 }
171
172 } catch (const std::exception& e) {
173 std::cerr << "Error: " << e.what() << std::endl;
174 return 1;
175 }
176
177 std::cout << "\n=== PostgreSQL Advanced Features Example completed ===" << std::endl;
178 return 0;
179}
Abstract interface for database backends.
int main()
static connection_config from_string(const std::string &connect_string)
Construct connection_config from legacy connection string.