Mercurial Hosting > traffic-intelligence
comparison trajectorymanagement/src/TrajectoryDBAccessBlob.h @ 1159:e1e7acef8eab
moved trajectory management library into Traffic Intelligence
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 22 Feb 2021 22:09:35 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1158:7eb972942f22 | 1159:e1e7acef8eab |
---|---|
1 #ifndef TRAJECTORYDBACCESSBLOB_H_ | |
2 #define TRAJECTORYDBACCESSBLOB_H_ | |
3 | |
4 #include "TrajectoryDBAccess.h" | |
5 | |
6 /** | |
7 * TrajectoryDBAccessBlob class. | |
8 * | |
9 * The TrajectoryDBAccessBlob class allows to perform basic operations on a database representing trajectory as a Blob. | |
10 */ | |
11 template<typename T> | |
12 class TrajectoryDBAccessBlob: public TrajectoryDBAccess<T> | |
13 { | |
14 public: | |
15 /** | |
16 * Create a table. | |
17 * | |
18 * @return information whether the operation was successful | |
19 */ | |
20 bool createTable(const std::string& tableName = "trajectories") | |
21 { | |
22 if (!TrajectoryDBAccess<T>::db->isConnected()) | |
23 { | |
24 return false; | |
25 } | |
26 | |
27 std::string statementString = "create table "+tableName+" ( trajectory_id INTEGER, trajectory BLOB );"; | |
28 const char *statement = statementString.c_str(); | |
29 | |
30 bool success = TrajectoryDBAccess<T>::db->executeStatement(statement); | |
31 return success; | |
32 } | |
33 | |
34 /** | |
35 * Read trajectories from a database. | |
36 * | |
37 * @param[out] trajectories trajectories | |
38 * @param[in] limit maximum number of trajectories, which will be read | |
39 * @param[in] offset offset | |
40 * @return information whether the operation was successful | |
41 */ | |
42 bool read(std::vector<Trajectory<T>*>* &trajectories, int limit = -1, int offset = -1, const std::string& tableName = "trajectories") | |
43 { | |
44 if (!TrajectoryDBAccess<T>::db->isConnected()) | |
45 { | |
46 return false; | |
47 } | |
48 | |
49 std::string statementString = "select * from "+tableName; | |
50 if (limit > 0) | |
51 { | |
52 statementString += " limit " + TrajectoryDBAccess<T>::toString(limit); | |
53 } | |
54 if (offset > 0) | |
55 { | |
56 statementString += " offset " + TrajectoryDBAccess<T>::toString(offset); | |
57 } | |
58 const char *statement = statementString.c_str(); | |
59 | |
60 std::vector<std::vector<std::string> > result; | |
61 bool success = TrajectoryDBAccess<T>::db->executeStatementGetMatrix(statement, result); | |
62 if (success) | |
63 { | |
64 if (trajectories == 0) | |
65 { | |
66 trajectories = new std::vector<Trajectory<T>*> (); | |
67 } | |
68 | |
69 for (unsigned int i = 0; i < result.size(); ++i) | |
70 { | |
71 std::string s = ""; | |
72 for (unsigned int j = 0; j < result[i].size(); ++j) | |
73 { | |
74 s += result[i][j]; | |
75 | |
76 if (j == 0) | |
77 { | |
78 s += " "; | |
79 } | |
80 } | |
81 | |
82 std::istringstream is(s); | |
83 Trajectory<T> *trajectory = new Trajectory<T> (); | |
84 is >> *trajectory; | |
85 trajectories->push_back(trajectory); | |
86 } | |
87 } | |
88 | |
89 return success; | |
90 } | |
91 | |
92 /** | |
93 * Write the trajectory to the database. | |
94 * | |
95 * @param[in] trajectory trajectory | |
96 * @return information whether the operation was successful | |
97 */ | |
98 bool write(const Trajectory<T> &trajectory, const std::string& tableName = "trajectories") | |
99 { | |
100 std::stringstream ss; | |
101 ss << trajectory; | |
102 std::string s = ss.str(); | |
103 | |
104 unsigned int sIdLen = TrajectoryDBAccess<T>::toString(trajectory.getId()).length(); | |
105 s[sIdLen] = ','; | |
106 s.insert(sIdLen, "\""); | |
107 s.insert(sIdLen + 2, "\""); | |
108 | |
109 std::string statementString = "insert into "+tableName+" (trajectory_id, trajectory) values (\"" + s + "\")"; | |
110 const char *statement = statementString.c_str(); | |
111 | |
112 bool success = TrajectoryDBAccess<T>::db->executeStatement(statement); | |
113 return success; | |
114 } | |
115 }; | |
116 | |
117 #endif /* TRAJECTORYDBACCESSBLOB_H_ */ |