31 filepath(path), db(nullptr), stmt(nullptr), database_open(true), end_of_database(false)
34 int error_code = sqlite3_open(filepath.c_str(), &db);
35 if (error_code != SQLITE_OK)
37 ARMARX_ERROR_S <<
"Error opening database: " << sqlite3_errmsg(db);
40 database_open =
false;
45 std::string sql_select =
"SELECT * FROM TopicData WHERE ID >= ?";
46 error_code = sqlite3_prepare_v2(db, sql_select.c_str(), -1, &stmt,
nullptr);
47 if (error_code != SQLITE_OK)
49 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
51 sqlite3_finalize(stmt);
53 database_open =
false;
60 ARMARX_ERROR_S <<
"Can not seek to start position, or maybe database is empty.";
61 sqlite3_finalize(stmt);
63 database_open =
false;
68 if (sqlite3_column_count(stmt) != 5)
70 ARMARX_ERROR_S <<
"Database table layout is wrong. Closing database.";
71 sqlite3_finalize(stmt);
73 database_open =
false;
81 sqlite3_finalize(stmt);
93 std::unique_lock lock(mutex);
98 ARMARX_ERROR_S <<
"Database not open or database table layout is wrong";
111 data.timestamp = IceUtil::Time::microSeconds(sqlite3_column_int64(stmt, 1));
112 data.topicName = std::string(
reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2)));
113 data.operationName = std::string(
reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3)));
116 const void* blob_data = sqlite3_column_blob(stmt, 4);
117 int size = sqlite3_column_bytes(stmt, 4);
119 data.inParams = std::vector<Ice::Byte>((Ice::Byte*)blob_data, (Ice::Byte*)blob_data + size);
122 int error_code = sqlite3_step(stmt);
124 if (error_code != SQLITE_ROW)
127 if (error_code == SQLITE_DONE)
130 end_of_database =
true;
134 ARMARX_ERROR_S <<
"Retrieving next row of data failed, closing database: " << sqlite3_errmsg(db);
135 sqlite3_finalize(stmt);
137 database_open =
false;
147 sqlite3_stmt* stmt_get_id;
148 std::string sql_get_id =
"SELECT ID FROM TopicData WHERE TIME >= ? LIMIT 1";
150 int error_code = sqlite3_prepare_v2(db, sql_get_id.c_str(), -1, &stmt_get_id,
nullptr);
151 if (error_code != SQLITE_OK)
153 ARMARX_ERROR <<
"Could not seek to timestamp: " << timestamp.toMicroSeconds();
154 ARMARX_ERROR <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
156 sqlite3_finalize(stmt_get_id);
160 error_code = sqlite3_bind_int64(stmt_get_id, 1, timestamp.toMicroSeconds());
161 if (error_code != SQLITE_OK)
163 ARMARX_ERROR <<
"Could not seek to timestamp: " << timestamp.toMicroSeconds();
164 ARMARX_ERROR <<
"Can not bind parameter to sql query: " << sqlite3_errmsg(db);
166 sqlite3_finalize(stmt_get_id);
170 error_code = sqlite3_step(stmt_get_id);
172 if (error_code != SQLITE_ROW)
174 ARMARX_ERROR <<
"Could not seek to timestamp: " << timestamp.toMicroSeconds();
175 ARMARX_ERROR <<
"Query did not succeed or we got no result: " << sqlite3_errmsg(db);
177 sqlite3_finalize(stmt_get_id);
181 bool returnValue =
seekTo(sqlite3_column_int(stmt_get_id, 0));
182 sqlite3_finalize(stmt_get_id);
189 sqlite3_stmt* stmt_replay_length;
190 std::string sql_replay_length =
"SELECT max(TIME) FROM TopicData";
191 int error_code = sqlite3_prepare_v2(db, sql_replay_length.c_str(), -1, &stmt_replay_length,
nullptr);
192 if (error_code != SQLITE_OK)
194 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
196 sqlite3_finalize(stmt_replay_length);
197 return IceUtil::Time::microSeconds(0);
199 error_code = sqlite3_step(stmt_replay_length);
200 if (error_code != SQLITE_ROW)
202 ARMARX_ERROR_S <<
"Can not execute sql statement, or no result: " << sqlite3_errmsg(db);
204 sqlite3_finalize(stmt_replay_length);
205 return IceUtil::Time::microSeconds(0);
208 long returnValue = sqlite3_column_int64(stmt_replay_length, 0);
209 sqlite3_finalize(stmt_replay_length);
210 return IceUtil::Time::microSeconds(returnValue);
215 std::vector<std::string> topicList;
220 ARMARX_ERROR <<
"Can not read list of replay topics if database is not open";
225 sqlite3_stmt* stmt_topic_list;
226 std::string sql_topic_list =
"SELECT TOPIC FROM Topics";
227 int error_code = sqlite3_prepare_v2(db, sql_topic_list.c_str(), -1, &stmt_topic_list,
nullptr);
228 if (error_code != SQLITE_OK)
230 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
232 sqlite3_finalize(stmt_topic_list);
235 error_code = sqlite3_step(stmt_topic_list);
236 if (error_code != SQLITE_ROW)
238 ARMARX_ERROR_S <<
"Can not execute sql statement, or no result: " << sqlite3_errmsg(db);
240 sqlite3_finalize(stmt_topic_list);
245 while (error_code == SQLITE_ROW)
247 topicList.push_back(std::string(
reinterpret_cast<const char*
>(sqlite3_column_text(stmt_topic_list, 0))));
248 error_code = sqlite3_step(stmt_topic_list);
253 if (error_code != SQLITE_DONE)
255 ARMARX_ERROR_S <<
"Error reading topic list: " << sqlite3_errmsg(db);
257 sqlite3_finalize(stmt_topic_list);
261 sqlite3_finalize(stmt_topic_list);
268 std::unique_lock lock(mutex);
273 ARMARX_ERROR <<
"Can not seek to position if database is not open";
279 sqlite3_clear_bindings(stmt);
282 end_of_database =
false;
285 int error_code = sqlite3_bind_int(stmt, 1,
ID);
286 if (error_code != SQLITE_OK)
288 ARMARX_ERROR_S <<
"Could not bind data while trying to seek: " << sqlite3_errmsg(db);
289 sqlite3_finalize(stmt);
291 database_open =
false;
295 error_code = sqlite3_step(stmt);
297 if (error_code != SQLITE_ROW)
299 ARMARX_ERROR_S <<
"Query did not succeed or we got no result while trying to seek: " << sqlite3_errmsg(db);
300 sqlite3_finalize(stmt);
302 database_open =
false;