Skip to content

Commit 045ee76

Browse files
committed
Added support for in-memory databases and URI filenames
1 parent 76a76bb commit 045ee76

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

src/sqlite3.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ module Cache = struct
156156
end
157157

158158
external db_open :
159-
mode : Mode.t -> mutex : Mut.t -> cache : Cache.t ->
160-
?vfs : string -> string -> db = "caml_sqlite3_open"
159+
mode : Mode.t -> uri : bool -> memory : bool -> mutex : Mut.t -> cache : Cache.t ->
160+
?vfs : string -> string -> db = "caml_sqlite3_open_bytecode" "caml_sqlite3_open_native"
161161

162-
let db_open ?mode ?mutex ?cache ?vfs name =
162+
let db_open ?mode ?(uri=false) ?(memory=false) ?mutex ?cache ?vfs name =
163163
let mode = Mode.lift mode in
164164
let mutex = Mut.lift mutex in
165165
let cache = Cache.lift cache in
166-
db_open ~mode ~mutex ~cache ?vfs name
166+
db_open ~mode ~uri ~memory ~mutex ~cache ?vfs name
167167

168168
external db_close : db -> bool = "caml_sqlite3_close"
169169

src/sqlite3.mli

+18-10
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ end
159159

160160
val db_open :
161161
?mode : [ `READONLY | `NO_CREATE ] ->
162+
?uri : bool ->
163+
?memory : bool ->
162164
?mutex : [ `NO | `FULL ] ->
163165
?cache : [ `SHARED | `PRIVATE ] ->
164166
?vfs : string ->
@@ -171,18 +173,24 @@ val db_open :
171173
database respectively.
172174
Behaviour explained here: https://www.sqlite.org/inmemorydb.html
173175
174-
The optional arguments [mode] and [mutex] are only meaningful with SQLite
175-
versions >= 3.5, [cache] only for versions >= 3.6.18. For older versions an
176-
exception will be raised if any of them is set to a non-default value. The
177-
database is opened read-only if [`READONLY] is passed as mode. The database
178-
file will not be created if it is missing and [`NO_CREATE] is set. [mutex]
179-
determines how the database is accessed. The mutex parameters [`NO] and
180-
[`FULL] correspond to [SQLITE_OPEN_NOMUTEX] and [SQLITE_OPEN_FULLMUTEX] in
181-
the SQLite3 API respectively. The cache parameters [`SHARED] and [`PRIVATE]
182-
correspond to [SQLITE_OPEN_SHAREDCACHE] and [SQLITE_OPEN_PRIVATECACHE] in
183-
the SQLite3 API respectively.
176+
The optional arguments [mode], [uri], [memory] and [mutex] are only
177+
meaningful with SQLite versions >= 3.5, [cache] only for versions >= 3.6.18.
178+
For older versions an exception will be raised if any of them is set to a
179+
non-default value. The database is opened read-only if [`READONLY] is
180+
passed as mode. The database file will not be created if it is missing and
181+
[`NO_CREATE] is set. The [uri] parameter enables URI filename
182+
interepretation and corresponds to [SQLITE_OPEN_URI] in the SQLite3 API.
183+
The [memory] parameter opens an in-memory database and corresponds to
184+
[SQLITE_OPEN_MEMORY] in the SQLite3 API. [mutex] determines how the
185+
database is accessed. The mutex parameters [`NO] and [`FULL] correspond to
186+
[SQLITE_OPEN_NOMUTEX] and [SQLITE_OPEN_FULLMUTEX] in the SQLite3 API
187+
respectively. The cache parameters [`SHARED] and [`PRIVATE] correspond to
188+
[SQLITE_OPEN_SHAREDCACHE] and [SQLITE_OPEN_PRIVATECACHE] in the SQLite3 API
189+
respectively.
184190
185191
@param mode default = read-write, create
192+
@param uri default = false
193+
@param memory default = false
186194
@param mutex default = nothing
187195
@param cache default = nothing
188196
@param vfs default = nothing

src/sqlite3_stubs.c

+16-5
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,20 @@ static struct custom_operations db_wrap_ops = {
374374
};
375375

376376
#ifdef SQLITE_HAS_OPEN_V2
377-
static inline int get_open_flags(value v_mode, value v_mutex, value v_cache)
377+
static inline int get_open_flags(value v_mode, value v_uri, value v_memory, value v_mutex, value v_cache)
378378
{
379379
int flags;
380380
switch (Int_val(v_mode)) {
381381
case 0 : flags = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); break;
382382
case 1 : flags = SQLITE_OPEN_READWRITE; break;
383383
default : flags = SQLITE_OPEN_READONLY; break;
384384
}
385+
if (Bool_val(v_uri)) {
386+
flags |= SQLITE_OPEN_URI;
387+
}
388+
if (Bool_val(v_memory)) {
389+
flags |= SQLITE_OPEN_MEMORY;
390+
}
385391
switch (Int_val(v_mutex)) {
386392
case 0 : break;
387393
case 1 : flags |= SQLITE_OPEN_NOMUTEX; break;
@@ -402,13 +408,13 @@ static inline int get_open_flags(value v_mode, value v_mutex, value v_cache)
402408
}
403409
#endif
404410

405-
CAMLprim value caml_sqlite3_open(
406-
value v_mode, value v_mutex, value v_cache, value v_vfs_opt, value v_file)
411+
CAMLprim value caml_sqlite3_open_native(
412+
value v_mode, value v_uri, value v_memory, value v_mutex, value v_cache, value v_vfs_opt, value v_file)
407413
{
408414
sqlite3 *db;
409415
int res;
410416
#ifdef SQLITE_HAS_OPEN_V2
411-
int flags = get_open_flags(v_mode, v_mutex, v_cache);
417+
int flags = get_open_flags(v_mode, v_uri, v_memory, v_mutex, v_cache);
412418
char *vfs;
413419
#endif
414420
int file_len = caml_string_length(v_file) + 1;
@@ -423,7 +429,7 @@ CAMLprim value caml_sqlite3_open(
423429
memcpy(vfs, String_val(v_vfs), vfs_len);
424430
}
425431
#else
426-
if (Int_val(v_mode) || Int_val(v_mutex) || Int_val(v_cache))
432+
if (Int_val(v_mode) || Bool_val(v_uri) || Bool_val(v_memory) || Int_val(v_mutex) || Int_val(v_cache))
427433
caml_failwith("SQLite3 version < 3.5 does not support open flags");
428434
if (v_vfs_opt != Val_None)
429435
caml_failwith("SQLite3 version < 3.5 does not support VFS modules");
@@ -466,6 +472,11 @@ CAMLprim value caml_sqlite3_open(
466472
}
467473
}
468474

475+
CAMLprim value caml_sqlite3_open_bytecode(value *argv, int argn)
476+
{
477+
return caml_sqlite3_open_native(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]);
478+
}
479+
469480
CAMLprim value caml_sqlite3_close(value v_db)
470481
{
471482
int ret, not_busy;

0 commit comments

Comments
 (0)