-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStore.cpp
More file actions
116 lines (95 loc) · 3.44 KB
/
DataStore.cpp
File metadata and controls
116 lines (95 loc) · 3.44 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
#include "DataStore.h"
namespace PaulNovack {
DataStore::DataStore(const AppConfig& config) {
connectionPool_ = new MySQLConnectionPool(config.DB_HOST,
config.DB_USERNAME,
config.DB_PASSWORD,
config.DB_DATABASE_NAME,
config.DB_POOL_SIZE,
config.DB_HEARTBEAT_INTERVAL);
}
DataStore::DataStore(const DataStore& orig) {
}
DataStore::~DataStore() {
}
map<int, WayPoint> DataStore::getWayPoints() {
map<int, WayPoint> WayPointsMap;
// Get a connection from the connection pool
sql::Connection* conn = connectionPool_->getConnection();
try {
// Create a SQL statement
sql::Statement* stmt = conn->createStatement();
string query = "SELECT * FROM waypoints";
// Execute the query
sql::ResultSet* resultSet = stmt->executeQuery(query);
while (resultSet->next()) {
WayPoint wp;
wp.id = resultSet->getInt("id");
wp.name = resultSet->getString("name");
wp.description = resultSet->getString("description");
wp.latitude = resultSet->getDouble("latitude");
wp.longitude = resultSet->getDouble("longitude");
wp.depth = resultSet->getDouble("depth");
WayPointsMap[wp.id] = wp;
}
// Clean up
delete resultSet;
delete stmt;
} catch (const sql::SQLException& e) {
cerr << "SQL error: " << e.what() << endl;
}
// Release the connection back to the connection pool
connectionPool_->releaseConnection(conn);
return WayPointsMap;
}
WayPoint DataStore::updateWayPoint(WayPoint wayPoint) {
sql::Connection* conn = connectionPool_->getConnection();
sql::PreparedStatement* pstmt;
pstmt = conn->prepareStatement("update waypoints SET name = ?, "
"description = ?, latitude = ?, longitude = ?, depth = ? "
"WHERE id = ?");
pstmt->setString(1, wayPoint.name);
pstmt->setString(2, wayPoint.description);
pstmt->setDouble(3, wayPoint.latitude);
pstmt->setDouble(4, wayPoint.longitude);
pstmt->setDouble(5, wayPoint.depth);
pstmt->setInt(6, wayPoint.id);
pstmt->executeUpdate();
connectionPool_->releaseConnection(conn);
return wayPoint;
}
int DataStore::insertWayPoint(WayPoint wayPoint) {
sql::Connection* conn = connectionPool_->getConnection();
sql::PreparedStatement* pstmt;
sql::ResultSet *res;
pstmt = conn->prepareStatement("insert into waypoints (name, "
" description, latitude, longitude, depth) values "
"(?,?,?,?,?) ");
pstmt->setString(1, wayPoint.name);
pstmt->setString(2, wayPoint.description);
pstmt->setDouble(3, wayPoint.latitude);
pstmt->setDouble(4, wayPoint.longitude);
pstmt->setDouble(5, wayPoint.depth);
if (!(pstmt->executeUpdate())) {
cout << "Error executing insert" << endl;
}
pstmt = conn->prepareStatement("select last_insert_id();");
res = pstmt->executeQuery();
// Get the insert ID
int insertID = 0;
if (res->next()) {
insertID = res->getInt(1);
}
connectionPool_->releaseConnection(conn);
return insertID;
}
bool DataStore::deleteWayPoint(WayPoint wayPoint) {
sql::Connection* conn = connectionPool_->getConnection();
sql::PreparedStatement* pstmt;
pstmt = conn->prepareStatement("delete waypoints where id = ? ");
pstmt->setInt(1, wayPoint.id);
pstmt->executeUpdate();
connectionPool_->releaseConnection(conn);
return true;
}
}