-
Notifications
You must be signed in to change notification settings - Fork 1
/
test3.c
78 lines (62 loc) · 2.15 KB
/
test3.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "sqlite3.h"
#include "vfs-ramcloud.h"
#include <inttypes.h>
#include <assert.h>
#include <stdio.h>
#define ROWS 100000
int main(int argc, char **argv) {
int retval;
char *dbname = "./my-db";
if (argc > 1)
dbname = argv[1];
//sqlite3_vfs *vfs = sqlite3_vfs_find("unix");
//assert(vfs != NULL);
//retval = sqlite3_vfs_register(vfs, 1);
SQLITE_RCVFS_CONNECTION *conn =
sqlite3_rcvfs_connect("zk:localhost:2181", "main", "sqlite3");
assert(conn != NULL);
retval = sqlite3_vfs_register(sqlite3_rcvfs("ramcloud", conn), 1);
assert(retval == 0);
sqlite3 *db;
retval = sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
assert(retval == 0);
printf("file opened\n");
retval = sqlite3_exec(db, "PRAGMA journal_mode=truncate;", NULL, NULL, NULL);
printf("retval is %d\n", retval);
assert(retval == 0);
retval = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS test (value INTEGER);", NULL, NULL, NULL);
printf("retval is %d\n", retval);
assert(retval == 0);
retval = sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
assert(retval == 0);
sqlite3_stmt *sql_insert;
retval = sqlite3_prepare(db, "INSERT INTO test (value) VALUES (:v)", -1, &sql_insert, NULL);
assert(retval == 0);
for (unsigned i = 0; i < ROWS; ++i) {
retval = sqlite3_bind_int64(sql_insert, 1, i);
assert(retval == 0);
retval = sqlite3_step(sql_insert);
assert(retval == SQLITE_DONE);
retval = sqlite3_reset(sql_insert);
assert(retval == 0);
}
retval = sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
assert(retval == 0);
retval = sqlite3_close_v2(db);
assert(retval == 0);
retval = sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READONLY, NULL);
assert(retval == 0);
sqlite3_stmt *sql_sum;
retval = sqlite3_prepare(db, "SELECT SUM(value) FROM test;", -1, &sql_sum, NULL);
assert(retval == 0);
retval = sqlite3_step(sql_sum);
assert(retval = SQLITE_ROW);
uint64_t sum = sqlite3_column_int64(sql_sum, 0);
retval = sqlite3_reset(sql_sum);
assert(retval == 0);
retval = sqlite3_close_v2(db);
assert(retval == 0);
sqlite3_rcvfs_disconnect(conn);
printf("sum is %lu\n", sum);
return 0;
}