-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDroneMap.cpp
More file actions
79 lines (59 loc) · 1.71 KB
/
DroneMap.cpp
File metadata and controls
79 lines (59 loc) · 1.71 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
#include <iostream>
#include <string>
#include "DroneMap.h"
#pragma comment(lib, "libmysql.lib")
#define BUF_SIZE 1024
DroneMap* DroneMap::instance = NULL;
DroneMap::DroneMap()
{
connectDB();
if(!hasConnection) return;
}
void DroneMap::connectDB()
{
hasConnection = false;
dbConn = mysql_init(NULL);
if(dbConn == NULL) return;
mysql_real_connect(dbConn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 3306, (char *)NULL, 0);
if(dbConn != NULL) hasConnection = true;
}
void DroneMap::closeDB()
{
if(!hasConnection) return;
else if(dbConn != NULL) mysql_close(dbConn);
}
bool DroneMap::doQuery(std::string command)
{
if(!hasConnection) return false;
int queryStat = 0;
queryStat = mysql_query(dbConn, command.c_str());
if(queryStat != 0) return false;
return true;
}
const std::vector<DroneMapData> DroneMap::getData(int srcRowIdx, int srcColIdx, int dstRowIdx, int dstColIdx)
{
MYSQL_RES *res;
MYSQL_ROW sqlRow;
std::string queryStr = "select * from ";
queryStr += TBL_NAME;
/* Do ascending order for query */
if(dstRowIdx < srcRowIdx) std::swap(srcRowIdx, dstRowIdx);
if(dstColIdx < srcColIdx) std::swap(srcColIdx, dstColIdx);
/* Add where statement to query */
char queryWhere[BUF_SIZE] = { 0, };
sprintf(queryWhere, " where ROW >= %d and ROW <= %d and COL >= %d and COL <= %d", srcRowIdx, dstRowIdx, srcColIdx, dstColIdx);
queryStr += queryWhere;
/* Clear the vector for result */
if(!resultSet.empty()) resultSet.clear();
if(doQuery(queryStr))
{
res = mysql_store_result(dbConn);
while((sqlRow = mysql_fetch_row(res)) != NULL)
{
DroneMapData data(atof(sqlRow[2]), atof(sqlRow[3]), atof(sqlRow[4]), atof(sqlRow[5]));
resultSet.push_back(data);
}
mysql_free_result(res);
}
return resultSet;
}