diff --git a/libpkg/pkg_jobs.c b/libpkg/pkg_jobs.c index 35ba979ac6..0e8aa40881 100644 --- a/libpkg/pkg_jobs.c +++ b/libpkg/pkg_jobs.c @@ -1584,7 +1584,7 @@ jobs_solve_full_upgrade(struct pkg_jobs *j) tll_foreach(*candidates, c) { pkg_emit_progress_tick(++elt_num, jcount); - sqlite3_snprintf(sizeof(sqlbuf), sqlbuf, " WHERE id=%" PRId64, + sqlite3_snprintf(sizeof(sqlbuf), sqlbuf, " WHERE p.id=%" PRId64, c->item); if ((it = pkgdb_query_cond(j->db, sqlbuf, NULL, MATCH_ALL)) == NULL) return (EPKG_FATAL); diff --git a/libpkg/pkgdb_query.c b/libpkg/pkgdb_query.c index a7a551cf49..c4f1b52015 100644 --- a/libpkg/pkgdb_query.c +++ b/libpkg/pkgdb_query.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2011-2012 Baptiste Daroussin + * Copyright (c) 2011-2022 Baptiste Daroussin * Copyright (c) 2011-2012 Julien Laffaye * Copyright (c) 2011 Will Andrews * Copyright (c) 2011 Philippe Pepiot @@ -79,44 +79,44 @@ pkgdb_get_pattern_query(const char *pattern, match_t match) if (pkgdb_case_sensitive()) { if (checkuid == NULL) { if (checkorigin == NULL) - comp = " WHERE (name = ?1 OR name || '-' || version = ?1)"; + comp = " WHERE (p.name = ?1 OR p.name || '-' || version = ?1)"; else - comp = " WHERE origin = ?1"; + comp = " WHERE (origin = ?1 OR categories.name || substr(origin, instr(origin, '/')) = ?1)"; } else { - comp = " WHERE name = ?1"; + comp = " WHERE p.name = ?1"; } } else { if (checkuid == NULL) { if (checkorigin == NULL) - comp = " WHERE (name = ?1 COLLATE NOCASE OR " - "name || '-' || version = ?1 COLLATE NOCASE)"; + comp = " WHERE (p.name = ?1 COLLATE NOCASE OR " + "p.name || '-' || version = ?1 COLLATE NOCASE)"; else - comp = " WHERE origin = ?1 COLLATE NOCASE"; + comp = " WHERE (origin = ?1 COLLATE NOCASE OR categories.name || substr(origin, instr(origin, '/')) = ?1 COLLATE NOCASE)"; } else { - comp = " WHERE name = ?1 COLLATE NOCASE"; + comp = " WHERE p.name = ?1 COLLATE NOCASE"; } } break; case MATCH_GLOB: if (checkuid == NULL) { if (checkorigin == NULL) - comp = " WHERE (name GLOB ?1 " - "OR name || '-' || version GLOB ?1)"; + comp = " WHERE (p.name GLOB ?1 " + "OR p.name || '-' || version GLOB ?1)"; else - comp = " WHERE origin GLOB ?1"; + comp = " WHERE (origin GLOB ?1 OR categories.name || substr(origin, instr(origin, '/')) GLOB ?1)"; } else { - comp = " WHERE name = ?1"; + comp = " WHERE p.name = ?1"; } break; case MATCH_REGEX: if (checkuid == NULL) { if (checkorigin == NULL) - comp = " WHERE (name REGEXP ?1 " - "OR name || '-' || version REGEXP ?1)"; + comp = " WHERE (p.name REGEXP ?1 " + "OR p.name || '-' || version REGEXP ?1)"; else - comp = " WHERE origin REGEXP ?1"; + comp = " WHERE (origin REGEXP ?1 OR categories.name || substr(origin, instr(origin, '/'))e REGEXP ?1)"; } else { - comp = " WHERE name = ?1"; + comp = " WHERE p.name = ?1"; } break; } @@ -140,24 +140,29 @@ pkgdb_query_cond(struct pkgdb *db, const char *cond, const char *pattern, match_ if (cond) sqlite3_snprintf(sizeof(sql), sql, - "SELECT id, origin, name, name as uniqueid, " + "SELECT DISTINCT p.id, origin, p.name, p.name as uniqueid, " "version, comment, desc, " "message, arch, maintainer, www, " "prefix, flatsize, licenselogic, automatic, " "locked, time, manifestdigest, vital " - "FROM packages AS p%s %s (%s) ORDER BY p.name;", + "FROM packages AS p " + "LEFT JOIN pkg_categories ON p.id = pkg_categories.package_id " + "LEFT JOIN categories ON categories.id = pkg_categories.category_id " + " %s %s (%s) ORDER BY p.name;", comp, pattern == NULL ? "WHERE" : "AND", cond + 7); else sqlite3_snprintf(sizeof(sql), sql, - "SELECT id, origin, name, name as uniqueid, " + "SELECT DISTINCT p.id, origin, p.name, p.name as uniqueid, " "version, comment, desc, " "message, arch, maintainer, www, " "prefix, flatsize, licenselogic, automatic, " "locked, time, manifestdigest, vital " - "FROM packages AS p%s " - "ORDER BY p.name;", comp); + "FROM packages AS p " + "LEFT JOIN pkg_categories ON p.id = pkg_categories.package_id " + "LEFT JOIN categories ON categories.id = pkg_categories.category_id " + "%s" + " ORDER BY p.name", comp); - pkg_debug(4, "Pkgdb: running '%s'", sql); if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) { ERROR_SQLITE(db->sqlite, sql); return (NULL); @@ -165,6 +170,7 @@ pkgdb_query_cond(struct pkgdb *db, const char *cond, const char *pattern, match_ if (match != MATCH_ALL) sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT); + pkg_debug(4, "Pkgdb: running '%s'", sqlite3_expanded_sql(stmt)); return (pkgdb_it_new_sqlite(db, stmt, PKG_INSTALLED, PKGDB_IT_FLAG_ONCE)); } diff --git a/libpkg/repo/binary/query.c b/libpkg/repo/binary/query.c index 49d9fda78f..8d7e3b4d5f 100644 --- a/libpkg/repo/binary/query.c +++ b/libpkg/repo/binary/query.c @@ -103,13 +103,15 @@ pkg_repo_binary_query(struct pkg_repo *repo, const char *cond, const char *patte char *sql = NULL; const char *comp = NULL; char basesql[] = "" - "SELECT id, origin, name, name as uniqueid, version, comment, " + "SELECT DISTINCT p.id, origin, p.name, p.name as uniqueid, version, comment, " "prefix, desc, arch, maintainer, www, " "licenselogic, flatsize, pkgsize, " "cksum, manifestdigest, path AS repopath, '%s' AS dbname " - "FROM packages AS p %s " + "FROM packages as p " + "LEFT JOIN pkg_categories ON p.id = pkg_categories.package_id " + "LEFT JOIN categories ON categories.id = pkg_categories.category_id %s" "%s%s%s " - "ORDER BY NAME;"; + "ORDER BY p.name;"; if (match != MATCH_ALL && (pattern == NULL || pattern[0] == '\0')) return (NULL); @@ -121,7 +123,7 @@ pkg_repo_binary_query(struct pkg_repo *repo, const char *cond, const char *patte xasprintf(&sql, basesql, repo->name, comp, "", "", ""); else xasprintf(&sql, basesql, repo->name, comp, - comp[0] != '\0' ? "AND (" : "WHERE (", cond + 7, ")"); + comp[0] != '\0' ? "AND (" : "WHERE ( ", cond + 7, " )"); stmt = prepare_sql(sqlite, sql); free(sql); @@ -294,13 +296,13 @@ pkg_repo_binary_build_search_query(xstring *sql, match_t match, what = NULL; break; case FIELD_ORIGIN: - what = "origin"; + what = "categories.name || substr(origin, instr(origin, '/')) as mycat"; break; case FIELD_NAME: - what = "name"; + what = "p.name"; break; case FIELD_NAMEVER: - what = "name || '-' || version"; + what = "p.name || '-' || version"; break; case FIELD_COMMENT: what = "comment"; @@ -318,13 +320,13 @@ pkg_repo_binary_build_search_query(xstring *sql, match_t match, orderby = NULL; break; case FIELD_ORIGIN: - orderby = " ORDER BY origin"; + orderby = " ORDER BY mycat"; break; case FIELD_NAME: - orderby = " ORDER BY name"; + orderby = " ORDER BY p.name"; break; case FIELD_NAMEVER: - orderby = " ORDER BY name, version"; + orderby = " ORDER BY p.name, version"; break; case FIELD_COMMENT: orderby = " ORDER BY comment"; @@ -349,11 +351,13 @@ pkg_repo_binary_search(struct pkg_repo *repo, const char *pattern, match_t match xstring *sql = NULL; char *sqlcmd = NULL; const char *multireposql = "" - "SELECT id, origin, name, version, comment, " + "SELECT DISTINCT p.id, origin, p.name, version, comment, " "prefix, desc, arch, maintainer, www, " "licenselogic, flatsize, pkgsize, " "cksum, path AS repopath, '%1$s' AS dbname, '%2$s' AS repourl " - "FROM packages "; + "FROM packages as p " + "LEFT JOIN pkg_categories ON p.id = pkg_categories.package_id " + "LEFT JOIN categories ON categories.id = pkg_categories.category_id "; if (pattern == NULL || pattern[0] == '\0') return (NULL); diff --git a/src/query.c b/src/query.c index 1af23230d3..8e82d3a18f 100644 --- a/src/query.c +++ b/src/query.c @@ -483,7 +483,7 @@ format_sql_condition(const char *str, xstring *sqlcond, bool for_remote) str++; switch (str[0]) { case 'n': - fprintf(sqlcond->fp, "name"); + fprintf(sqlcond->fp, "p.name"); state = OPERATOR_STRING; break; case 'o':