]> pd.if.org Git - zpackage/blob - lib/db.c
e275d9288e7d4ecd37769f24a514022bb9e078d1
[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
18 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
19         int i;
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 #if 0
28 static char *create_table = "create table if not exists files (hash text primary key, size integer, compression text, content blob)";
29 #endif
30
31 struct dbh {
32         sqlite3 *db;
33         char *errmsg;
34         int rc;
35 };
36
37 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
38 int begin(sqlite3 *db) {
39         int rc;
40         char *err;
41
42         rc = sqlite3_exec(db, "begin;", callback, 0, &err);
43         if (rc != SQLITE_OK) {
44                 SQLERROR(err);
45                 sqlite3_free(err);
46         }
47         return rc;
48 }
49
50 int commit(sqlite3 *db) {
51         int rc;
52         char *err;
53
54         rc = sqlite3_exec(db, "commit;", callback, 0, &err);
55         if (rc != SQLITE_OK) {
56                 SQLERROR(err);
57                 sqlite3_free(err);
58         }
59         return rc;
60 }
61
62 int rollback(sqlite3 *db) {
63         int rc;
64         char *err;
65
66         rc = sqlite3_exec(db, "rollback;", callback, 0, &err);
67         if (rc != SQLITE_OK) {
68                 SQLERROR(err);
69                 sqlite3_free(err);
70         }
71         return rc;
72 }