summaryrefslogtreecommitdiff
path: root/src/postgresHandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/postgresHandler.cpp')
-rw-r--r--src/postgresHandler.cpp56
1 files changed, 33 insertions, 23 deletions
diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp
index b2fc102..cdbea28 100644
--- a/src/postgresHandler.cpp
+++ b/src/postgresHandler.cpp
@@ -3,10 +3,11 @@
#include <regex>
#include <stdexcept>
-#define INT_INDEX 2
-#define STRING_INDEX 3
-#define LIST_OF_INT_INDEX 4
-#define NULL_INDEX 5
+#define KEY_INDEX 1
+#define INT_INDEX 3
+#define STRING_INDEX 4
+#define LIST_OF_INT_INDEX 5
+#define NULL_INDEX 6
const pqxx::result execute_query(pqxx::work &t_transaction,
std::string_view t_query) {
@@ -39,11 +40,19 @@ const std::string format_json_value_to_sql(const unsigned int &t_type_index,
const std::string &t_json_value) {
switch (t_type_index) {
case INT_INDEX:
- case STRING_INDEX:
return t_json_value;
+ case STRING_INDEX: {
+ std::string json_copy = t_json_value;
+ *json_copy.begin() = '\'';
+ *std::prev(json_copy.end()) = '\'';
+ return json_copy;
+ }
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()) = ')';
@@ -54,35 +63,36 @@ 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;
+}
+
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))");
+ "\"([^\"?]+)\":(([0-9]+)|(\"[^\"?]*\")|(\\[[0-9,]*\\])|(null))");
auto words_begin =
std::sregex_iterator(t_cards.begin(), t_cards.end(), key_regex);
auto words_end = std::sregex_iterator();
if (words_begin != words_end) {
- std::string insert_query("INSERT INTO cards (id, collectible, slug, "
- "class_id, multi_class_ids, "
- "card_type_id, card_set_id, rarity_id, "
- "artist_name, health, attack, "
- "mana_cost, name, text, image, image_gold, "
- "flavor_text, crop_image, "
- "keyword_ids, is_zilliax_functional_module, "
- "is_zilliax_cosmetic_module) VALUES (");
- // std::prev(words_end) before
+ // 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) {
+ insert_columns.append(format_key((*it)[KEY_INDEX].str()) +
+ ","); // key
const unsigned int type_index = get_type_of_regex_iterator(it);
- // TODO: format info depending on type_index then add it to
- // insert_query
- insert_query.append(
+ values.append(
format_json_value_to_sql(type_index, (*it)[type_index].str()) +
- ", ");
+ ",");
}
- *std::prev(insert_query.end()) = ';';
- *std::prev(insert_query.end(), 2) = ')';
- std::cout << insert_query << std::endl;
- execute_query(t_tx, insert_query);
+ // TODO: may be modified
+ *std::prev(insert_columns.end()) = ')';
+ *std::prev(values.end()) = ')';
+ values.append(";");
+ execute_query(t_tx, insert_columns + values);
}
}