|
| 1 | +// |
| 2 | +// DbRepository.cpp |
| 3 | +// PostgreSQL |
| 4 | +// |
| 5 | +// Created by Kyle Parker on 10/3/24. |
| 6 | +// |
| 7 | + |
| 8 | +/// Attention students: Nothing to do here! |
| 9 | + |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | +#include "DbRepository.hpp" |
| 13 | + |
| 14 | +/// Construct a controller with a given filled out URL |
| 15 | +/// @Param fullURL The complete server URL for connecting to |
| 16 | +DbRepository::DbRepository(const string& fullURL) { |
| 17 | + this->url = fullURL; |
| 18 | +} |
| 19 | + |
| 20 | +/// Cleanup before destroying obejct |
| 21 | +DbRepository::~DbRepository() { |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +/// Select all rows from a given table |
| 26 | +/// @Param table The table to read everything from |
| 27 | +pqxx::result DbRepository::selectAllFrom(const string& table) const { |
| 28 | + pqxx::result results = pqxx::result(); |
| 29 | + |
| 30 | + try { |
| 31 | + // Connect to the database |
| 32 | + pqxx::connection conn(url); |
| 33 | + |
| 34 | + // Check if the connection is open |
| 35 | +#ifdef DEBUG |
| 36 | + if (conn.is_open()) { |
| 37 | + std::cout << "Opened database successfully: " << conn.dbname() << std::endl; |
| 38 | + } else { |
| 39 | + std::cout << "Can't open database" << std::endl; |
| 40 | + return results; |
| 41 | + } |
| 42 | +#else |
| 43 | + if (!conn.is_open()) { |
| 44 | + return results; |
| 45 | + } |
| 46 | +#endif |
| 47 | + |
| 48 | + // Create a non-transactional object |
| 49 | + pqxx::work trans(conn); |
| 50 | + |
| 51 | + // Execute a SQL query |
| 52 | + results = trans.exec("SELECT * FROM " + table + ";"); |
| 53 | + |
| 54 | + // Print the result |
| 55 | + return results; |
| 56 | + |
| 57 | + } catch (const pqxx::sql_error &e) { |
| 58 | + std::cerr << "SQL error: " << e.what() << std::endl; |
| 59 | + std::cerr << "Query: " << e.query() << std::endl; |
| 60 | + } catch (const std::exception &e) { |
| 61 | + std::cerr << e.what() << std::endl; |
| 62 | + } |
| 63 | + return results; |
| 64 | +} |
| 65 | + |
| 66 | +/// Select all fields from a given table |
| 67 | +/// @Param fields The fields to select in |
| 68 | +/// @Param table The table to read from |
| 69 | +/// @Param where Conditionals |
| 70 | +pqxx::result DbRepository::selectWhere(const string& fields, const string& table, const string& where) const { |
| 71 | + pqxx::result results; |
| 72 | + |
| 73 | + try { |
| 74 | + |
| 75 | + // Connect to the database |
| 76 | + pqxx::connection conn(url); |
| 77 | + |
| 78 | + // Check if the connection is open |
| 79 | +#ifdef DEBUG |
| 80 | + if (conn.is_open()) { |
| 81 | + std::cout << "Opened database successfully: " << conn.dbname() << std::endl; |
| 82 | + } else { |
| 83 | + std::cout << "Can't open database" << std::endl; |
| 84 | + return results; |
| 85 | + } |
| 86 | +#else |
| 87 | + if (!conn.is_open()) { |
| 88 | + return results; |
| 89 | + } |
| 90 | +#endif |
| 91 | + |
| 92 | + // Create a transactional object |
| 93 | + pqxx::work trans(conn); |
| 94 | + |
| 95 | + // Execute a SQL query |
| 96 | + results = trans.exec("SELECT " + fields + " FROM " + table + " WHERE " + where + ";"); |
| 97 | + |
| 98 | + } catch (const pqxx::sql_error &e) { |
| 99 | + std::cerr << "SQL error: " << e.what() << std::endl; |
| 100 | + std::cerr << "Query: " << e.query() << std::endl; |
| 101 | + } catch (const std::exception &e) { |
| 102 | + std::cerr << "Exception: " << e.what() << std::endl; |
| 103 | + } |
| 104 | + return results; |
| 105 | +} |
0 commit comments