Mercurial Hosting > traffic-intelligence
comparison trajectorymanagement/src/DBSQLiteAccess.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 DBSQLITEACCESS_H_ | |
2 #define DBSQLITEACCESS_H_ | |
3 | |
4 #include "opencv2/core/core.hpp" | |
5 | |
6 #include <sqlite3.h> | |
7 | |
8 #include <sstream> | |
9 #include <string> | |
10 #include <vector> | |
11 #include <map> | |
12 | |
13 template<typename T> class TrajectoryElement; | |
14 | |
15 /** | |
16 * DBSQLiteAccess class. | |
17 * | |
18 * The DBSQLiteAccess class allows to perform basic operations on the database like connecting, disconnecting, creating a table, deleting a table or executing other statements. | |
19 */ | |
20 class DBSQLiteAccess | |
21 { | |
22 public: | |
23 /** | |
24 * Constructor. | |
25 */ | |
26 DBSQLiteAccess(); | |
27 | |
28 /** | |
29 * Destructor. | |
30 */ | |
31 virtual ~DBSQLiteAccess(); | |
32 | |
33 /** | |
34 * Connect to the database. | |
35 * | |
36 * @param[in] database name of the database | |
37 * @return information whether the operation was successful | |
38 */ | |
39 bool connect(const char *database); | |
40 | |
41 /** | |
42 * Inform whether the connection to the database has been established. | |
43 * | |
44 * @return information whether the connection to the database has been established | |
45 */ | |
46 bool isConnected(); | |
47 | |
48 /** | |
49 * Disconnect from the database. | |
50 * | |
51 * @return information whether the operation was successful | |
52 */ | |
53 bool disconnect(); | |
54 | |
55 int sqliteErrCode(); | |
56 | |
57 /** | |
58 * Get error message. | |
59 * | |
60 * @return last error message | |
61 */ | |
62 const char *sqliteErrMsg(); | |
63 | |
64 /** | |
65 * Execute statement. | |
66 * | |
67 * @param[in] statement statement | |
68 * @return information whether the operation was successful | |
69 */ | |
70 bool executeStatement(const char *statement); | |
71 | |
72 bool executeStatementGetSingleValue(const char *statement, std::string& result); | |
73 | |
74 template<typename T> | |
75 bool executeStatementGetSingleValue(const char *statement, T& result) | |
76 { | |
77 char **dbResult = 0; | |
78 int nrows, ncols; | |
79 bool success = executeStatement(statement, &dbResult, nrows, ncols); | |
80 | |
81 if (success && nrows == 1 && ncols == 1 && dbResult[1] != NULL) | |
82 { | |
83 std::istringstream is(dbResult[1]); | |
84 is >> result; | |
85 } | |
86 else | |
87 { | |
88 success = false; | |
89 } | |
90 | |
91 sqlite3_free_table(dbResult); | |
92 | |
93 return success; | |
94 } | |
95 | |
96 /** | |
97 * Execute statement and get results. | |
98 * | |
99 * @param[in] statement statement | |
100 * @return result as a matrix of strings | |
101 */ | |
102 bool executeStatementGetMatrix(const char *statement, std::vector<std::vector<std::string> >& result); | |
103 | |
104 /** | |
105 * Execute statement and get results of a select. | |
106 * | |
107 * @param[in] statement statement | |
108 * @return result as a matrix of strings | |
109 */ | |
110 bool executeStatementSelect(std::vector<std::vector<std::string> >& result, const char *statement); | |
111 | |
112 /** Execute statement and get result of select as a list of integers (eg trajectory ids) */ | |
113 bool executeStatementSelectIntegers(std::vector<int>& result, const char *statement); | |
114 | |
115 /** Execute statement and get result of select as a list of integers (more than one per row, eg trajectory first and last frames) */ | |
116 bool executeStatementSelectMultipleIntegers(std::vector<std::vector<int> >& result, const char *statement); | |
117 | |
118 /** | |
119 * Execute statement and get results of a select for two integers followed by two floats. | |
120 * | |
121 * @param[in] statement statement | |
122 * @return result as a matrix of strings | |
123 */ | |
124 bool executeStatementSelectTrajectoryElements(std::map<int, std::vector<TrajectoryElement<cv::Point2f> > >& trajectoryElements, const char *statement); | |
125 | |
126 /** | |
127 * Execute statement and get results of a select for two integers (prototype id and matched trajectory id) | |
128 * | |
129 * @param[in] | |
130 * @return result as a matrix of strings | |
131 */ | |
132 bool executeStatementSelectPrototypeMatches(std::multimap<int,int>& matches, const char *statement); | |
133 | |
134 /** | |
135 * Execute begin transaction command. | |
136 * | |
137 * @return information whether the operation was successful | |
138 */ | |
139 bool begin(); | |
140 | |
141 /** | |
142 * Execute end transaction command. | |
143 * | |
144 * @return information whether the operation was successful | |
145 */ | |
146 bool end(); | |
147 | |
148 /** | |
149 * Execute rollback transaction command. | |
150 * | |
151 * @return information whether the operation was successful | |
152 */ | |
153 bool rollback(); | |
154 | |
155 /** | |
156 * Execute commit transaction command. | |
157 * | |
158 * @return information whether the operation was successful | |
159 */ | |
160 bool commit(); | |
161 | |
162 private: | |
163 /** | |
164 * Pointer to SQLite3 class. | |
165 */ | |
166 sqlite3 *db; | |
167 | |
168 /** | |
169 * Information whether a connection has been established. | |
170 */ | |
171 bool connected; | |
172 | |
173 /** | |
174 * Execute statement and get result. | |
175 * | |
176 * @param[in] statement statement | |
177 * @param[out] result results | |
178 * @param[out] nrows of rows in the returned data \a result | |
179 * @param[out] ncols of columns in the returned data \a result | |
180 * @return information whether the operation was successful | |
181 */ | |
182 bool executeStatement(const char *statement, char ***result, int &nrows, int &ncols); | |
183 }; | |
184 | |
185 #endif /* DBSQLITEACCESS_H_ */ |