+int64_t LocalDb::SegmentToId(const string &segment)
+{
+ int rc;
+ sqlite3_stmt *stmt;
+ static const char s1[] =
+ "insert or ignore into segments(segment) values (?);";
+ static const char s2[] =
+ "select segmentid from segments where segment = ?";
+ const char *tail;
+ int64_t result;
+
+ rc = sqlite3_prepare_v2(db, s1, strlen(s1), &stmt, &tail);
+ if (rc != SQLITE_OK) {
+ throw IOException("Find id by segment name");
+ }
+ sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
+ SQLITE_TRANSIENT);
+ rc = sqlite3_step(stmt);
+ if (rc != SQLITE_DONE) {
+ throw IOException("Could not execute INSERT statement!");
+ }
+ sqlite3_finalize(stmt);
+
+ rc = sqlite3_prepare_v2(db, s2, strlen(s2), &stmt, &tail);
+ if (rc != SQLITE_OK) {
+ throw IOException("Find id by segment name");
+ }
+
+ sqlite3_bind_text(stmt, 1, segment.c_str(), segment.size(),
+ SQLITE_TRANSIENT);
+
+ rc = sqlite3_step(stmt);
+ if (rc == SQLITE_DONE) {
+ throw IOException("No segment found by id");
+ } else if (rc == SQLITE_ROW) {
+ result = sqlite3_column_int64(stmt, 0);
+ } else {
+ throw IOException("Error executing find segment by id query");
+ }
+
+ sqlite3_finalize(stmt);
+
+ return result;
+}
+
+string LocalDb::IdToSegment(int64_t segmentid)
+{
+ int rc;
+ sqlite3_stmt *stmt;
+ static const char s[] =
+ "select segment from segments where segmentid = ?";
+ const char *tail;
+ string result;
+
+ rc = sqlite3_prepare_v2(db, s, strlen(s), &stmt, &tail);
+ if (rc != SQLITE_OK) {
+ throw IOException("Find segment by id");
+ }
+
+ sqlite3_bind_int64(stmt, 1, segmentid);
+
+ rc = sqlite3_step(stmt);
+ if (rc == SQLITE_DONE) {
+ throw IOException("No segment found by id");
+ } else if (rc == SQLITE_ROW) {
+ result = (const char *)sqlite3_column_text(stmt, 0);
+ } else {
+ throw IOException("Error executing find segment by id query");
+ }
+
+ sqlite3_finalize(stmt);
+
+ return result;
+}
+