|
@@ -1,44 +1,144 @@
|
|
|
#include "DatabaseConnection.h"
|
|
|
-#include "structs.h"
|
|
|
+#include <iostream>
|
|
|
+#include <pqxx/pqxx>
|
|
|
#include <sstream>
|
|
|
+#include <utility>
|
|
|
+#include "structs.h"
|
|
|
|
|
|
-DatabaseConnection::DatabaseConnection() {}
|
|
|
+std::string fieldsAsString(std::vector<std::string> fields) {
|
|
|
+ std::string field_string{"("};
|
|
|
+ std::string delim = "";
|
|
|
|
|
|
-bool DatabaseConnection::isConnected() {
|
|
|
- return this->connected ? true : false;
|
|
|
+ for (const auto &field : fields) {
|
|
|
+ std::cout << field << std::endl;
|
|
|
+ field_string += delim + field;
|
|
|
+ delim += ",";
|
|
|
+ }
|
|
|
+ std::cout << field_string << std::endl;
|
|
|
+ field_string += ")";
|
|
|
+ return std::move(field_string);
|
|
|
}
|
|
|
|
|
|
+// https://en.wikipedia.org/wiki/Row-_and_column-major_order
|
|
|
+std::string valuesAsString(std::vector<std::string> values, size_t number_of_fields) {
|
|
|
+ std::string value_string{"VALUES ("};
|
|
|
+ int index = 1;
|
|
|
+ for (const auto &value : values) {
|
|
|
+ if (index % number_of_fields == 0) {
|
|
|
+ value_string += value + "), (";
|
|
|
+ } else {
|
|
|
+ value_string += value + ",";
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ value_string.erase(value_string.size() - 3);
|
|
|
+
|
|
|
+ return std::move(value_string);
|
|
|
+}
|
|
|
+
|
|
|
+std::string insertStatement(DatabaseQuery query) {
|
|
|
+ return std::string{"INSERT INTO " + query.table + fieldsAsString(query.fields) + valuesAsString(query.values, query.fields.size())};
|
|
|
+}
|
|
|
+
|
|
|
+std::string selectStatement(DatabaseQuery query) {
|
|
|
+ return std::string{"SELECT " + fieldsAsString(query.fields) + " FROM " + query.table};
|
|
|
+}
|
|
|
+
|
|
|
+// TODO: Update query
|
|
|
+// TODO: Filtering
|
|
|
+
|
|
|
+DatabaseConnection::DatabaseConnection() {}
|
|
|
+
|
|
|
bool DatabaseConnection::setConfig(DatabaseConfiguration config) {
|
|
|
m_config = config;
|
|
|
+ m_db_name = config.credentials.name;
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+pqxx::result DatabaseConnection::performInsert (DatabaseQuery query) {
|
|
|
+ pqxx::connection connection(getConnectionString().c_str());
|
|
|
+ pqxx::work worker(connection);
|
|
|
+ pqxx::result pqxx_result = worker.exec(insertStatement(query));
|
|
|
+ worker.commit();
|
|
|
+
|
|
|
+ return pqxx_result;
|
|
|
+}
|
|
|
+
|
|
|
// std::string DatabaseConnection::getDbName(){return }
|
|
|
|
|
|
+std::string DatabaseConnection::getConnectionString() {
|
|
|
+ std::string connectionString{};
|
|
|
+ connectionString += "dbname = ";
|
|
|
+ connectionString += m_config.credentials.name;
|
|
|
+ connectionString += " user = ";
|
|
|
+ connectionString += m_config.credentials.user;
|
|
|
+ connectionString += " password = ";
|
|
|
+ connectionString += m_config.credentials.password;
|
|
|
+ connectionString += " hostaddr = ";
|
|
|
+ connectionString += m_config.address;
|
|
|
+ connectionString += " port = ";
|
|
|
+ connectionString += m_config.port;
|
|
|
+ std::cout << connectionString << std::endl;
|
|
|
+ return connectionString;
|
|
|
+}
|
|
|
+
|
|
|
QueryResult DatabaseConnection::query(DatabaseQuery query) {
|
|
|
switch (query.type) {
|
|
|
- case QueryType::INSERT: {
|
|
|
- std::stringstream query_stream{};
|
|
|
+ case QueryType::SELECT: {
|
|
|
+ std::stringstream query_stream{};
|
|
|
+ query_stream << "SELECT * FROM " << query.table;
|
|
|
+ pqxx::connection connection(getConnectionString().c_str());
|
|
|
+ pqxx::work worker(connection);
|
|
|
+ pqxx::result pqxx_result = worker.exec(query_stream.str());
|
|
|
|
|
|
- query_stream << "INSERT INTO" << query.table << " VALUES (\"test\")";
|
|
|
- QueryResult result{};
|
|
|
- break;
|
|
|
- }
|
|
|
- case QueryType::DELETE:
|
|
|
- std::stringstream query_stream{};
|
|
|
+ QueryResult result{};
|
|
|
+ result.table = query.table;
|
|
|
+
|
|
|
+ for (auto row : pqxx_result) {
|
|
|
+ auto row_c_str = row[0].c_str();
|
|
|
+ if (row_c_str != nullptr) {
|
|
|
+ std::string row_string(row_c_str);
|
|
|
+ auto pair = std::make_pair("table", row_string);
|
|
|
+ result.values.push_back(pair);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ case QueryType::INSERT: {
|
|
|
+ auto pqxx_result = performInsert(query);
|
|
|
+ QueryResult result{};
|
|
|
+
|
|
|
+ for (auto row : pqxx_result) {
|
|
|
+ auto row_c_str = row[0].c_str();
|
|
|
+ std::cout << "Insert row result" << row_c_str << std::endl;
|
|
|
+ if (row_c_str != nullptr) {
|
|
|
+ std::string row_string(row_c_str);
|
|
|
+ auto pair = std::make_pair("table", row_string);
|
|
|
+ result.values.push_back(pair);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ case QueryType::DELETE: {
|
|
|
+ std::stringstream query_stream{};
|
|
|
|
|
|
- query_stream << "INSERT INTO" << query.table << " VALUES (\"test\")";
|
|
|
- QueryResult result{};
|
|
|
- return result;
|
|
|
+ query_stream << " " << query.table << " VALUES (\"test\")";
|
|
|
+ QueryResult result{};
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
+ return QueryResult{};
|
|
|
}
|
|
|
|
|
|
-bool DatabaseConnection::connectToDatabase() {
|
|
|
- pqxx::connection C("dbname = kiq user = kiqadmin password = kiqadmin \
|
|
|
- hostaddr = 127.0.0.1 port = 5432");
|
|
|
- m_connection = std::move(&C);
|
|
|
+pqxx::connection DatabaseConnection::getConnection() {
|
|
|
+ std::string connectionString{("dbname = " + m_config.credentials.name +
|
|
|
+ " user = " + m_config.credentials.user +
|
|
|
+ " password = " + m_config.credentials.password +
|
|
|
+ " hostaddr = " + m_config.address + " port " +
|
|
|
+ m_config.port)};
|
|
|
+ return pqxx::connection(connectionString);
|
|
|
}
|
|
|
|
|
|
-std::string DatabaseConnection::getDbName() {
|
|
|
- return std::to_string(*m_connection->dbname());
|
|
|
-}
|
|
|
+std::string DatabaseConnection::getDbName() { return m_db_name; }
|