1 #define _POSIX_C_SOURCE 200809L
12 #define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
15 * flags 0 = find installed
16 * flags 1 = skip installed
17 * flags 2 = skip not installed
20 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
21 struct zpm_package *pkg;
23 pkg = malloc(sizeof *pkg);
31 void zpm_sqlite_error(struct zpm *zpm) {
33 if (zpm->errmsg) free(zpm->errmsg);
34 zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db));
37 char *zpm_findpkg(struct zpm *zpm, char *pkgstr, char *where) {
38 char *select = "select pkgid, package, version, release from packages_pkgid";
39 char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp";
41 // char *order = "order by package, version collate vercmp, cast(release as integer)";
43 char *query, *pkgid = 0;
45 /* null pkgstr find "best" package
46 * best is shortest complete package if any are complete
47 * shortest incomplete if any are incomplete
48 * where more than one version is in those sets, best is
49 * latest as determined by vercmp
52 sql = sqlite3_str_new(zpm->db);
54 sqlite3_str_appendall(sql, select);
55 sqlite3_str_appendall(sql, " where true");
62 zpm_parse_package(pkgstr, package, version, &release);
64 sqlite3_str_appendf(sql, " and package = %Q",
68 sqlite3_str_appendf(sql, " and version = %Q",
72 sqlite3_str_appendf(sql, " and release = %d",
78 sqlite3_str_appendall(sql, " and ");
79 sqlite3_str_appendall(sql, where);
83 sqlite3_str_appendall(sql, " ");
84 sqlite3_str_appendall(sql, group);
85 sqlite3_str_appendall(sql, " limit 1;");
87 if (sqlite3_str_errcode(sql)) {
89 sqlite3_free(sqlite3_str_finish(sql));
93 query = sqlite3_str_finish(sql);
95 pkgid = zpm_db_string(zpm, query);;