32 std::cout <<
"=== Database System - PostgreSQL Advanced Features Example ===" << std::endl;
36 auto pg_manager = std::make_unique<postgres_manager>();
38 std::cout <<
"\n1. Database Connection:" << std::endl;
41 std::string connection_string =
"host=localhost port=5432 dbname=testdb user=testuser password=testpass";
43 std::cout <<
"Attempting to connect to PostgreSQL..." << std::endl;
45 auto connect_result = pg_manager->initialize(config);
47 if (connect_result.is_ok()) {
48 std::cout <<
"Successfully connected to PostgreSQL database" << std::endl;
51 std::cout <<
"\n2. Creating Advanced Table:" << std::endl;
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,
61 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
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;
70 std::cout <<
"Failed to create products table: " << table_result.error().message << std::endl;
74 std::cout <<
"\n3. Inserting Sample Data:" << std::endl;
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))",
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))",
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))"
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;
98 std::cout <<
"Failed to insert product (may already exist)" << std::endl;
103 std::cout <<
"\n4. Advanced PostgreSQL Queries:" << std::endl;
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);
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);
119 std::cout << std::endl;
122 std::cout <<
"No gaming products found" << std::endl;
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);
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);
139 std::cout << std::endl;
142 std::cout <<
"No TechCorp products found" << std::endl;
146 std::cout <<
"\n5. Cleanup:" << std::endl;
156 pg_manager->shutdown();
157 std::cout <<
"Disconnected from PostgreSQL database" << std::endl;
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;
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;