]> pd.if.org Git - zpackage/blob - lib/db.c
cleanup sqlite references
[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         for(i=0; i<argc; i++){
20                 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
21         }
22         printf("\n");
23         return 0;
24 }
25
26 struct dbh {
27         sqlite3 *db;
28         char *errmsg;
29         int rc;
30 };
31
32 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
33 int begin(sqlite3 *db) {
34         int rc;
35         char *err;
36
37         rc = sqlite3_exec(db, "begin;", callback, 0, &err);
38         if (rc != SQLITE_OK) {
39                 SQLERROR(err);
40                 sqlite3_free(err);
41         }
42         return rc;
43 }
44
45 int commit(sqlite3 *db) {
46         int rc;
47         char *err;
48
49         rc = sqlite3_exec(db, "commit;", callback, 0, &err);
50         if (rc != SQLITE_OK) {
51                 SQLERROR(err);
52                 sqlite3_free(err);
53         }
54         return rc;
55 }
56
57 int rollback(sqlite3 *db) {
58         int rc;
59         char *err;
60
61         rc = sqlite3_exec(db, "rollback;", callback, 0, &err);
62         if (rc != SQLITE_OK) {
63                 SQLERROR(err);
64                 sqlite3_free(err);
65         }
66         return rc;
67 }