32 filepath(path), db(nullptr), stmt(nullptr), database_open(true), end_of_database(false)
35 int error_code = sqlite3_open(filepath.c_str(), &db);
36 if (error_code != SQLITE_OK)
38 ARMARX_ERROR_S <<
"Error opening database: " << sqlite3_errmsg(db);
41 database_open =
false;
46 std::string sql_select =
"SELECT * FROM TopicData WHERE ID >= ?";
47 error_code = sqlite3_prepare_v2(db, sql_select.c_str(), -1, &stmt,
nullptr);
48 if (error_code != SQLITE_OK)
50 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
52 sqlite3_finalize(stmt);
54 database_open =
false;
61 ARMARX_ERROR_S <<
"Can not seek to start position, or maybe database is empty.";
62 sqlite3_finalize(stmt);
64 database_open =
false;
69 if (sqlite3_column_count(stmt) != 5)
71 ARMARX_ERROR_S <<
"Database table layout is wrong. Closing database.";
72 sqlite3_finalize(stmt);
74 database_open =
false;
82 sqlite3_finalize(stmt);
96 std::unique_lock lock(mutex);
101 ARMARX_ERROR_S <<
"Database not open or database table layout is wrong";
114 data.timestamp = IceUtil::Time::microSeconds(sqlite3_column_int64(stmt, 1));
115 data.topicName = std::string(
reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 2)));
117 std::string(
reinterpret_cast<const char*
>(sqlite3_column_text(stmt, 3)));
120 const void* blob_data = sqlite3_column_blob(stmt, 4);
121 int size = sqlite3_column_bytes(stmt, 4);
123 data.inParams = std::vector<Ice::Byte>((Ice::Byte*)blob_data, (Ice::Byte*)blob_data + size);
126 int error_code = sqlite3_step(stmt);
128 if (error_code != SQLITE_ROW)
131 if (error_code == SQLITE_DONE)
134 end_of_database =
true;
138 ARMARX_ERROR_S <<
"Retrieving next row of data failed, closing database: "
139 << sqlite3_errmsg(db);
140 sqlite3_finalize(stmt);
142 database_open =
false;
153 sqlite3_stmt* stmt_get_id;
154 std::string sql_get_id =
"SELECT ID FROM TopicData WHERE TIME >= ? LIMIT 1";
156 int error_code = sqlite3_prepare_v2(db, sql_get_id.c_str(), -1, &stmt_get_id,
nullptr);
157 if (error_code != SQLITE_OK)
159 ARMARX_ERROR <<
"Could not seek to timestamp: " << timestamp.toMicroSeconds();
160 ARMARX_ERROR <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
162 sqlite3_finalize(stmt_get_id);
166 error_code = sqlite3_bind_int64(stmt_get_id, 1, timestamp.toMicroSeconds());
167 if (error_code != SQLITE_OK)
169 ARMARX_ERROR <<
"Could not seek to timestamp: " << timestamp.toMicroSeconds();
170 ARMARX_ERROR <<
"Can not bind parameter to sql query: " << sqlite3_errmsg(db);
172 sqlite3_finalize(stmt_get_id);
176 error_code = sqlite3_step(stmt_get_id);
178 if (error_code != SQLITE_ROW)
180 ARMARX_ERROR <<
"Could not seek to timestamp: " << timestamp.toMicroSeconds();
181 ARMARX_ERROR <<
"Query did not succeed or we got no result: " << sqlite3_errmsg(db);
183 sqlite3_finalize(stmt_get_id);
187 bool returnValue =
seekTo(sqlite3_column_int(stmt_get_id, 0));
188 sqlite3_finalize(stmt_get_id);
196 sqlite3_stmt* stmt_replay_length;
197 std::string sql_replay_length =
"SELECT max(TIME) FROM TopicData";
199 sqlite3_prepare_v2(db, sql_replay_length.c_str(), -1, &stmt_replay_length,
nullptr);
200 if (error_code != SQLITE_OK)
202 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
204 sqlite3_finalize(stmt_replay_length);
205 return IceUtil::Time::microSeconds(0);
207 error_code = sqlite3_step(stmt_replay_length);
208 if (error_code != SQLITE_ROW)
210 ARMARX_ERROR_S <<
"Can not execute sql statement, or no result: " << sqlite3_errmsg(db);
212 sqlite3_finalize(stmt_replay_length);
213 return IceUtil::Time::microSeconds(0);
216 long returnValue = sqlite3_column_int64(stmt_replay_length, 0);
217 sqlite3_finalize(stmt_replay_length);
218 return IceUtil::Time::microSeconds(returnValue);
221 std::vector<std::string>
224 std::vector<std::string> topicList;
229 ARMARX_ERROR <<
"Can not read list of replay topics if database is not open";
234 sqlite3_stmt* stmt_topic_list;
235 std::string sql_topic_list =
"SELECT TOPIC FROM Topics";
237 sqlite3_prepare_v2(db, sql_topic_list.c_str(), -1, &stmt_topic_list,
nullptr);
238 if (error_code != SQLITE_OK)
240 ARMARX_ERROR_S <<
"Can not prepare sql statement: " << sqlite3_errmsg(db);
242 sqlite3_finalize(stmt_topic_list);
245 error_code = sqlite3_step(stmt_topic_list);
246 if (error_code != SQLITE_ROW)
248 ARMARX_ERROR_S <<
"Can not execute sql statement, or no result: " << sqlite3_errmsg(db);
250 sqlite3_finalize(stmt_topic_list);
255 while (error_code == SQLITE_ROW)
257 topicList.push_back(std::string(
258 reinterpret_cast<const char*
>(sqlite3_column_text(stmt_topic_list, 0))));
259 error_code = sqlite3_step(stmt_topic_list);
264 if (error_code != SQLITE_DONE)
266 ARMARX_ERROR_S <<
"Error reading topic list: " << sqlite3_errmsg(db);
268 sqlite3_finalize(stmt_topic_list);
272 sqlite3_finalize(stmt_topic_list);
280 std::unique_lock lock(mutex);
285 ARMARX_ERROR <<
"Can not seek to position if database is not open";
291 sqlite3_clear_bindings(stmt);
294 end_of_database =
false;
297 int error_code = sqlite3_bind_int(stmt, 1,
ID);
298 if (error_code != SQLITE_OK)
300 ARMARX_ERROR_S <<
"Could not bind data while trying to seek: " << sqlite3_errmsg(db);
301 sqlite3_finalize(stmt);
303 database_open =
false;
307 error_code = sqlite3_step(stmt);
309 if (error_code != SQLITE_ROW)
311 ARMARX_ERROR_S <<
"Query did not succeed or we got no result while trying to seek: "
312 << sqlite3_errmsg(db);
313 sqlite3_finalize(stmt);
315 database_open =
false;