]> pd.if.org Git - zpackage/blob - lib/db.c
add warnings to compile flags and fix
[zpackage] / lib / db.c
1 /*
2  * add a file/files to an sqlite db
3  * in the 'files' table.
4  */
5
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11
12 #include <sys/mman.h>
13
14 #include "sqlite3.h"
15 #include "sha256.h"
16
17 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
18         int i;
19         NotUsed = 0; /* suppress warning */
20         for(i=0; i<argc; i++){
21                 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
22         }
23         printf("\n");
24         return 0;
25 }
26
27 struct dbh {
28         sqlite3 *db;
29         char *errmsg;
30         int rc;
31 };
32
33 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
34 int begin(sqlite3 *db) {
35         int rc;
36         char *err;
37
38         rc = sqlite3_exec(db, "begin;", callback, 0, &err);
39         if (rc != SQLITE_OK) {
40                 SQLERROR(err);
41                 sqlite3_free(err);
42         }
43         return rc;
44 }
45
46 int commit(sqlite3 *db) {
47         int rc;
48         char *err;
49
50         rc = sqlite3_exec(db, "commit;", callback, 0, &err);
51         if (rc != SQLITE_OK) {
52                 SQLERROR(err);
53                 sqlite3_free(err);
54         }
55         return rc;
56 }
57
58 int rollback(sqlite3 *db) {
59         int rc;
60         char *err;
61
62         rc = sqlite3_exec(db, "rollback;", callback, 0, &err);
63         if (rc != SQLITE_OK) {
64                 SQLERROR(err);
65                 sqlite3_free(err);
66         }
67         return rc;
68 }