]> pd.if.org Git - zpackage/commitdiff
add functions to find scripts and run a query
authorNathan Wagner <nw@hydaspes.if.org>
Sun, 16 Sep 2018 13:16:30 +0000 (13:16 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Mon, 17 Sep 2018 12:16:13 +0000 (12:16 +0000)
lib/dbquery.c [new file with mode: 0644]
lib/script_hash.c [new file with mode: 0644]

diff --git a/lib/dbquery.c b/lib/dbquery.c
new file mode 100644 (file)
index 0000000..c2f50b6
--- /dev/null
@@ -0,0 +1,37 @@
+#include <stdarg.h>
+#include <string.h>
+
+#include "sqlite3.h"
+#include "zpm.h"
+
+sqlite3_stmt *zpm_dbquery(struct zpm *zpm, char *query, ...) {
+       sqlite3 *db;
+       char *sql;
+       va_list args;
+       sqlite3_stmt *st;
+       int rv;
+
+       if (!zpm || zpm->error || !zpm->db) {
+               zpm->error = 1;
+               return 0;
+       }
+
+       db = zpm->db;
+
+       va_start(args, query);
+       sql = sqlite3_vmprintf(query, args);
+       va_end(args);
+
+       if (!sql) {
+               zpm->error = 1;
+               return 0;
+       }
+
+       rv = sqlite3_prepare_v2(db, sql, strlen(sql), &st, NULL);
+       if (rv != SQLITE_OK) {
+               zpm->error = rv;
+               return 0;
+       }
+
+       return st;
+}
diff --git a/lib/script_hash.c b/lib/script_hash.c
new file mode 100644 (file)
index 0000000..d4f03fa
--- /dev/null
@@ -0,0 +1,36 @@
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sqlite3.h"
+#include "zpm.h"
+
+int zpm_script_hash(struct zpm *zpm, char *pkgstr, char *phase, char *hash) {
+       char *pkgid;
+       char *template = "select hash from scripts_pkgid where pkgid = %q and phase = %q";
+       sqlite3_stmt *st;
+
+       pkgid = zpm_findpkg(zpm, pkgstr);
+
+       st = zpm_dbquery(zpm, template, pkgid, phase);
+       free(pkgid);
+
+       if (!st) {
+               return 0;
+       }
+
+       switch (sqlite3_step(st)) {
+               case SQLITE_ROW:
+                       strncpy(hash, (const char *) sqlite3_column_text(st, 0),
+                                       ZPM_HASH_STRLEN);
+                       hash[ZPM_HASH_STRLEN] = 0;
+                       break;
+               default: zpm->error = 1; /* fall through */
+               case SQLITE_DONE:
+                       sqlite3_finalize(st);
+                       return 0; break; /* not found */
+       }
+
+       sqlite3_finalize(st);
+       return 1;
+}