-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathhello.c
More file actions
103 lines (92 loc) · 3.26 KB
/
Copy pathhello.c
File metadata and controls
103 lines (92 loc) · 3.26 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Minimal C sample against the SQLRite FFI surface.
*
* Walks through:
* - opening an in-memory connection
* - CREATE TABLE + INSERT via sqlrite_execute
* - SELECT via sqlrite_query + sqlrite_step + sqlrite_column_*
* - explicit memory freeing (sqlrite_free_string, sqlrite_finalize,
* sqlrite_close)
* - a BEGIN / ROLLBACK transaction block
*
* Build + run:
* cd sqlrite-ffi && cargo build --release
* cc -I../sqlrite-ffi/include \
* -L../target/release -lsqlrite_c \
* examples/c/hello.c -o hello
* # macOS: also pass -Wl,-rpath,../target/release
* ./hello
*
* (On Linux, add `-Wl,-rpath,$PWD/../target/release` so the runtime
* can find libsqlrite_c.so without LD_LIBRARY_PATH juggling.)
*/
#include <stdio.h>
#include <stdlib.h>
#include "sqlrite.h"
/* Small helper that prints the library's last error and exits. */
static void die(const char *what) {
const char *err = sqlrite_last_error();
fprintf(stderr, "%s: %s\n", what, err ? err : "(no error message)");
exit(1);
}
int main(void) {
struct SqlriteConnection *conn = NULL;
if (sqlrite_open_in_memory(&conn) != Ok) {
die("sqlrite_open_in_memory");
}
if (sqlrite_execute(conn,
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);") != Ok) {
die("CREATE TABLE");
}
if (sqlrite_execute(conn,
"INSERT INTO users (name, age) VALUES ('alice', 30);") != Ok) {
die("INSERT alice");
}
if (sqlrite_execute(conn,
"INSERT INTO users (name, age) VALUES ('bob', 25);") != Ok) {
die("INSERT bob");
}
/* Show the table. */
struct SqlriteStatement *stmt = NULL;
if (sqlrite_query(conn, "SELECT id, name, age FROM users;", &stmt) != Ok) {
die("sqlrite_query");
}
int col_count = 0;
sqlrite_column_count(stmt, &col_count);
printf("Columns: ");
for (int i = 0; i < col_count; i++) {
char *name = NULL;
sqlrite_column_name(stmt, i, &name);
printf("%s%s", name, i + 1 < col_count ? ", " : "\n");
sqlrite_free_string(name);
}
while (1) {
enum SqlriteStatus st = sqlrite_step(stmt);
if (st == Done) break;
if (st != Row) die("sqlrite_step");
int64_t id = 0;
char *name = NULL;
int64_t age = 0;
sqlrite_column_int64(stmt, 0, &id);
sqlrite_column_text(stmt, 1, &name);
sqlrite_column_int64(stmt, 2, &age);
printf(" row: id=%lld name=%s age=%lld\n",
(long long)id, name, (long long)age);
sqlrite_free_string(name);
}
sqlrite_finalize(stmt);
/* Transaction that gets rolled back: the row doesn't survive. */
sqlrite_execute(conn, "BEGIN;");
sqlrite_execute(conn, "INSERT INTO users (name, age) VALUES ('phantom', 99);");
printf("\nIn transaction: %d\n", sqlrite_in_transaction(conn));
sqlrite_execute(conn, "ROLLBACK;");
printf("After rollback: %d\n", sqlrite_in_transaction(conn));
/* Count rows post-rollback — should be 2. */
sqlrite_query(conn, "SELECT id FROM users;", &stmt);
int count = 0;
while (sqlrite_step(stmt) == Row) count++;
printf("Row count after rollback: %d (expected 2)\n", count);
sqlrite_finalize(stmt);
sqlrite_close(conn);
return 0;
}