blob: f00d33757a89a8ac40d918576add3ce4d9e8d697 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include "postgresHandler.h"
#include <iostream>
#include <regex>
#include <stdexcept>
#define INT_INDEX 2
#define STRING_INDEX 3
#define LIST_OF_INT_INDEX 4
#define NULL_INDEX 5
const pqxx::result execute_query(pqxx::work &t_transaction,
std::string_view t_query) {
return t_transaction.exec(t_query);
}
void display_query_result(const pqxx::result &t_r) {
const int nb_columns = t_r.columns();
for (const auto &row : t_r) {
for (int i = 0; i < nb_columns; i++) {
std::cout << row[i].c_str() << " | ";
}
std::cout << std::endl;
}
}
static const unsigned int
get_type_of_regex_iterator(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) {
if ((*t_sregex_it)[type] != "") {
return type;
}
}
throw std::invalid_argument("regex iterator input is invalid");
}
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))");
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;
}
}
|