Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include <caml/mlvalues.h>#include <caml/memory.h>#include <caml/fail.h>#include <dlfcn.h>#include <string.h>
typedef int (*collate_fn)(void *, int, const void *, int, const void *);
static int byte_order(void *ctx, int alen, const void *a, int blen, const void *b){ (void)ctx; int common = alen < blen ? alen : blen; int order = memcmp(a, b, common); return order ? order : (alen > blen) - (alen < blen);}
/* Load the platform's reference SQLite without linking it into the OCaml * engine. The executable exits with sqlite3_exec's actual result code. */CAMLprim value sqlite_reference_execute(value path, value sql, value collation){ CAMLparam3(path, sql, collation); void *library = dlopen("libsqlite3.dylib", RTLD_NOW | RTLD_LOCAL); if (!library) library = dlopen("libsqlite3.so.0", RTLD_NOW | RTLD_LOCAL); if (!library) library = dlopen("libsqlite3.so", RTLD_NOW | RTLD_LOCAL); if (!library) caml_failwith("stock SQLite library is unavailable"); int (*open_db)(const char *, void **) = dlsym(library, "sqlite3_open"); int (*exec_sql)(void *, const char *, void *, void *, char **) = dlsym(library, "sqlite3_exec"); int (*close_db)(void *) = dlsym(library, "sqlite3_close"); int (*create_collation)(void *, const char *, int, void *, collate_fn) = dlsym(library, "sqlite3_create_collation"); if (!open_db || !exec_sql || !close_db || (Is_block(collation) && !create_collation)) { dlclose(library); caml_failwith("stock SQLite API is unavailable"); } void *db = NULL; int rc = open_db(String_val(path), &db); if (rc == 0 && Is_block(collation)) { const int sqlite_utf8 = 1; rc = create_collation(db, String_val(Field(collation, 0)), sqlite_utf8, NULL, byte_order); } if (rc == 0) rc = exec_sql(db, String_val(sql), NULL, NULL, NULL); if (db) close_db(db); dlclose(library); CAMLreturn(Val_int(rc));}