DistroqueryAPI: getRepositoryPackages: attach results for binary packages

This commit is contained in:
Silvan Calarco 2024-07-06 22:07:38 +02:00
parent 35ec97e05e
commit ec8b4088c0
2 changed files with 21 additions and 4 deletions

View File

@ -137,15 +137,31 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
return j;
}
for (auto arch: ct->arch) {
if (arch == NULL) break;
attachRepositoryDatabases(ct, db, arch, 0);
}
string query_append;
if (query != "")
query_append = "WHERE name LIKE '%" + query + "%' OR summary LIKE '%" + query +
"%' OR description LIKE '%" + query + "%'";
sql = "SELECT COUNT(*) OVER() AS total_count,* FROM sources " + query_append +
" ORDER BY name COLLATE NOCASE ASC"
sql = "SELECT u.*,COUNT(*) OVER() AS total_count FROM ("
"SELECT name,arch,version,release,summary,buildtime,0 AS sort FROM sources " + query_append;
int archorder = 1;
for (auto arch: ct->arch) {
if (arch == NULL) break;
sql += " UNION SELECT name,arch,version,release,summary,buildtime," + to_string(archorder) +
" AS sort FROM '" + repository + "_" + arch + "'.packages " + query_append;
archorder++;
}
sql += ") AS u ORDER BY name COLLATE NOCASE ASC,sort"
" LIMIT " + to_string(per_page) +
" OFFSET " + to_string(per_page * (page -1));
if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) {
json packages = json::array();
long count = 0;
@ -160,6 +176,7 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
}
json package;
package["name"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "name")));
package["arch"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "arch")));
package["version"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "version")));
package["release"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "release")));
package["summary"] = reinterpret_cast<const char*>(sqlite3_column_text(stmt,sqlite3_find_column_id(stmt, NULL, "summary")));
@ -173,7 +190,7 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
j["query"]["to"] = per_page * (page - 1) + count;
j["packages"] = packages;
} else {
j["error"] = "error preparing query '" + sql + "'";
j["error"] = "error preparing query '" + sql + "': " + sqlite3_errmsg(db);
return j;
}
return j;

View File

@ -97,7 +97,7 @@ void attachRepositoryDatabases(struct configTag* ct, sqlite3 *db, string arch, i
char *errmsg;
dbname = string(ct->repository_dir) + ct->tag + "-" + arch + ".db";
sql = "ATTACH DATABASE '" + dbname + "' as '" + ct->tag + "_" + arch;
sql = "ATTACH DATABASE '" + dbname + "' as '" + ct->tag + "_" + arch + "'";
if (sqlite3_exec(db, sql.c_str(), NULL, NULL, &errmsg)) {
cerr << "ERROR: unable to exec statement for " << sql << ": " << errmsg << endl;
}