55// ---------------------------------------
66
77#include " databaseConnection.hpp"
8+ #include " base64.hpp"
89#include < pqxx/pqxx>
910
1011DatabaseConnection::DatabaseConnection (const std::string& endpoint, int port,
@@ -20,41 +21,53 @@ DatabaseConnection::DatabaseConnection(const std::string& endpoint, int port,
2021
2122}
2223
23- void DatabaseConnection::executeQuery (const std::string& query) const {
24- try {
25- // Establish connection
26- pqxx::connection conn (this ->connection_string );
27-
28- // Rest of your code remains the same
29- if (!conn.is_open ()) {
30- throw std::invalid_argument (" Failed to open database connection" );
31- }
32-
33- std::cout << " Connected to database successfully!" << std::endl;
34-
35- // Create a transaction
36- pqxx::work txn (conn);
37-
38- // Execute query
39- pqxx::result result = txn.exec (query);
40-
41- // Print results
42- for (const auto & row : result) {
43- for (const auto & field : row) {
44- std::cout << field.c_str () << " \t " ;
45- }
46- std::cout << std::endl;
47- }
48-
49- // Commit transaction
50- txn.commit ();
51-
52- } catch (const pqxx::broken_connection& e) {
53- std::cerr << " Connection error: " << e.what () << std::endl;
54- } catch (const pqxx::sql_error& e) {
55- std::cerr << " SQL error: " << e.what () << std::endl;
56- std::cerr << " Query was: " << e.query () << std::endl;
57- } catch (const pqxx::usage_error& e) {
58- std::cerr << " Usage error: " << e.what () << std::endl;
24+ std::vector<PriceData> DatabaseConnection::executeQuery (const std::string& query) const {
25+ std::vector<PriceData> results;
26+
27+ try {
28+ pqxx::connection conn (this ->connection_string );
29+
30+ if (!conn.is_open ()) {
31+ throw std::invalid_argument (" Failed to open database connection" );
32+ }
33+
34+ std::cout << " Connected to database successfully!" << std::endl;
35+
36+ pqxx::work txn (conn);
37+ pqxx::result result = txn.exec (query);
38+
39+ // Convert results to PriceData objects
40+ for (const auto & row : result) {
41+ double value1 = row[0 ].as <double >();
42+ double value2 = row[1 ].as <double >();
43+ std::string timestamp_str = row[2 ].as <std::string>();
44+
45+ auto timestamp = Utilities::parseTimestamp (timestamp_str);
46+
47+ results.emplace_back (value1, value2, timestamp);
48+ }
49+
50+ txn.commit ();
51+
52+ } catch (const std::exception& e) {
53+ std::cerr << " Error: " << e.what () << std::endl;
54+ }
55+
56+ return results;
57+ }
58+
59+ // Example usage function to demonstrate how to work with the results
60+ void DatabaseConnection::printResults (const std::vector<PriceData>& results) const {
61+ for (const auto & data : results) {
62+ // Convert timestamp back to string for display
63+ auto time_t = std::chrono::system_clock::to_time_t (data.timestamp );
64+ auto tm = *std::localtime (&time_t );
65+ std::stringstream ss;
66+ ss << std::put_time (&tm, " %Y-%m-%d %H:%M:%S" );
67+
68+ std::cout << std::fixed << std::setprecision (4 )
69+ << data.value1 << " \t "
70+ << data.value2 << " \t "
71+ << ss.str () << std::endl;
5972 }
6073}
0 commit comments