]> pd.if.org Git - zpackage/blob - lib/findpkg.c
add where clause argument to findpkg
[zpackage] / lib / findpkg.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <ctype.h>
7
8 #include "zpm.h"
9
10 #include "sqlite3.h"
11
12 #define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
13
14 /*
15  * flags 0 = find installed
16  * flags 1 = skip installed
17  * flags 2 = skip not installed
18  */
19
20 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
21         struct zpm_package *pkg;
22
23         pkg = malloc(sizeof *pkg);
24         if (pkg) {
25                 pkg->zpm = zpm;
26                 pkg->release = 0;
27         }
28         return pkg;
29 }
30
31 void zpm_sqlite_error(struct zpm *zpm) {
32         zpm->error = 1;
33         if (zpm->errmsg) free(zpm->errmsg);
34         zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db));
35 }
36
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";
40
41 //      char *order = "order by package, version collate vercmp, cast(release as integer)";
42         sqlite3_str *sql;
43         char *query, *pkgid = 0;
44
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
50          */
51
52         sql = sqlite3_str_new(zpm->db);
53
54         sqlite3_str_appendall(sql, select);
55         sqlite3_str_appendall(sql, " where true");
56
57         if (pkgstr) {
58                 int release;
59                 char version[32];
60                 char package[32];
61
62                 zpm_parse_package(pkgstr, package, version, &release);
63                 if (*package != 0) {
64                         sqlite3_str_appendf(sql, " and package = %Q",
65                                         package);
66                 }
67                 if (*version != 0) {
68                         sqlite3_str_appendf(sql, " and version = %Q",
69                                         version);
70                 }
71                 if (release != 0) {
72                         sqlite3_str_appendf(sql, " and release = %d",
73                                         release);
74                 }
75         }
76
77         if (where) {
78                 sqlite3_str_appendall(sql, " and ");
79                 sqlite3_str_appendall(sql, where);
80         }
81
82
83         sqlite3_str_appendall(sql, " ");
84         sqlite3_str_appendall(sql, group);
85         sqlite3_str_appendall(sql, " limit 1;");
86
87         if (sqlite3_str_errcode(sql)) {
88                 zpm->error = 1;
89                 sqlite3_free(sqlite3_str_finish(sql));
90                 return 0;
91         }
92
93         query = sqlite3_str_finish(sql);
94
95         pkgid = zpm_db_string(zpm, query);;
96
97         sqlite3_free(query);
98
99         return pkgid;
100 }