summaryrefslogtreecommitdiff
path: root/src/postgresHandler.cpp
diff options
context:
space:
mode:
authorAotrix <[email protected]>2025-03-25 22:00:39 +0100
committerAotrix <[email protected]>2025-03-25 22:00:39 +0100
commit50a6cf2e28758f83eecc0e46f39e044be519a8e4 (patch)
treea46984017bfa4cdeca66e581f640bb1812a22ecf /src/postgresHandler.cpp
parent6ba1abb1458c4b4ffad3723f15ad52a504b8c264 (diff)
you can now add columns in cards table!
Diffstat (limited to 'src/postgresHandler.cpp')
-rw-r--r--src/postgresHandler.cpp32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp
index cdbea28..d80157e 100644
--- a/src/postgresHandler.cpp
+++ b/src/postgresHandler.cpp
@@ -1,4 +1,6 @@
#include "postgresHandler.h"
+#include <cctype>
+#include <cstddef>
#include <iostream>
#include <regex>
#include <stdexcept>
@@ -50,12 +52,9 @@ const std::string format_json_value_to_sql(const unsigned int &t_type_index,
case NULL_INDEX:
return "NULL";
case LIST_OF_INT_INDEX: {
- if (t_json_value.size() == 2) {
- return "NULL";
- }
std::string json_copy = t_json_value;
- *json_copy.begin() = '(';
- *std::prev(json_copy.end()) = ')';
+ json_copy.append("\'");
+ json_copy.insert(json_copy.begin(), '\'');
return json_copy;
}
default:
@@ -64,13 +63,23 @@ const std::string format_json_value_to_sql(const unsigned int &t_type_index,
}
std::string format_key(const std::string &key) {
- // FIXME: add _ before an uppercase char, and set all chars to lowercase
- // ones
- return key;
+ std::string key_copy(key);
+ std::stack<size_t> uppercase_indexes;
+ for (size_t i = 0; i < key.length(); i++) {
+ if (std::isupper(key[i])) {
+ uppercase_indexes.push(i);
+ }
+ }
+ while (!uppercase_indexes.empty()) {
+ size_t top_index = uppercase_indexes.top();
+ uppercase_indexes.pop();
+ key_copy[top_index] = std::tolower(key_copy[top_index]);
+ key_copy.insert(std::next(key_copy.begin(), top_index), '_');
+ }
+ return key_copy;
}
void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx) {
- // TODO: to complete
std::regex key_regex(
"\"([^\"?]+)\":(([0-9]+)|(\"[^\"?]*\")|(\\[[0-9,]*\\])|(null))");
@@ -78,7 +87,6 @@ void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx) {
std::sregex_iterator(t_cards.begin(), t_cards.end(), key_regex);
auto words_end = std::sregex_iterator();
if (words_begin != words_end) {
- // FIXME: add parent_id:int and copy_of_card_id:list_of_ints columns
std::string insert_columns("INSERT INTO cards (");
std::string values(" VALUES (");
for (auto it = words_begin; it != words_end; ++it) {
@@ -89,10 +97,10 @@ void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx) {
format_json_value_to_sql(type_index, (*it)[type_index].str()) +
",");
}
- // TODO: may be modified
*std::prev(insert_columns.end()) = ')';
*std::prev(values.end()) = ')';
- values.append(";");
+ values.append(" ON CONFLICT (id) DO NOTHING;");
+
execute_query(t_tx, insert_columns + values);
}
}