From: Nathan Wagner Date: Thu, 1 Nov 2018 13:00:06 +0000 (+0000) Subject: add where clause argument to findpkg X-Git-Tag: v0.2.16~48 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=30aa9bfe1e9a0e682f7c942b8e68054551f14857 add where clause argument to findpkg --- diff --git a/lib/findpkg.c b/lib/findpkg.c index 072f9f1..cbf7004 100644 --- a/lib/findpkg.c +++ b/lib/findpkg.c @@ -34,29 +34,13 @@ void zpm_sqlite_error(struct zpm *zpm) { zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db)); } -char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { +char *zpm_findpkg(struct zpm *zpm, char *pkgstr, char *where) { char *select = "select pkgid, package, version, release from packages_pkgid"; char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp"; -#if 0 - char *sstr[] = { - "status = 'installed'", - "", - "status != 'installed'" - }; -#endif // char *order = "order by package, version collate vercmp, cast(release as integer)"; sqlite3_str *sql; - sqlite3_stmt *stmt; char *query, *pkgid = 0; - char package[32]; - char version[32]; - int release; - - if (pkgstr == NULL) { - fprintf(stderr, "trying to parse null string\n"); - return NULL; - } /* null pkgstr find "best" package * best is shortest complete package if any are complete @@ -65,29 +49,40 @@ char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { * latest as determined by vercmp */ - /* given a package name, get the packages */ - /* no package name, get all */ - - // char where[1024] = ""; - - zpm_parse_package(pkgstr, package, version, &release); - - /* install a collation function */ - zpm_addvercmp(zpm); - - /* TODO allow more args to nail down version and release */ - sql = sqlite3_str_new(zpm->db); + sqlite3_str_appendall(sql, select); - sqlite3_str_appendf(sql, " where package = %Q", package); - if (*version) { - sqlite3_str_appendf(sql, " and version = %Q", version); + sqlite3_str_appendall(sql, " where true"); + + if (pkgstr) { + int release; + char version[32]; + char package[32]; + + zpm_parse_package(pkgstr, package, version, &release); + if (*package != 0) { + sqlite3_str_appendf(sql, " and package = %Q", + package); + } + if (*version != 0) { + sqlite3_str_appendf(sql, " and version = %Q", + version); + } + if (release != 0) { + sqlite3_str_appendf(sql, " and release = %d", + release); + } } - if (release) { - sqlite3_str_appendf(sql, " and release = %d", release); + + if (where) { + sqlite3_str_appendall(sql, " and "); + sqlite3_str_appendall(sql, where); } - sqlite3_str_appendf(sql, " %s", group); - sqlite3_str_appendf(sql, " limit 1"); + + + sqlite3_str_appendall(sql, " "); + sqlite3_str_appendall(sql, group); + sqlite3_str_appendall(sql, " limit 1;"); if (sqlite3_str_errcode(sql)) { zpm->error = 1; @@ -96,28 +91,10 @@ char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { } query = sqlite3_str_finish(sql); - sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL); - sqlite3_free(query); -#if 0 - if (zpm->pkgid) { - free(zpm->pkgid); - } - zpm->pkgid = 0; -#endif - - switch (sqlite3_step(stmt)) { - case SQLITE_ROW: - pkgid = strdup((const char *)sqlite3_column_text(stmt, 0)); - break; - case SQLITE_DONE: - /* not found */ - break; - default: - zpm_sqlite_error(zpm); - break; - } - sqlite3_finalize(stmt); + pkgid = zpm_db_string(zpm, query);; + + sqlite3_free(query); return pkgid; } diff --git a/lib/foreach_path.c b/lib/foreach_path.c index 4afc122..561e24f 100644 --- a/lib/foreach_path.c +++ b/lib/foreach_path.c @@ -31,6 +31,39 @@ void *data, char **errmsg) { } + sql = sqlite3_str_value(s); + if (!sql) { + sqlite3_str_finish(s); + zpm->error = 1; + return 0; + } + + zpm_exec(zpm, sql, callback, data, errmsg); + sqlite3_str_finish(s); + if (*errmsg) { + fprintf(stderr, "errmsg: %s\n", *errmsg); + zpm->error = 2; + return 0; + } + + return 1; +} + +int zpm_foreach_package(struct zpm *zpm, char *where, +int (*callback)(void *cbdata, int ncols, char **vals, char **cols), +void *data, char **errmsg) { + char *sql; + sqlite3_str *s; + + if (!zpm || zpm->error || !callback) return 0; + + s = sqlite3_str_new(zpm->db); + sqlite3_str_appendall(s, "select * from packages_pkgid"); + + if (where) { + sqlite3_str_appendf(s, " where %s", where); + } + sql = sqlite3_str_value(s); if (!sql) { sqlite3_str_finish(s); diff --git a/lib/script_hash.c b/lib/script_hash.c index 2185f77..ebca550 100644 --- a/lib/script_hash.c +++ b/lib/script_hash.c @@ -35,7 +35,7 @@ int zpm_script_hash(struct zpm *zpm, char *pkgstr, char *phase, char *hash) { char *template = "select hash from scripts_pkgid where pkgid = %Q and stage = %Q"; sqlite3_stmt *st; - pkgid = zpm_findpkg(zpm, pkgstr); + pkgid = zpm_findpkg(zpm, pkgstr, NULL); st = zpm_dbquery(zpm, template, pkgid, phase); diff --git a/zpm-findpkg.c b/zpm-findpkg.c index 3fd608c..2ce88f6 100644 --- a/zpm-findpkg.c +++ b/zpm-findpkg.c @@ -7,79 +7,14 @@ #include "zpm.h" -static int found = 0; - void usage(void) { fprintf(stderr, "zpm-findpkg [-I] [-s ...] [-S ] [package]\n"); } -static int prow(void *f, int ncols, char **vals, char **cols) { - FILE *out = f; - int i; - - if (cols == 0) { - fprintf(stderr, "sqlite can't get column names\n"); - } - for (i=3;i3) fprintf(out, "\t"); - fprintf(out, "%s", vals[i]); - } - fprintf(out, "\n"); - found++; - return 0; -} - -void parse_package(char *pstr, char *name, char *ver, int *rel) { - if (name) *name = 0; - if (ver) *ver = 0; - if (rel) *rel = -1; - - /* string - ver - rel */ - /* rel is all digits */ - /* possible forms: - * ^(.+)-([0-9][^-]*)-([\d+])$ - * ^(.+)-([0-9][^-]*)$ - * ^(.+)$ - * The main problem in parsing is that the package name itself - * can contain a '-', so you can't just split on '-' - * Also, the version can be just digits. - */ - - /* everything up to the first '-' is in the name */ - while (*pstr) { - if (*pstr == '-' && isdigit(*(pstr+1))) { - break; - } - if (name) { - *name++ = *pstr; - } - pstr++; - } - if (name) *name = 0; - if (*pstr == '-') { - pstr++; - } - while (*pstr && *pstr != '-') { - if (ver) { - *ver++ = *pstr; - } - pstr++; - } - if (ver) *ver = 0; - if (*pstr == '-') { - pstr++; - } - if (rel && *pstr) { - *rel = atoi(pstr); - } -} - int main(int ac, char **av){ - int opt; - struct zpm pkg; - char *dbfile, *pkgstr; - char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages"; - char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp"; + int opt, argn; + struct zpm zpm; + char *dbfile = 0, *pkgstr = 0, *pkgid = 0; char *sql; sqlite3_str *include; @@ -88,7 +23,6 @@ int main(int ac, char **av){ dbfile = getenv("ZPMDB"); - query = sqlite3_str_new(NULL); include = sqlite3_str_new(NULL); exclude = sqlite3_str_new(NULL); @@ -107,74 +41,43 @@ int main(int ac, char **av){ break; } } - int argn = optind; + argn = optind; if (!dbfile) { fprintf(stderr, "must specify db\n"); return 1; } - /* given a package name, get the packages */ - /* no package name, get all */ - -#ifdef DEBUG - fprintf(stderr, "opening db %s\n", dbfile); -#endif + query = sqlite3_str_new(NULL); - if (zpm_open(&pkg, dbfile)) { - char *errmsg; + if (zpm_open(&zpm, dbfile)) { char *excludes, *includes; excludes = sqlite3_str_value(exclude); includes = sqlite3_str_value(include); - sqlite3_str_appendall(query, " "); - sqlite3_str_appendall(query, select); - sqlite3_str_appendall(query, " where true"); - if (includes) { - sqlite3_str_appendf(query, " and status in (%s)", includes+1); + sqlite3_str_appendf(query, "status in (%s)", includes+1); } if (excludes) { - sqlite3_str_appendf(query, " and status not in (%s)", excludes+1); + sqlite3_str_appendf(query, "%sstatus not in (%s)", + includes ? " and " : "", + excludes+1); } if (ac >= argn) { - int release; - char version[32]; - char package[32]; - pkgstr = av[argn]; - - parse_package(pkgstr, package, version, &release); - if (*package != 0) { - sqlite3_str_appendf(query, " and package = %Q", - package); - } - if (*version != 0) { - sqlite3_str_appendf(query, " and version = %Q", - version); - } - if (release != -1) { - sqlite3_str_appendf(query, " and release = %d", - release); - } } - sqlite3_str_appendall(query, " "); - sqlite3_str_appendall(query, group); - sqlite3_str_appendall(query, ";"); - sql = sqlite3_str_value(query); - if (sql) { -#ifdef DEBUG - fprintf(stderr, "q: %s\n", sql); -#endif - zpm_exec(&pkg, sql, prow, stdout, &errmsg); - } else { - fprintf(stderr, "unable to form database query\n"); + + pkgid = zpm_findpkg(&zpm, pkgstr, sql); + + sqlite3_free(sql); + if (pkgid) { + printf("%s\n", pkgid); } + zpm_close(&zpm); } - zpm_close(&pkg); - return found ? 0 : 1; + return pkgid ? 0 : 1; } diff --git a/zpm-script.c b/zpm-script.c index 438cf50..ceca239 100644 --- a/zpm-script.c +++ b/zpm-script.c @@ -202,7 +202,7 @@ int main(int ac, char **av){ /* first non option arg is always a package id */ pkgstr = av[argn]; - pkgid = zpm_findpkg(&zpm, pkgstr); + pkgid = zpm_findpkg(&zpm, pkgstr, NULL); if (!pkgid) { fprintf(stderr, "no package for %s\n", pkgstr); diff --git a/zpm.h b/zpm.h index f9e7b33..adc69bb 100644 --- a/zpm.h +++ b/zpm.h @@ -76,7 +76,7 @@ struct zpm_package { }; int zpm_parse_package(char *pstr, char *name, char *ver, int *rel); -char *zpm_findpkg(struct zpm *zpm, char *pkgstr); +char *zpm_findpkg(struct zpm *zpm, char *pkgstr, char *where); int zpm_quote(char *value, char *dest, size_t n); struct zpm_file { @@ -174,6 +174,9 @@ int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char ** int zpm_foreach_path(struct zpm *zpm, char *pkgid, char *where, int (*callback)(void *f, int ncols, char **vals, char **cols), void *data, char **errmsg); +int zpm_foreach_package(struct zpm *zpm, char *where, +int (*callback)(void *cbdata, int ncols, char **vals, char **cols), +void *data, char **errmsg); int zpm_script_hash(struct zpm *zpm, char *pkgstr, char *phase, char *hash); int zpm_script_set(struct zpm *zpm, char *pkgstr, char *phase, char *hash);