-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLConnector.cpp
286 lines (249 loc) · 7 KB
/
SQLConnector.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "SQLConnector.h"
#include "util.h"
#include "cppconn/driver.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <time.h>
#define SQLLOG std::setprecision(3) << std::fixed << dtime() << " SQLB:"
SQLConnector::SQLConnector(ConfigFile cf)
{
m_config = cf;
m_pDriver = get_driver_instance();
std::string hostName = m_config.getParam("SQL_PARAMETERS",
"HOSTNAME",
"localhost");
std::string userName = m_config.getParam("SQL_PARAMETERS",
"USERNAME",
"xbtherm");
std::string userPass = m_config.getParam("SQL_PARAMETERS",
"PASSWORD",
"therm");
std::string database = m_config.getParam("SQL_PARAMETERS",
"DATABASE",
"therm");
m_pConnection = m_pDriver->connect(hostName, userName, userPass);
m_pConnection->setSchema(database);
}
SQLConnector::~SQLConnector()
{
delete m_pConnection;
}
int SQLConnector::putVals(long int time,
const std::vector<std::pair<std::string, double>>& vals)
{
// If there are no values, bomb immediately
if (vals.size() == 0)
{
std::cout << SQLLOG << " putVals given no data, bailing out" << std::endl;
return -1;
}
// First check whether we have a table
std::string tableName = m_config.getParam("SQL_PARAMETERS",
"TABLE_NAME",
"temperatures");
std::vector<std::string> colNames;
for (auto& i : vals)
colNames.push_back(i.first);
if (!doesTableExist(tableName))
{
if (createTable(tableName, colNames) != 0)
{
std::cout << SQLLOG << " createTable failed, bailing out" << std::endl;
return -1;
}
}
if (!doTableColumnsWork(tableName, colNames))
{
if (makeTableColumnsWork(tableName, colNames) != 0)
{
std::cout << SQLLOG << " makeTableColumnsWork failed, bailing out"
<< std::endl;
return -1;
}
}
if (addRow(tableName, time, vals) != 0)
{
std::cout << SQLLOG << " addRow failed." << std::endl;
return -1;
}
return 0;
}
bool SQLConnector::doesTableExist(std::string tableName)
{
std::stringstream ss;
ss << "show tables;";
std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
std::shared_ptr<sql::ResultSet> pResults(pStatement->executeQuery(ss.str()));
pResults->beforeFirst();
while (pResults->next())
{
if (pResults->getString(1) == tableName)
return true;
}
return false;
}
int SQLConnector::createTable(std::string tableName,
const std::vector<std::string>& colNames)
{
if (colNames.size() == 0)
{
std::cout << SQLLOG << " createTable provided empty colNames"
<< std::endl;
return -1;
}
std::vector<std::string> colNamesLocal(colNames);
std::stringstream ss;
ss << "create table " << tableName << "\n"
<< " (id INT AUTO_INCREMENT PRIMARY KEY,\n"
<< " time TIMESTAMP,\n";
std::string last = colNamesLocal.back();
colNamesLocal.pop_back();
for (auto& colName : colNamesLocal)
{
ss << " " << colName << " FLOAT,\n";
}
ss << " " << last << " FLOAT );";
std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
bool result = pStatement->execute(ss.str());
if (!result)
{
std::cout << SQLLOG << " created table with:" << std::endl;
std::cout << ss.str() << std::endl;
}
else
{
std::cout << SQLLOG << " error creating table with:" << std::endl;
std::cout << ss.str() << std::endl;
return -1;
}
ss.str(std::string());
ss << "CREATE INDEX " << tableName << "_time_index ON "
<< tableName << " (time);";
result = pStatement->execute(ss.str());
if (!result)
{
std::cout << SQLLOG << " created index with:" << std::endl;
std::cout << ss.str() << std::endl;
}
else
{
std::cout << SQLLOG << " error creating index with:" << std::endl;
std::cout << ss.str() << std::endl;
return -1;
}
return 0;
}
bool SQLConnector::doTableColumnsWork(std::string tableName,
const std::vector<std::string>& colNames)
{
std::stringstream ss;
ss << "describe " << tableName << ";";
std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
std::shared_ptr<sql::ResultSet> pResults(pStatement->executeQuery(ss.str()));
std::map<std::string, int> colsFound;
for (auto& colName : colNames)
colsFound[colName] = 0;
pResults->beforeFirst();
while (pResults->next())
{
for (auto& colName : colNames)
{
if (pResults->getString("Field") == colName)
colsFound[colName] = 1;
}
}
for (auto& colPair : colsFound)
{
if (colPair.second == 0)
return false;
}
return true;
}
int SQLConnector::makeTableColumnsWork(std::string tableName,
const std::vector<std::string>& colNames)
{
std::stringstream ss;
ss << "describe " << tableName << ";";
std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
std::shared_ptr<sql::ResultSet> pResults(pStatement->executeQuery(ss.str()));
std::map<std::string, int> colsFound;
for (auto& colName : colNames)
colsFound[colName] = 0;
pResults->beforeFirst();
while (pResults->next())
{
for (auto& colName : colNames)
{
if (pResults->getString("Field") == colName)
colsFound[colName] = 1;
}
}
for (auto& colPair : colsFound)
{
if (colPair.second == 0)
{
// Add the column
ss.str(std::string());
ss << "ALTER TABLE " << tableName << " ADD "
<< colPair.first << " FLOAT;";
bool result = pStatement->execute(ss.str());
if (!result)
{
std::cout << SQLLOG << " added column with:" << std::endl;
std::cout << ss.str() << std::endl;
}
else
{
std::cout << SQLLOG << " error adding column with:" << std::endl;
std::cout << ss.str() << std::endl;
return -1;
}
}
}
return 0;
}
int SQLConnector::addRow(std::string tableName,
long int time,
const std::vector<std::pair<std::string, double>>& vals)
{
struct tm* t = localtime(&time);
char buf[50];
snprintf(buf, 50, "%04d-%02d-%02d %02d:%02d:%02d",
t->tm_year+1900, t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec);
std::string timestring(buf);
std::vector<std::pair<std::string, double>> valsCopy(vals);
std::pair<std::string, double> lastVal = valsCopy.back();
valsCopy.pop_back();
std::stringstream ss;
ss << "INSERT INTO " << tableName << " ( time, ";
for (auto& val : valsCopy)
{
ss << val.first << ", ";
}
ss << lastVal.first << " ) ";
ss << "VALUES ( '" << timestring << "', ";
for (auto& val : valsCopy)
{
ss << val.second << ", ";
}
ss << lastVal.second << ");";
std::shared_ptr<sql::Statement> pStatement(m_pConnection->createStatement());
bool result = pStatement->execute(ss.str());
if (!result)
{
std::cout << SQLLOG << " added row with:" << std::endl;
std::cout << ss.str() << std::endl;
}
else
{
std::cout << SQLLOG << " error adding row with:" << std::endl;
std::cout << ss.str() << std::endl;
return -1;
}
return 0;
}