-
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
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
#include <lean/lean.h>
#include <sqlite3.h>
#include <stdio.h>
uint32_t myAdd(uint32_t a, uint32_t b) {
printf("a = %d, b = %d\n", a, b);
return a + b;
}
lean_obj_res myLeanFun() {
return lean_io_result_mk_ok(lean_box(0));
}
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_exec(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);
return lean_io_result_mk_error(lean_mk_io_error_other_error(c, err));
}
cursor->cols = sqlite3_column_count(cursor->stmt);
if (cursor->cols == 0)
return lean_io_result_mk_ok(lean_box(0));
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_step(b_lean_obj_arg cursor_box) {
cursor_t* cursor = unbox_cursor(cursor_box);
int c = sqlite3_step(cursor->stmt);
if (c == SQLITE_ROW) {
lean_object* row = lean_alloc_array(0, cursor->cols);
for (int i = 0; i < cursor->cols; i++) {
const unsigned char* text = sqlite3_column_text(cursor->stmt, i);
lean_object* s = lean_mk_string((const char*) text);
lean_array_push(row, s);
}
lean_object *some_row = lean_alloc_ctor(1, 1, 0);
lean_ctor_set(some_row, 0, row);
return lean_io_result_mk_ok(some_row);
}
if (c == SQLITE_DONE) {
lean_object* none_row = lean_alloc_ctor(0, 0, 0);
return lean_io_result_mk_ok(none_row);
}
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_reset_cursor(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));
}
int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
uint32_t wasd(uint32_t a) {
printf("hello world\n");
sqlite3 *db;
char *zErrMsg = 0;
int err;
err = sqlite3_open("test.sqlite3", &db);
if (err != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}
err = sqlite3_exec(db, "select 1 = 1;", callback, 0, &zErrMsg);
if (err != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return a;
}