summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAotrix <[email protected]>2025-03-25 03:30:28 +0100
committerAotrix <[email protected]>2025-03-25 03:30:28 +0100
commite578e5b065f3eb7872a128723035dede6879cc07 (patch)
treeb681b789455997c938c210abdf8629ed8fe2f26b /src
parent562f5dee52a79f52ae78a2a72c3c0adde769cd9a (diff)
little bug in insert query
Diffstat (limited to 'src')
-rw-r--r--src/postgresHandler.cpp48
1 files changed, 42 insertions, 6 deletions
diff --git a/src/postgresHandler.cpp b/src/postgresHandler.cpp
index f00d337..b2fc102 100644
--- a/src/postgresHandler.cpp
+++ b/src/postgresHandler.cpp
@@ -24,7 +24,7 @@ void display_query_result(const pqxx::result &t_r) {
}
static const unsigned int
-get_type_of_regex_iterator(std::sregex_iterator t_sregex_it) {
+get_type_of_regex_iterator(const std::sregex_iterator &t_sregex_it) {
std::array<int, 4> types{INT_INDEX, STRING_INDEX, LIST_OF_INT_INDEX,
NULL_INDEX};
for (const int &type : types) {
@@ -35,6 +35,25 @@ get_type_of_regex_iterator(std::sregex_iterator t_sregex_it) {
throw std::invalid_argument("regex iterator input is invalid");
}
+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 NULL_INDEX:
+ return "NULL";
+ case LIST_OF_INT_INDEX: {
+ std::string json_copy = t_json_value;
+ *json_copy.begin() = '(';
+ *std::prev(json_copy.end()) = ')';
+ return json_copy;
+ }
+ default:
+ throw std::invalid_argument("type index is invalid: ");
+ }
+}
+
void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx) {
// TODO: to complete
std::regex key_regex(
@@ -43,10 +62,27 @@ void add_cards_to_database(const std::string &t_cards, pqxx::work &t_tx) {
auto words_begin =
std::sregex_iterator(t_cards.begin(), t_cards.end(), key_regex);
auto words_end = std::sregex_iterator();
- std::string insert_query; // TODO: complete: add base first "insert into..."
- for (auto it = words_begin; it != words_end; ++it) {
- const unsigned int type_index = get_type_of_regex_iterator(it);
- // TODO: format info depending on type_index then add it to insert_query
- std::cout << (*it)[type_index].str() << std::endl;
+ 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
+ for (auto it = words_begin; it != words_end; ++it) {
+ 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(
+ 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);
}
}