This commit is contained in:
@@ -182,22 +182,13 @@ bool Database::remove_by_hash(const std::string& hash) {
|
||||
return success;
|
||||
}
|
||||
|
||||
bool Database::get(const std::string& hash_or_labeltag, dbEntry& entry) {
|
||||
std::string sql;
|
||||
if (hash_or_labeltag.find(':') != std::string::npos) {
|
||||
// Query by label:tag - search for exact match in the JSON array
|
||||
sql = "SELECT hash, labeltags, metadata FROM objects WHERE json_array_length(labeltags) > 0 AND EXISTS (SELECT 1 FROM json_each(labeltags) WHERE value = ?);";
|
||||
} else {
|
||||
// Query by hash
|
||||
sql = "SELECT hash, labeltags, metadata FROM objects WHERE hash = ?;";
|
||||
}
|
||||
|
||||
bool Database::run_sql_text(const std::string& sql, const std::string& bind_text, dbEntry& entry) {
|
||||
sqlite3_stmt* stmt;
|
||||
if (sqlite3_prepare_v2(db_, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, hash_or_labeltag.c_str(), -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 1, bind_text.c_str(), -1, SQLITE_STATIC);
|
||||
|
||||
if (sqlite3_step(stmt) != SQLITE_ROW) {
|
||||
sqlite3_finalize(stmt);
|
||||
@@ -207,7 +198,7 @@ bool Database::get(const std::string& hash_or_labeltag, dbEntry& entry) {
|
||||
entry.hash = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
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.labeltags = nlohmann::json::parse(labeltags_str).get<std::vector<std::string>>();
|
||||
entry.metadata = nlohmann::json::parse(metadata_str);
|
||||
|
||||
@@ -215,6 +206,18 @@ bool Database::get(const std::string& hash_or_labeltag, dbEntry& entry) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Database::get(const std::string& hash_or_labeltag, dbEntry& entry) {
|
||||
|
||||
if (hash_or_labeltag.find(':') != std::string::npos)
|
||||
return (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE json_array_length(labeltags) > 0 AND EXISTS (SELECT 1 FROM json_each(labeltags) WHERE value = ?);", hash_or_labeltag, entry));
|
||||
|
||||
if (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE hash = ?;", hash_or_labeltag, entry))
|
||||
return true;
|
||||
|
||||
std::string with_latest = hash_or_labeltag + ":latest";
|
||||
return (run_sql_text("SELECT hash, labeltags, metadata FROM objects WHERE hash = ?;", with_latest, entry));
|
||||
}
|
||||
|
||||
bool Database::list(std::vector<dbEntry>& entries) {
|
||||
std::string sql = "SELECT hash, labeltags, metadata FROM objects;";
|
||||
sqlite3_stmt* stmt;
|
||||
|
@@ -40,6 +40,8 @@ class Database {
|
||||
bool merge_existing_entry(const dbEntry& existing, const dbEntry& new_entry, dbEntry& merged);
|
||||
bool insert_new_entry(const dbEntry& entry);
|
||||
bool handle_tag_conflicts(const dbEntry& entry);
|
||||
|
||||
bool run_sql_text(const std::string& sql, const std::string& bind_text, dbEntry& entry);
|
||||
};
|
||||
|
||||
} // namespace simple_object_storage
|
||||
|
Reference in New Issue
Block a user