+int64_t LocalDb::SegmentToId(const string &segment)
+{
+ int rc;
+ sqlite3_stmt *stmt;
+ int64_t result;
+
+ stmt = Prepare("insert or ignore into segments(segment) values (?)");
+ 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);
+
+ stmt = Prepare("select segmentid from segments where segment = ?");
+ 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;
+ string result;
+
+ stmt = Prepare("select segment from segments where segmentid = ?");
+ 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;
+}
+