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) {
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";
42 "status = 'installed'",
44 "status != 'installed'"
48 // char *order = "order by package, version collate vercmp, cast(release as integer)";
51 char *query, *pkgid = 0;
57 fprintf(stderr, "trying to parse null string\n");
61 /* null pkgstr find "best" package
62 * best is shortest complete package if any are complete
63 * shortest incomplete if any are incomplete
64 * where more than one version is in those sets, best is
65 * latest as determined by vercmp
68 /* given a package name, get the packages */
69 /* no package name, get all */
71 // char where[1024] = "";
73 zpm_parse_package(pkgstr, package, version, &release);
75 /* install a collation function */
78 /* TODO allow more args to nail down version and release */
80 sql = sqlite3_str_new(zpm->db);
81 sqlite3_str_appendall(sql, select);
82 sqlite3_str_appendf(sql, " where package = %Q", package);
84 sqlite3_str_appendf(sql, " and version = %Q", version);
87 sqlite3_str_appendf(sql, " and release = %d", release);
89 sqlite3_str_appendf(sql, " %s", group);
90 sqlite3_str_appendf(sql, " limit 1");
92 if (sqlite3_str_errcode(sql)) {
94 sqlite3_free(sqlite3_str_finish(sql));
98 query = sqlite3_str_finish(sql);
99 sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL);
109 switch (sqlite3_step(stmt)) {
111 pkgid = strdup((const char *)sqlite3_column_text(stmt, 0));
117 zpm_sqlite_error(zpm);
120 sqlite3_finalize(stmt);