-
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
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
#include <lean/lean.h>
#include <sqlite3.h>
#include <stdio.h>
typedef struct {
sqlite3_stmt* stmt;
int cols;
} cursor_t;
lean_external_class* g_sqlite_object_external_class = NULL;
lean_external_class* g_sqlite_cursor_external_class = NULL;
void noop_foreach(void* mod, b_lean_obj_arg fn) {}
lean_object* box_connection(sqlite3 *conn) {
return lean_alloc_external(g_sqlite_object_external_class, conn);
}
sqlite3* unbox_connection(lean_object* o) {
return (sqlite3*) lean_get_external_data(o);
}
lean_object* box_cursor(cursor_t* cursor) {
return lean_alloc_external(g_sqlite_cursor_external_class, cursor);
}
cursor_t* unbox_cursor(lean_object* o) {
return (cursor_t*) lean_get_external_data(o);
}
void connection_finalize(void* conn) {
if (!conn) return;
sqlite3_close(conn);
}
void cursor_finalize(void* cursor_ptr) {
cursor_t* cursor = (cursor_t*) cursor_ptr;
if (!cursor->stmt) return;
sqlite3_finalize(cursor->stmt);
if (!cursor) return;
free(cursor);
}
lean_obj_res lean_sqlite_initialize() {
g_sqlite_object_external_class = lean_register_external_class(connection_finalize, noop_foreach);
g_sqlite_cursor_external_class = lean_register_external_class(cursor_finalize, noop_foreach);
return lean_io_result_mk_ok(lean_box(0));
}
lean_obj_res lean_sqlite_open(b_lean_obj_arg path) {
const char* path_str = lean_string_cstr(path);
sqlite3* conn = malloc(sizeof(sqlite3*));
int c = sqlite3_open(path_str, &conn);
if (c == SQLITE_OK)
return lean_io_result_mk_ok(box_connection(conn));
lean_object *err = lean_mk_string(sqlite3_errmsg(conn));
sqlite3_close(conn);
return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}
lean_obj_res lean_sqlite_prepare(b_lean_obj_arg conn_box, b_lean_obj_arg query_str) {
sqlite3* conn = unbox_connection(conn_box);
const char* query = lean_string_cstr(query_str);
cursor_t* cursor = malloc(sizeof(cursor_t*));
int c = sqlite3_prepare_v2(conn, query, -1, &cursor->stmt, NULL);
if (c != SQLITE_OK) {
lean_object* err = lean_mk_string(sqlite3_errmsg(conn));
free(cursor);
lean_object* res = lean_alloc_ctor(0, 1, 0);
lean_ctor_set(res, 0, err);
return lean_io_result_mk_ok(res);
}
cursor->cols = (uint32_t) sqlite3_column_count(cursor->stmt);
lean_object* res = lean_alloc_ctor(1, 1, 0);
lean_ctor_set(res, 0, box_cursor(cursor));
return lean_io_result_mk_ok(res);
}
lean_obj_res lean_sqlite_cursor_column_text(b_lean_obj_arg cursor_box, uint32_t col) {
cursor_t* cursor = unbox_cursor(cursor_box);
const unsigned char* text = sqlite3_column_text(cursor->stmt, col);
lean_object* s = lean_mk_string((const char*) text);
return lean_io_result_mk_ok(s);
}
lean_obj_res lean_sqlite_cursor_column_int(b_lean_obj_arg cursor_box, uint32_t col) {
cursor_t* cursor = unbox_cursor(cursor_box);
const int integer = sqlite3_column_int(cursor->stmt, col);
return lean_io_result_mk_ok(lean_box(integer));
}
lean_obj_res lean_sqlite_cursor_step(b_lean_obj_arg cursor_box) {
cursor_t* cursor = unbox_cursor(cursor_box);
int c = sqlite3_step(cursor->stmt);
if (c == SQLITE_ROW) {
return lean_io_result_mk_ok(lean_box(1));
}
if (c == SQLITE_DONE) {
return lean_io_result_mk_ok(lean_box(0));
}
lean_object* err = lean_mk_string(sqlite3_errmsg(sqlite3_db_handle(cursor->stmt)));
return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}
lean_obj_res lean_sqlite_cursor_reset(b_lean_obj_arg cursor_box) {
cursor_t* cursor = unbox_cursor(cursor_box);
int c = sqlite3_reset(cursor->stmt);
if (c == SQLITE_OK)
return lean_io_result_mk_ok(lean_box(0));
lean_object *err = lean_mk_string(sqlite3_errmsg(sqlite3_db_handle(cursor->stmt)));
return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}
uint32_t lean_sqlite_cursor_columns_count(b_lean_obj_arg cursor_box) {
cursor_t* cursor = unbox_cursor(cursor_box);
return cursor->cols;
}