Merge branch 'callbacks'
[m6w6/ext-psi] / tests / sqlite / sqlite.psi
1 lib "sqlite3";
2
3 extern const char *sqlite3_libversion(void);
4 function sqlite3\version() : string {
5 return to_string(sqlite3_libversion);
6 }
7
8 extern const char *sqlite3_errstr(int errno);
9 function sqlite3\errstr(int $errno) : string {
10 let errno = intval($errno);
11 return to_string(sqlite3_errstr);
12 }
13
14 /* obviously fake, we ever always need a pointer to it */
15 struct sqlite3::(8, 8);
16
17 typedef struct sqlite3 sqlite3;
18
19 typedef int64_t sqlite_int64;
20 typedef uint64_t sqlite_uint64;
21
22 typedef sqlite_int64 sqlite3_int64;
23 typedef sqlite_uint64 sqlite3_uint64;
24
25 extern void sqlite3_free(void *ptr);
26 function sqlite3\free(object $object) : void {
27 let ptr = objval($object);
28 return void(sqlite3_free);
29 }
30
31 extern int sqlite3_close(sqlite3 *db);
32 function sqlite3\close(object $db) : int {
33 let db = objval($db);
34 return to_int(sqlite3_close);
35 }
36
37 extern int sqlite3_open(const char *filename, sqlite3 **db_ptr);
38 function sqlite3\open(string $uri, object &$db) : int {
39 let filename = pathval($uri);
40 let db_ptr = &NULL;
41 return to_int(sqlite3_open);
42 set $db = to_object(*db_ptr);
43 }
44
45 typedef int (*sqlite3_callback)(void *data, int argc, char** argv, char** cols);
46
47 /*
48 * C calls into us, so we have to have a way to define how the callback
49 * arguments have to be marshaled for the userland callback, i.e. from
50 * native C types to ZE zvals /and/ how the userland return value has
51 * to be marshaled to a native type. All in all, the opposite of function impls.
52 */
53
54 extern int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *data, char **errmsg);
55 function sqlite3\exec(object $db, string $sql, callable $cb, mixed $cd, string &$error = null) : int {
56 let db = objval($db);
57 let sql = strval($sql);
58 let callback = callback intval(
59 $cb(
60 zval(data),
61 to_int(argc),
62 to_array(argv, argc, to_string(argv)),
63 to_array(cols, argc, to_string(cols))
64 )
65 );
66 let data = zval($cd);
67 let errmsg = &NULL;
68 return to_int(sqlite3_exec);
69 set $error = to_string(*errmsg);
70 }