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
* 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;
}
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;
}
}
+ 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);
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);
#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;
dbfile = getenv("ZPMDB");
- query = sqlite3_str_new(NULL);
include = sqlite3_str_new(NULL);
exclude = sqlite3_str_new(NULL);
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;
}
/* 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);
};
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 {
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);