Skip to content

Commit

Permalink
query: accept every categories as A when looking for "<A>/<B>"
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed May 25, 2022
1 parent b0b3231 commit ade813b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion libpkg/pkg_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 28 additions & 22 deletions libpkg/pkgdb_query.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2011-2012 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2022 Baptiste Daroussin <[email protected]>
* Copyright (c) 2011-2012 Julien Laffaye <[email protected]>
* Copyright (c) 2011 Will Andrews <[email protected]>
* Copyright (c) 2011 Philippe Pepiot <[email protected]>
Expand Down Expand Up @@ -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;
}
Expand All @@ -140,31 +140,37 @@ 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);
}

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));
}
Expand Down
28 changes: 16 additions & 12 deletions libpkg/repo/binary/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit ade813b

Please sign in to comment.