X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=lib%2Fdbquery.c;h=3bda2d9e2af8c9cb647f019d0ea9f50bcbc60f78;hb=69491d14441cfc893e18e1c60c13c995b93002c9;hp=11131645d9b4d388c7229665515b588492a4164e;hpb=c907b8ec08b06a4a256fd12b79b4bcf5088fbb19;p=zpackage diff --git a/lib/dbquery.c b/lib/dbquery.c index 1113164..3bda2d9 100644 --- a/lib/dbquery.c +++ b/lib/dbquery.c @@ -2,6 +2,7 @@ #include #include +#include #include "sqlite3.h" #include "zpm.h" @@ -39,6 +40,7 @@ sqlite3_stmt *zpm_dbqueryv(struct zpm *zpm, char *query, va_list args) { zpm->error = rv; return 0; } + sqlite3_free(sql); return st; } @@ -63,7 +65,7 @@ sqlite3_stmt *zpm_dbquery(struct zpm *zpm, char *query, ...) { return st; } -void zpm_db_run(struct zpm *zpm, char *query, ...) { +int zpm_db_run(struct zpm *zpm, char *query, ...) { sqlite3_stmt *st; va_list args; int rv; @@ -79,8 +81,28 @@ void zpm_db_run(struct zpm *zpm, char *query, ...) { zpm_seterror(zpm, "db error: %s", sqlite3_errstr(rv)); } - sqlite3_finalize(st); - return ; + return sqlite3_finalize(st); +} + +int zpm_findhash(struct zpm *zpm, char *find, char *dest) { + int count; + char *found; + + count = zpm_db_int(zpm, "select count(*) from files where hash like '%q%%';", find); + if (count != 1) { + return count; + } + if (dest) { + found = zpm_db_string(zpm, "select hash from files where hash like '%s%%' limit 1;", find); + if (find) { + strcpy(dest, found); + free(found); + } else { + count = 0; + } + } + return count; + } char *zpm_db_string(struct zpm *zpm, char *query, ...) {