-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
227 lines (194 loc) · 5.76 KB
/
Copy pathConnection.cpp
File metadata and controls
227 lines (194 loc) · 5.76 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream>
#include <map>
#include <stdexcept>
#include "Connection.h"
using namespace std;
MysqlConnection::MysqlConnection()
{
this->conn = mysql_init(0);
this->connectedToDb = false;
}
MysqlConnection::MysqlConnection(const MysqlConnection& other)
{
this->conn = mysql_init(0);
this->connectedToDb = false;
}
void MysqlConnection::connectToDb(string url, string user, string password, string dbName, int port)
{
conn = mysql_real_connect(this->conn, url.c_str(), user.c_str(), password.c_str(), dbName.c_str(), port, NULL, 0);
if (!conn) {
cout << "Conection failed" << endl;
cout << "Chyba èíslo: " << mysql_errno(this->conn) << endl;
cout << "Popis chyby: " << mysql_error(this->conn) << endl;
}
else {
cout << "Connection successfull" << endl;
this->connectedToDb = true;
}
}
vector<string> MysqlConnection::getColNames(string db, string table) const
{
vector<string> colNames;
int qstate;
string query = "SELECT column_name as \'Column name\' FROM information_schema.COLUMNS WHERE table_schema = \'" + db + "\' AND table_name = \'" + table + "\' AND NOT COLUMN_KEY = \'PRI\';";
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES * res;
MYSQL_ROW row;
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res)) {
colNames.push_back(row[0]);
}
}
else {
cout << "Query failed" << endl;
throw runtime_error("Query failed");
}
return colNames;
}
vector<string> MysqlConnection::getColNamesWithoutTargetAndPrimaryKey(string db, string table, string target, string primaryKey) const
{
vector<string> colNames;
int qstate;
string query = "SELECT column_name as \'Column name\' FROM information_schema.COLUMNS WHERE table_schema = \'" + db + "\' AND table_name = \'" + table + "\' AND NOT COLUMN_KEY = \'PRI\';";
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES* res;
MYSQL_ROW row;
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res)) {
if((row[0] != target) && (row[0] != primaryKey))
{
colNames.push_back(row[0]);
}
}
}
else {
cout << "Query failed" << endl;
throw runtime_error("Query failed");
}
return colNames;
}
map<string, double> MysqlConnection::getRow(string dbName, string tableName, const int& rowIdx) const
{
map<string, double> rowMap;
int qstate;
string query = "SELECT * FROM " + tableName + " WHERE idx = " + to_string(rowIdx) + ";";
vector<string> colNames = this->getColNames(dbName, tableName);
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES* res;
MYSQL_ROW row;
res = mysql_store_result(conn);
row = mysql_fetch_row(res);
for (int i = 0; i < colNames.size(); i++) {
rowMap[colNames[i]] = stod(row[i+1]); // i+1 cause first columns is primary index
}
}
else {
cout << "Query failed" << endl;
cout << "Query: " << query << endl;
throw runtime_error("Query failed");
}
return rowMap;
}
map<string, double> MysqlConnection::getRow(string dbName, string tableName, const int& rowIdx, const vector<string> & colNames) const
{
map<string, double> rowMap;
int qstate;
string query = "SELECT ";
for (string colName : colNames) {
if (colName == colNames[colNames.size() - 1]) {
query += colName + " ";
}
else {
query += colName + ", ";
}
}
query += "FROM " + tableName + " WHERE idx = " + to_string(rowIdx) + "; ";
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES* res;
MYSQL_ROW row;
res = mysql_store_result(conn);
row = mysql_fetch_row(res);
for (int i = 0; i < colNames.size(); i++) {
rowMap[colNames[i]] = stod(row[i]); // i+1 cause first columns is primary index
}
}
else {
cout << "Query failed" << endl;
cout << "Query: " << query << endl;
throw runtime_error("Query failed");
}
return rowMap;
}
vector<pair<int, double>> MysqlConnection::getTargetVarValues(string targetVarName, string primaryKeyName, string tableName) const
{
vector<pair<int, double>> targetVarValues(0);
int qstate;
string query = "SELECT " + primaryKeyName + ", " + targetVarName + " FROM " + tableName;
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES* res;
MYSQL_ROW row;
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res)) {
targetVarValues.push_back(make_pair<int, double>(stoi(row[0]), stod(row[1])));
}
}
else {
cout << "Query failed" << endl;
throw runtime_error("Query failed");
}
return targetVarValues;
}
vector<pair<int, double>> MysqlConnection::getTargetVarValues(string targetVarName, string primaryKeyName, string tableName, const vector<int>& rowIdxs) const
{
vector<pair<int, double>> targetVarValues(0);
int qstate;
for (const auto& idx : rowIdxs) {
string query = "SELECT " + targetVarName + " FROM " + tableName + " WHERE " + primaryKeyName + "=" + to_string( idx);
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES* res;
MYSQL_ROW row;
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res)) {
targetVarValues.push_back(make_pair<int, double>(int(idx), stod(row[0])));
}
}
else {
cout << "Query failed" << endl;
throw runtime_error("Query failed");
}
}
return targetVarValues;
}
vector<int> MysqlConnection::getPrimaryKeys(string primaryKeyName, string tableName) const
{
vector<int> primaryKeys;
int qstate;
string query = "SELECT " + primaryKeyName + " FROM " + tableName;
qstate = mysql_query(this->conn, query.c_str());
if (!qstate) {
MYSQL_RES* res;
MYSQL_ROW row;
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res)) {
primaryKeys.push_back(stoi(row[0]));
}
}
else {
cout << "Query failed" << endl;
throw runtime_error("Query failed");
}
return primaryKeys;
}
bool MysqlConnection::isConnectedToDb() const
{
return this->connectedToDb;
}
Connection::Connection()
{
}