23669d6eb4501530ff4adfdbfd548ac8a7ad2842
[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
27 extern int sqlite3_close(sqlite3 *db);
28 function sqlite3\close(object &$db) : int {
29 let db = objval($db);
30 return to_int(sqlite3_close);
31 // unset($db) as safe-guard
32 set $db = void(db);
33 }
34
35 extern int sqlite3_open(const char *filename, sqlite3 **db_ptr);
36 function sqlite3\open(string $uri, object &$db) : int {
37 let filename = pathval($uri);
38 let db_ptr = &NULL;
39 return to_int(sqlite3_open);
40 set $db = to_object(*db_ptr);
41 }
42
43 typedef int (*sqlite3_callback)(void *data, int argc, char** argv, char** cols);
44
45 /*
46 * C calls into us, so we have to have a way to define how the callback
47 * arguments have to be marshaled for the userland callback, i.e. from
48 * native C types to ZE zvals /and/ how the userland return value has
49 * to be marshaled to a native type. All in all, the opposite of function impls.
50 */
51
52 extern int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *data, char **errmsg);
53 function sqlite3\exec(object $db, string $sql, callable $cb, mixed $cd, string &$error = null) : int {
54 let db = objval($db);
55 let sql = strval($sql);
56 let callback = callback intval(
57 $cb(
58 zval(data),
59 to_int(argc),
60 to_array(argv, argc, to_string(argv)),
61 to_array(cols, argc, to_string(cols))
62 )
63 );
64 let data = zval($cd);
65 let errmsg = &NULL;
66 return to_int(sqlite3_exec);
67 set $error = to_string(*errmsg);
68 free sqlite3_free(*errmsg);
69 }