-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqlite_api.c
62 lines (49 loc) · 1.08 KB
/
sqlite_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sqlite3.h>
#include "sqlite_api.h"
#include "bit_func.h"
static sqlite3 *meta_db;
void sqlite_api_query_cb(const char *input)
{
const char *ptr = input;
char query[4096];
int ret;
assert(input != NULL);
if (input[0] == 0) {
return;
}
while (sgets(query, sizeof(query), &ptr)) {
ret = sqlite3_exec(meta_db, query, 0, 0, 0);
if (ret != SQLITE_OK) {
printf("Error executing query:\n%s\n", query);
exit(1);
}
}
}
void sqlite_api_init(struct session_info *s)
{
int ret;
//TODO check if db file exists, init new db with schema
ret = sqlite3_open("metadata.db", &meta_db);
if (ret) {
printf("Cannot open database\n");
sqlite3_close(meta_db);
exit(1);
}
ret = sqlite3_exec(meta_db, "BEGIN TRANSACTION;", 0, 0, 0);
if (ret) {
printf("Cannot begin transaction\n");
}
s->sql_callback = sqlite_api_query_cb;
}
void sqlite_api_destroy()
{
int ret;
ret = sqlite3_exec(meta_db, "COMMIT TRANSACTION;", 0, 0, 0);
if (ret) {
printf("Cannot commit transaction\n");
}
sqlite3_close(meta_db);
}