-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgres.cpp
203 lines (167 loc) · 5.08 KB
/
postgres.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <chrono>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <pqxx/pqxx>
#include <pqxx/stream_to>
#include <pqxx/tablereader>
#include <pqxx/transaction>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
// #include "test_helpers.hxx"
// #include "test_types.hxx"
#include <experimental/string_view>
// #include <json.hpp>
#if defined PQXX_HAVE_OPTIONAL
#include <optional>
#elif defined PQXX_HAVE_EXP_OPTIONAL && !defined(PQXX_HIDE_EXP_OPTIONAL)
#include <experimental/optional>
#endif
using namespace std;
template <typename T>
struct ReadFromFileStream {
std::ifstream streem;
ReadFromFileStream(const char * str): {
streem{str, std::ifstream::in}
}
operator T <<(T v)
{
std::vector<int> vec(3);
auto val = vec.begin();
while (val != vec.end()) {
streem >> *val;
val++;
}
v << std::make_tuple(vec[0], vec[1], vec[2]);
return v;
}
constexpr operator T&() noexcept { return streem; }
constexpr operator const T&() const noexcept { return streem; }
};
// makeReadFromFileStream ifs(ReadFromFileStream("data.txt"));
// while (ifs)
// {
// //inserter takes in a tuple << binary operator
// inserter << ifs;
// };
class Postgres {
public:
pqxx::connection conn;
Postgres(std::string connect) : conn{connect}{};
template <typename A>
void create_table(A query) {
try {
pqxx::work transaction2{conn};
transaction2.exec(query);
transaction2.commit();
} catch (const std::exception& e) {
cout << e.what() << std::endl;
}
}
template <typename T, typename S>
void stream_to_table(T table_name, S ifs) {
pqxx::work transaction{conn};
pqxx::stream_to inserter{transaction, table_name};
// while (ifs) {
inserter << ifs.streem;
// }
inserter.complete();
transaction.commit();
};
// bool intilize_posgress()
// {
// try
// {
// pqxx::work worker(conn);
// conn.prepare("check_db",
// "SELECT EXISTS ( SELECT 1 FROM "
// "information_schema.tables WHERE table_name = $1 )");
// pqxx::result r = worker.exec_prepared("check_db", "company7");
// std::string r_value = r[0][0].c_str();
// if (r_value == "t")
// {
// conn.prepare("crate_company_table",
// " CREATE TABLE IF NOT EXISTS COMPANY( ID INT "
// "PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT "
// "NOT NULL,ADDRESS CHAR(50), SALARY REAL);");
// pqxx::result a = worker.exec_prepared("crate_company_table");
// std::vector<std::vector<std::string>> insert{
// {"2", "Allen", "25", "Texas", "15000.00"},
// {"3", "Teddy", "23", "Norway", "20000.00"},
// {"4", "Mark", "25", "Rich-Mond", "65000.00"}};
// conn.prepare("Insert_Into_User_Table",
// "INSERT INTO COMPANY(ID, NAME, AGE, ADDRESS, SALARY) "
// "VALUES($1, $2, $3, $4, $5);");
// auto user = insert.begin();
// while (user != insert.end())
// {
// int val1 = std::stoi(user[0][0]);
// int val2 = std::stoi(user[0][2]);
// int val3 = std::stof(user[0][4]);
// worker.exec_prepared("Insert_Into_User_Table", val1, user[0][1], val2,
// user[0][3], val3);
// user++;
// };
// }
// worker.commit();
// std::cout << "success" << std::endl;
// }
// catch (const std::exception &e)
// {
// std::cerr << "error: " << e.what() << std::endl;
// return 1;
// }
// return 0;
// };
};
template <typename... Ts>
class simulation_wrapper : public simulation_base
{
tuple<shared_ptr<Ts>... > stored_inputs{new Ts...};
public:
// MAGIC STARTS HERE
int execute_simulation_wrapped() { return execute_simulation_wrapped(std::make_index_sequence<sizeof...(Ts)>{}); }
private:
template <std::size_t... Is>
int execute_simulation_wrapped(std::index_sequence<Is...>) { return simulation(*std::get<Is>(stored_inputs)...); }
// MAGIC ENDS HERE
protected:
virtual int simulation(const Ts&...){return 0;};
};
int main() {
std::string conn= "dbname = postgres user = hoss password = 789456123 hostaddr = 127.0.0.1 port = 5432";
Postgres psql{conn};
psql.create_table(
"CREATE TABLE stream_to_test ("
"number0 INT NOT NULL,"
"number2 INT NULL,"
"number3 INT NULL"
")");
psql.stream_to_table("stream_to_test", "data.txt");
std::ifstream filein("data.txt");
for (std::string line; std::getline(filein, line);) {
std::stringstream ss;
ss << line;
std::string temp;
int found;
while (!ss.eof()) {
ss >> temp;
if (std::stringstream(temp) >> found) {
std::cout << found << " ";
temp = "";
}
// std::cout << line << std::endl;
}
}
// psql.stream_to_table("stream_to_test", ReadFromFileStream("data.txt"));
const char * file = "dtat.txt";
auto ifs = ReadFromFileStream(file);
// Postgres::create_table(conn, );
// Postgres::test_nonoptionals(conn);
// Postgres::intilize_posgress(conn);
}