Skip to content

Commit

Permalink
pkg-rquery: Add -e <condition> support
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrewery committed Apr 19, 2012
1 parent a1baa33 commit fc79904
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 74 deletions.
7 changes: 5 additions & 2 deletions libpkg/pkg.h
Expand Up @@ -102,7 +102,11 @@ typedef enum {
/**
* The argument is an extended regular expression.
*/
MATCH_EREGEX
MATCH_EREGEX,
/**
* The argument is a WHERE clause to use as condition
*/
MATCH_CONDITION
} match_t;

/**
Expand Down Expand Up @@ -641,7 +645,6 @@ int pkgdb_unregister_pkg(struct pkgdb *pkg, const char *origin);
struct pkgdb_it * pkgdb_query(struct pkgdb *db, const char *pattern,
match_t type);
struct pkgdb_it * pkgdb_rquery(struct pkgdb *db, const char *pattern, match_t type, const char *reponame);
struct pkgdb_it * pkgdb_query_condition(struct pkgdb *db, const char *condition);
struct pkgdb_it * pkgdb_search(struct pkgdb *db, const char *pattern,
match_t type, unsigned int field, const char *reponame);

Expand Down
55 changes: 19 additions & 36 deletions libpkg/pkgdb.c
Expand Up @@ -852,6 +852,9 @@ pkgdb_get_pattern_query(const char *pattern, match_t match)
else
comp = " WHERE EREGEXP(?1, origin)";
break;
case MATCH_CONDITION:
comp = pattern;
break;
}

return (comp);
Expand All @@ -878,6 +881,10 @@ pkgdb_get_match_how(match_t match)
case MATCH_EREGEX:
how = "EREGEXP(?1, %s)";
break;
case MATCH_CONDITION:
/* This case should not be called by pkgdb_get_match_how() */
assert(0);
break;
}

return (how);
Expand All @@ -891,7 +898,7 @@ pkgdb_query(struct pkgdb *db, const char *pattern, match_t match)
const char *comp = NULL;

assert(db != NULL);
assert(match == MATCH_ALL || pattern != NULL);
assert(match == MATCH_ALL || (pattern != NULL && pattern[0] != '\0'));

comp = pkgdb_get_pattern_query(pattern, match);

Expand All @@ -908,37 +915,12 @@ pkgdb_query(struct pkgdb *db, const char *pattern, match_t match)
return (NULL);
}

if (match != MATCH_ALL)
if (match != MATCH_ALL && match != MATCH_CONDITION)
sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT);

return (pkgdb_it_new(db, stmt, PKG_INSTALLED));
}

struct pkgdb_it *
pkgdb_query_condition(struct pkgdb *db, const char *condition)
{
char sql[BUFSIZ];
sqlite3_stmt *stmt;

assert(db != NULL);
assert (condition != NULL);

snprintf(sql, sizeof(sql),
"SELECT id, origin, name, version, comment, desc, "
"message, arch, maintainer, www, "
"prefix, flatsize, licenselogic, automatic "
"time, infos "
"FROM packages AS p WHERE %s "
"ORDER BY p.name;", condition);

if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) {
ERROR_SQLITE(db->sqlite);
return (NULL);
}

return (pkgdb_it_new(db, stmt, PKG_INSTALLED));
}

struct pkgdb_it *
pkgdb_query_which(struct pkgdb *db, const char *path)
{
Expand Down Expand Up @@ -2682,7 +2664,12 @@ pkgdb_rquery(struct pkgdb *db, const char *pattern, match_t match, const char *r
bool multirepos_enabled = false;
const char *reponame = NULL;
const char *comp = NULL;
char basesql[BUFSIZ];
char basesql[BUFSIZ] = ""
"SELECT id, origin, name, version, comment, "
"prefix, desc, arch, maintainer, www, "
"licenselogic, flatsize AS newflatsize, pkgsize, "
"cksum, path AS repopath, '%s' AS dbname "
"FROM '%s'.packages p";

assert(db != NULL);
assert(match == MATCH_ALL || (pattern != NULL && pattern[0] != '\0'));
Expand All @@ -2693,13 +2680,9 @@ pkgdb_rquery(struct pkgdb *db, const char *pattern, match_t match, const char *r

sql = sbuf_new_auto();
comp = pkgdb_get_pattern_query(pattern, match);
snprintf(basesql, BUFSIZ,
"SELECT id, origin, name, version, comment, "
"prefix, desc, arch, maintainer, www, "
"licenselogic, flatsize AS newflatsize, pkgsize, "
"cksum, path AS repopath, '%%s' AS dbname "
"FROM '%%s'.packages p %s ",
comp);
if (comp && comp[0]) {
strlcat(basesql, comp, sizeof(basesql));
}

pkg_config_bool(PKG_CONFIG_MULTIREPOS, &multirepos_enabled);

Expand All @@ -2724,7 +2707,7 @@ pkgdb_rquery(struct pkgdb *db, const char *pattern, match_t match, const char *r

sbuf_delete(sql);

if (match != MATCH_ALL)
if (match != MATCH_ALL && match != MATCH_CONDITION)
sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT);

return (pkgdb_it_new(db, stmt, PKG_REMOTE));
Expand Down
4 changes: 4 additions & 0 deletions pkg/pkg-rquery.8
Expand Up @@ -43,6 +43,10 @@ The following options are supported by
.Bl -tag -width F1
.It Fl a
Match all packages from the database
.It Fl e
Match packages using the given
.Ar evaluation-condition.
See EVALUATION FORMAT for details.
.It Fl r Ar reponame
Fetches packages from the given
.Ar reponame
Expand Down
26 changes: 8 additions & 18 deletions pkg/query.c
Expand Up @@ -385,6 +385,7 @@ int
format_sql_condition(const char *str, struct sbuf *sqlcond)
{
type_t state = NONE;
sbuf_cat(sqlcond, " WHERE ");
while (str[0] != '\0') {
if (str[0] == ';') {
fprintf(stderr, "';' is forbidden in evaluation format");
Expand Down Expand Up @@ -695,6 +696,7 @@ exec_query(int argc, char **argv)
pkgname = optarg;
break;
case 'e':
match = MATCH_CONDITION;
condition = optarg;
break;
default:
Expand Down Expand Up @@ -733,6 +735,7 @@ exec_query(int argc, char **argv)
sqlcond = sbuf_new_auto();
if (format_sql_condition(condition, sqlcond) != EPKG_OK)
return (EX_USAGE);
sbuf_finish(sqlcond);
}

ret = pkgdb_open(&db, PKGDB_DEFAULT);
Expand All @@ -747,24 +750,11 @@ exec_query(int argc, char **argv)
if (ret != EPKG_OK)
return (EX_IOERR);

if (condition != NULL) {
sbuf_finish(sqlcond);
if ((it = pkgdb_query_condition(db, sbuf_data(sqlcond))) == NULL)
return (EX_IOERR);

while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK)
print_query(pkg, argv[0], multiline);

pkgdb_it_free(it);

if (ret != EPKG_END)
return (EX_SOFTWARE);

return (EXIT_SUCCESS);
}

if (match == MATCH_ALL) {
if ((it = pkgdb_query(db, NULL, match)) == NULL)
if (match == MATCH_ALL || match == MATCH_CONDITION) {
const char *condition_sql = NULL;
if (match == MATCH_CONDITION && sqlcond)
condition_sql = sbuf_data(sqlcond);
if ((it = pkgdb_query(db, condition_sql, match)) == NULL)
return (EX_IOERR);

while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK)
Expand Down
25 changes: 7 additions & 18 deletions pkg/rquery.c
Expand Up @@ -107,6 +107,7 @@ exec_rquery(int argc, char **argv)
match = MATCH_EREGEX;
break;
case 'e':
match = MATCH_CONDITION;
condition = optarg;
break;
case 'r':
Expand Down Expand Up @@ -138,6 +139,7 @@ exec_rquery(int argc, char **argv)
sqlcond = sbuf_new_auto();
if (format_sql_condition(condition, sqlcond) != EPKG_OK)
return (EX_USAGE);
sbuf_finish(sqlcond);
}

ret = pkgdb_open(&db, PKGDB_REMOTE);
Expand All @@ -152,24 +154,11 @@ exec_rquery(int argc, char **argv)
if (ret != EPKG_OK)
return (EX_IOERR);

if (condition != NULL) {
sbuf_finish(sqlcond);
if ((it = pkgdb_query_condition(db, sbuf_data(sqlcond))) == NULL)
return (EX_IOERR);

while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK)
print_query(pkg, argv[0], multiline);

pkgdb_it_free(it);

if (ret != EPKG_END)
return (EX_SOFTWARE);

return (EXIT_SUCCESS);
}

if (match == MATCH_ALL) {
if ((it = pkgdb_rquery(db, NULL, match, reponame)) == NULL)
if (match == MATCH_ALL || match == MATCH_CONDITION) {
const char *condition_sql = NULL;
if (match == MATCH_CONDITION && sqlcond)
condition_sql = sbuf_data(sqlcond);
if ((it = pkgdb_rquery(db, condition_sql, match, reponame)) == NULL)
return (EX_IOERR);

while ((ret = pkgdb_it_next(it, &pkg, query_flags)) == EPKG_OK)
Expand Down

0 comments on commit fc79904

Please sign in to comment.