From 7fa585701657aa1969f2116e71bfcb18c00ca7f0 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sun, 16 Sep 2018 13:16:30 +0000 Subject: [PATCH] add functions to find scripts and run a query --- lib/dbquery.c | 37 +++++++++++++++++++++++++++++++++++++ lib/script_hash.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 lib/dbquery.c create mode 100644 lib/script_hash.c diff --git a/lib/dbquery.c b/lib/dbquery.c new file mode 100644 index 0000000..c2f50b6 --- /dev/null +++ b/lib/dbquery.c @@ -0,0 +1,37 @@ +#include +#include + +#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 index 0000000..d4f03fa --- /dev/null +++ b/lib/script_hash.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#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; +} -- 2.40.0