]> pd.if.org Git - zpackage/commitdiff
add where filter argument to foreach-path
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 12 Oct 2018 11:07:49 +0000 (11:07 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 12 Oct 2018 11:07:49 +0000 (11:07 +0000)
lib/foreach_path.c
zpm-foreach-path.c
zpm.h

index b73739b4887f3d9db6fc6b026ecc82ea1b058264..4afc122e1d4370861a3bde8cbf34bd2c5036640c 100644 (file)
@@ -9,31 +9,42 @@
 #include "sqlite3.h"
 #include "zpm.h"
 
-int zpm_foreach_path(struct zpm *zpm, char *pkgid, 
+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) {
+       char *sql;
+       sqlite3_str *s;
 
-       char *files = "select * from packagefiles where"
-               " printf('%%s-%%s-%%s', package, version, release) = %Q"
-               " order by path"
-               ;
+       if (!zpm || zpm->error || !callback) return 0;
 
-       char *sql;
+       s = sqlite3_str_new(zpm->db);
+       sqlite3_str_appendall(s, "select * from packagefiles_pkgid where ");
+
+       if (where) {
+               sqlite3_str_appendf(s, "%s", where);
+       } else {
+               sqlite3_str_appendall(s, "true");
+       }
+
+       if (pkgid) {
+               sqlite3_str_appendf(s, " and printf('%%s-%%s-%%s', package, version, release) = %Q", pkgid);
+       }
 
-       if (!zpm || zpm->error || !pkgid || !callback) return 0;
 
-       sql = sqlite3_mprintf(files, pkgid);
+       sql = sqlite3_str_value(s);
        if (!sql) {
-               return 0;
+               sqlite3_str_finish(s);
                zpm->error = 1;
+               return 0;
        }
 
        zpm_exec(zpm, sql, callback, data, errmsg);
-       if (errmsg) {
+       sqlite3_str_finish(s);
+       if (*errmsg) {
+               fprintf(stderr, "errmsg: %s\n", *errmsg);
                zpm->error = 2;
                return 0;
        }
-       sqlite3_free(sql);
 
        return 1;
 }
index 2403576f4941d12d4f72f4b22b361f4bb941734f..cad19282b603b0eb619867df1b10291d77f959c8 100644 (file)
@@ -267,7 +267,7 @@ int main(int ac, char **av){
        /* install a collation function */
        // zpm_addvercmp(&pkg);
 
-       if (!zpm_foreach_path(&pkg, conf.pkgid, conf.callback, &conf, &errmsg)) {
+       if (!zpm_foreach_path(&pkg, conf.pkgid, 0, conf.callback, &conf, &errmsg)) {
                if (errmsg) {
                        fprintf(stderr, "database error: %s\n", errmsg);
                        exit(EXIT_FAILURE);
diff --git a/zpm.h b/zpm.h
index f3e37346ad214ec0fe39881bc1ef4e841efd31d7..1617619f227328a4b689f4a4b5b867e9f4f11b4a 100644 (file)
--- a/zpm.h
+++ b/zpm.h
@@ -171,7 +171,7 @@ int zpm_addvercmp(struct zpm *pkg);
 
 int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg);
 
-int zpm_foreach_path(struct zpm *zpm, char *pkgid, 
+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);