Bug fixing
This commit is contained in:
@@ -154,15 +154,15 @@ Database::~Database() {
|
||||
}
|
||||
}
|
||||
|
||||
bool Database::remove(const std::string& label_tag) {
|
||||
std::string sql = "DELETE FROM objects WHERE label_tag = ?;";
|
||||
bool Database::remove(const std::string& labeltag) {
|
||||
std::string sql = "DELETE FROM objects WHERE labeltag = ?;";
|
||||
sqlite3_stmt* stmt;
|
||||
|
||||
if (sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, label_tag.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 1, labeltag.c_str(), -1, SQLITE_STATIC);
|
||||
bool success = sqlite3_step(stmt) == SQLITE_DONE;
|
||||
sqlite3_finalize(stmt);
|
||||
return success;
|
||||
@@ -199,8 +199,8 @@ bool Database::get(const std::string& key, dbEntry& entry) {
|
||||
|
||||
if (key.find(':') != std::string::npos) {
|
||||
// Create JSON array pattern for LIKE query
|
||||
std::string label_tag_pattern = "%\"" + key + "\"%";
|
||||
sqlite3_bind_text(stmt, 1, label_tag_pattern.c_str(), -1, SQLITE_STATIC);
|
||||
std::string labeltag_pattern = "%\"" + key + "\"%";
|
||||
sqlite3_bind_text(stmt, 1, labeltag_pattern.c_str(), -1, SQLITE_STATIC);
|
||||
} else {
|
||||
sqlite3_bind_text(stmt, 1, key.c_str(), -1, SQLITE_STATIC);
|
||||
}
|
||||
@@ -214,7 +214,7 @@ bool Database::get(const std::string& key, dbEntry& entry) {
|
||||
std::string labeltags_str = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
std::string metadata_str = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
|
||||
entry.label_tags = nlohmann::json::parse(labeltags_str).get<std::vector<std::string>>();
|
||||
entry.labeltags = nlohmann::json::parse(labeltags_str).get<std::vector<std::string>>();
|
||||
entry.metadata = nlohmann::json::parse(metadata_str);
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
@@ -236,7 +236,7 @@ bool Database::list(std::vector<dbEntry>& entries) {
|
||||
std::string labeltags_str = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
std::string metadata_str = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
|
||||
entry.label_tags = nlohmann::json::parse(labeltags_str).get<std::vector<std::string>>();
|
||||
entry.labeltags = nlohmann::json::parse(labeltags_str).get<std::vector<std::string>>();
|
||||
entry.metadata = nlohmann::json::parse(metadata_str);
|
||||
entries.push_back(entry);
|
||||
}
|
||||
@@ -247,12 +247,12 @@ bool Database::list(std::vector<dbEntry>& entries) {
|
||||
|
||||
bool Database::merge_existing_entry(const dbEntry& existing, const dbEntry& new_entry, dbEntry& merged) {
|
||||
// Merge label:tag pairs
|
||||
std::set<std::string> merged_labeltags(existing.label_tags.begin(), existing.label_tags.end());
|
||||
merged_labeltags.insert(new_entry.label_tags.begin(), new_entry.label_tags.end());
|
||||
std::set<std::string> merged_labeltags(existing.labeltags.begin(), existing.labeltags.end());
|
||||
merged_labeltags.insert(new_entry.labeltags.begin(), new_entry.labeltags.end());
|
||||
|
||||
// Create merged entry
|
||||
merged = new_entry; // Start with new entry's data
|
||||
merged.label_tags = std::vector<std::string>(merged_labeltags.begin(), merged_labeltags.end());
|
||||
merged.labeltags = std::vector<std::string>(merged_labeltags.begin(), merged_labeltags.end());
|
||||
|
||||
// Update metadata - preserve fields from existing entry that aren't in new entry
|
||||
merged.metadata = existing.metadata; // Start with existing metadata
|
||||
@@ -261,7 +261,7 @@ bool Database::merge_existing_entry(const dbEntry& existing, const dbEntry& new_
|
||||
}
|
||||
|
||||
// Ensure required fields are set correctly
|
||||
merged.metadata["labeltags"] = merged.label_tags;
|
||||
merged.metadata["labeltags"] = merged.labeltags;
|
||||
merged.metadata["hash"] = merged.hash;
|
||||
|
||||
// Update database
|
||||
@@ -272,7 +272,7 @@ bool Database::merge_existing_entry(const dbEntry& existing, const dbEntry& new_
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string labeltags_str = nlohmann::json(merged.label_tags).dump();
|
||||
std::string labeltags_str = nlohmann::json(merged.labeltags).dump();
|
||||
std::string metadata_str = merged.metadata.dump();
|
||||
|
||||
sqlite3_bind_text(stmt, 1, labeltags_str.c_str(), -1, SQLITE_STATIC);
|
||||
@@ -294,10 +294,10 @@ bool Database::insert_new_entry(const dbEntry& entry) {
|
||||
|
||||
// Update metadata to include labeltags and hash
|
||||
nlohmann::json metadata = entry.metadata;
|
||||
metadata["labeltags"] = entry.label_tags;
|
||||
metadata["labeltags"] = entry.labeltags;
|
||||
metadata["hash"] = entry.hash;
|
||||
|
||||
std::string labeltags_str = nlohmann::json(entry.label_tags).dump();
|
||||
std::string labeltags_str = nlohmann::json(entry.labeltags).dump();
|
||||
std::string metadata_str = metadata.dump();
|
||||
|
||||
sqlite3_bind_text(stmt, 1, entry.hash.c_str(), -1, SQLITE_STATIC);
|
||||
@@ -310,7 +310,7 @@ bool Database::insert_new_entry(const dbEntry& entry) {
|
||||
}
|
||||
|
||||
bool Database::handle_tag_conflicts(const dbEntry& entry) {
|
||||
for (const auto& label_tag : entry.label_tags) {
|
||||
for (const auto& labeltag : entry.labeltags) {
|
||||
// Find all entries with this exact label:tag pair
|
||||
std::string find_sql = "SELECT hash, labeltags, metadata FROM objects WHERE labeltags LIKE ?;";
|
||||
sqlite3_stmt* stmt;
|
||||
@@ -318,8 +318,8 @@ bool Database::handle_tag_conflicts(const dbEntry& entry) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string label_tag_pattern = "%\"" + label_tag + "\"%";
|
||||
sqlite3_bind_text(stmt, 1, label_tag_pattern.c_str(), -1, SQLITE_STATIC);
|
||||
std::string labeltag_pattern = "%\"" + labeltag + "\"%";
|
||||
sqlite3_bind_text(stmt, 1, labeltag_pattern.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
std::string other_hash = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
@@ -331,20 +331,20 @@ bool Database::handle_tag_conflicts(const dbEntry& entry) {
|
||||
// Parse the other entry
|
||||
dbEntry other;
|
||||
other.hash = other_hash;
|
||||
other.label_tags = nlohmann::json::parse(other_labeltags_str).get<std::vector<std::string>>();
|
||||
other.labeltags = nlohmann::json::parse(other_labeltags_str).get<std::vector<std::string>>();
|
||||
other.metadata = nlohmann::json::parse(other_metadata_str);
|
||||
|
||||
// Remove the exact label:tag pair
|
||||
std::vector<std::string> new_labeltags;
|
||||
for (const auto& other_label_tag : other.label_tags) {
|
||||
if (other_label_tag != label_tag) {
|
||||
new_labeltags.push_back(other_label_tag);
|
||||
for (const auto& other_labeltag : other.labeltags) {
|
||||
if (other_labeltag != labeltag) {
|
||||
new_labeltags.push_back(other_labeltag);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the other entry if it had the label:tag pair removed
|
||||
if (new_labeltags.size() != other.label_tags.size()) {
|
||||
other.label_tags = new_labeltags;
|
||||
if (new_labeltags.size() != other.labeltags.size()) {
|
||||
other.labeltags = new_labeltags;
|
||||
other.metadata["labeltags"] = new_labeltags; // Update metadata to match
|
||||
|
||||
std::string update_sql = "UPDATE objects SET labeltags = ?, metadata = ? WHERE hash = ?;";
|
||||
|
Reference in New Issue
Block a user