X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=lib%2Fdbquery.c;h=f68eb4ab2489c2bdf3a6b121cafee2822ac78e95;hb=86d735d52280359df4896d5c542ddfbe33d8df7b;hp=cba95378ae2a258c8351b9f3ac07191bc46ce337;hpb=383712eefa950c5dc619f8cd5fb60be8e8041502;p=zpackage diff --git a/lib/dbquery.c b/lib/dbquery.c index cba9537..f68eb4a 100644 --- a/lib/dbquery.c +++ b/lib/dbquery.c @@ -2,6 +2,7 @@ #include #include +#include #include "sqlite3.h" #include "zpm.h" @@ -35,9 +36,11 @@ sqlite3_stmt *zpm_dbqueryv(struct zpm *zpm, char *query, va_list args) { rv = sqlite3_prepare_v2(db, sql, strlen(sql), &st, NULL); if (rv != SQLITE_OK) { SQLERROR(sqlite3_errmsg(db)); + fprintf(stderr, "sql (%d): %s\n", rv, sql); zpm->error = rv; return 0; } + sqlite3_free(sql); return st; } @@ -82,6 +85,27 @@ void zpm_db_run(struct zpm *zpm, char *query, ...) { return ; } +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, ...) { sqlite3_stmt *st; va_list args; @@ -94,11 +118,18 @@ char *zpm_db_string(struct zpm *zpm, char *query, ...) { rv = sqlite3_step(st); - if (rv == SQLITE_ROW) { + switch (rv) { + case SQLITE_ROW: result = (char *)sqlite3_column_text(st, 0); if (result) { result = strdup(result); } + break; + case SQLITE_DONE: break; + default: + zpm->error = 1; + zpm_seterror(zpm, "db error: %s", sqlite3_errstr(rv)); + break; } sqlite3_finalize(st);