diff --git a/src/DistroqueryAPI.cpp b/src/DistroqueryAPI.cpp index f816598..d0514ec 100644 --- a/src/DistroqueryAPI.cpp +++ b/src/DistroqueryAPI.cpp @@ -91,6 +91,9 @@ json DistroqueryAPI::getPackageSourceDetailsById(configTag* ct, long id) { ostringstream ss; ss << std::put_time(gmtime(&itt), "%FT%TZ"); j["buildtime"] = ss.str(); + // Download URL + j["download_url"] = string(ct->download_prefix) + ct->download_dir + "/SRPMS.base/" + + string(j["name"]) + "-" + string(j["version"]) + "-" + string(j["release"]) + ".src.rpm"; // Source URL j["source_url"] = "https://src.openmamba.org/rpms/" + string(j["name"]); } else { @@ -117,7 +120,7 @@ json DistroqueryAPI::getRepositoryPackages(string repository) { return j; } - sql = "SELECT * FROM sources"; + sql = "SELECT * FROM sources ORDER BY name COLLATE NOCASE ASC"; if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { j = json::array(); while (sqlite3_step(stmt) == SQLITE_ROW) { @@ -134,10 +137,42 @@ json DistroqueryAPI::getRepositoryPackages(string repository) { return j; } +json DistroqueryAPI::getBuiltPackagesFromSourceID(configTag* ct, long id) { + json j = json::array(); + string sql; + sqlite3_stmt *stmt; + + for (auto arch: ct->arch) { + if (arch == NULL) break; + auto db = openRepositoryDatabase(ct, arch); + if (!db) { + j = {}; + j["error"] = "error opening database for repository '" + string(ct->tag) + "' and arch '" + arch + "'"; + return j; + } + + sql = "SELECT * FROM packages WHERE id_source = " + to_string(id) + " ORDER BY name"; + if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { + while (sqlite3_step(stmt) == SQLITE_ROW) { + json package = {}; + package["arch"] = arch; + package["name"] = reinterpret_cast(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "name"))); + j.push_back(package); + } + sqlite3_finalize(stmt); + } else { + j = {}; + j["error"] = "error preparing query '" + sql + "'"; + return j; + } + } + return j; +} + json DistroqueryAPI::getPackageSourceDetails(string repository, string package) { json j; string sql; - sqlite3_stmt *stmt; + sqlite3_stmt *stmt, *stmt2; struct configTag* ct = findRepositoryByTag(repository.c_str()); if (ct == NULL) { @@ -157,8 +192,30 @@ json DistroqueryAPI::getPackageSourceDetails(string repository, string package) long id; if (sqlite3_step(stmt) == SQLITE_ROW) { id = sqlite3_column_int(stmt,sqlite3_find_column_id(stmt, NULL, "id")); - return getPackageSourceDetailsById(ct, id); + j = getPackageSourceDetailsById(ct, id); + } else { + j["error"] = "source package '" + package + "' not found in repository '" + repository + "'"; + return j; } + + // Build requires + sql = "SELECT * FROM buildrequires WHERE id_source=" + to_string(id) + + " ORDER BY buildrequirename"; + j["buildrequires"] = json::array(); + if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt2, NULL) == SQLITE_OK) { + while (sqlite3_step(stmt2) == SQLITE_ROW) { + json buildrequire = {}; + buildrequire["name"] = reinterpret_cast(sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, NULL, "buildrequirename"))); + buildrequire["flags"] = rpmSenseFlagsToString(sqlite3_column_int(stmt2,sqlite3_find_column_id(stmt2, NULL, "buildrequireflags"))); + buildrequire["version"] = reinterpret_cast(sqlite3_column_text(stmt2,sqlite3_find_column_id(stmt2, NULL, "buildrequireversion"))); + j["buildrequires"].push_back(buildrequire); + } + sqlite3_finalize(stmt2); + } + + // Children + j["children"] = getBuiltPackagesFromSourceID(ct, id); + } else { j["error"] = "error preparing query '" + sql + "'"; return j; @@ -204,7 +261,7 @@ json DistroqueryAPI::getPackageDetails(string repository, string package, string j["size"] = sqlite3_column_int(stmt,sqlite3_find_column_id(stmt, NULL, "size")); } else { - j["error"] = "no results from query '" + sql + "'"; + j["error"] = "package '" + package + "' not found in repository '" + repository + "'"; return j; } diff --git a/src/DistroqueryAPI.hpp b/src/DistroqueryAPI.hpp index e7efae7..dc1c60a 100644 --- a/src/DistroqueryAPI.hpp +++ b/src/DistroqueryAPI.hpp @@ -44,6 +44,7 @@ class DistroqueryAPI { json getRepositoryPackages(string repository); json getPackageSourceDetailsById(configTag* ct, long id); json getPackageSourceDetails(string repository, string package); + json getBuiltPackagesFromSourceID(configTag* ct, long id); json getPackageDetails(string repository, string package, string arch); };