]> pd.if.org Git - zpackage/commitdiff
add where clause argument to findpkg
authorNathan Wagner <nw@hydaspes.if.org>
Thu, 1 Nov 2018 13:00:06 +0000 (13:00 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 3 Nov 2018 12:39:53 +0000 (12:39 +0000)
lib/findpkg.c
lib/foreach_path.c
lib/script_hash.c
zpm-findpkg.c
zpm-script.c
zpm.h

index 072f9f1cbd21745259eeb1c0b59e412424bd490a..cbf70042c413baa4671a36005b83f6987d1459f3 100644 (file)
@@ -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;
 }
index 4afc122e1d4370861a3bde8cbf34bd2c5036640c..561e24f089b1674e88de4991b2a8e1ddd919f213 100644 (file)
@@ -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);
index 2185f77cae20a4dc22ac6ce5f06bf2cda0fac4fe..ebca5508d4c65f80fc335c1d20b51ee8fa89173e 100644 (file)
@@ -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);
 
index 3fd608c56327f5ed1653fb6fbbbef31811b18c79..2ce88f67b3b6d50af29dc70a4540e3f98e0cf42e 100644 (file)
@@ -7,79 +7,14 @@
 
 #include "zpm.h"
 
-static int found = 0;
-
 void usage(void) {
        fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [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;i<ncols;i++) {
-               if (i>3) 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;
 }
index 438cf50099a8e99d243e29dcb107dd95ad8ec9df..ceca239530873d7036c7549db8afa1cb0d6cd7dc 100644 (file)
@@ -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 f9e7b3391a8d725094e8127de6ba9f55a64c98d3..adc69bba4376d03459d656ac70bf5a05d20e1c7e 100644 (file)
--- 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);