Page
Library
Module
Module type
Parameter
Class
Class type
Source
Sqlite3_utils
SourceException raised by most of the functions below when a Sqlite failure occurs, with the corresponding error code.
Exception raised when the declared Ty.t
does not match the actual result returned by Sqlite.
Turn the error into a string using Rc.to_string
val with_db :
?wal:bool ->
?mode:[ `NO_CREATE | `READONLY ] ->
?mutex:[ `FULL | `NO ] ->
?cache:[ `PRIVATE | `SHARED ] ->
?vfs:string ->
?timeout:int ->
string ->
(t -> 'a) ->
'a
Temporarily open a DB connection. Parameters follow Sqlite3.db_open
.
Values representing types to pass to a statement, or to extract from a row
A Cursor is a special iterator over Sqlite rows of results. It should be consumed quickly as it will not survive the call to exec
, exec_raw
, etc.
Locally make a statement out of the given string, then cleanup when f
returns.
Run the query purely for its side effects, without any parameter. If you need to parametrize this with user inputs, use exec_raw
or exec_raw_args
or exec_no_cursor
to use a safe parametrized statement instead of builtin a string that is vulnerable to SQL injections.
Same as exec_raw
but uses check_ret_exn
to unwrap errors.
val exec_raw_args :
t ->
string ->
Sqlite3.Data.t array ->
f:(Data.t array Cursor.t -> 'b) ->
('b, Rc.t) result
Similar to exec_raw
but also takes an array of parameters that will fill the ?
placeholders of the prepare statement.
val exec_raw_args_exn :
t ->
string ->
Sqlite3.Data.t array ->
f:(Data.t array Cursor.t -> 'b) ->
'b
Same as exec_raw_args
but uses check_ret_exn
to unwrap errors.
val exec :
t ->
string ->
ty:(('a, ('res, Rc.t) result) Ty.t * ('b, 'c) Ty.t * 'b) ->
f:('c Cursor.t -> 'res) ->
'a
Typesafe alternative to exec_raw_args
.
Example:
# with_db ":memory:" (fun db ->
exec0_exn db "create table person (name text, age int);";
exec0_exn db "insert into person values ('alice', 20), ('bob', 25) ;";
exec db "select age from person where name=? ;"
~ty:Ty.(p1 text, p1 int, (fun (x:int) -> x))
"alice"
~f:Cursor.to_list);;
- : (int list, Rc.t) result = Ok [20]
val exec_exn :
t ->
string ->
ty:(('a, 'res) Ty.t * ('b, 'c) Ty.t * 'b) ->
f:('c Cursor.t -> 'res) ->
'a
Same as exec
but uses check_ret_exn
to unwrap the result.
val exec_no_params :
t ->
string ->
ty:(('b, 'c) Ty.t * 'b) ->
f:('c Cursor.t -> 'res) ->
('res, Rc.t) result
Same as exec
but without parameters. ty
only takes two arguments: the type description for result rows, and a function to map rows to 'c
val exec_no_params_exn :
t ->
string ->
ty:(('b, 'c) Ty.t * 'b) ->
f:('c Cursor.t -> 'res) ->
'res
Same as exec_no_params_exn
but uses check_ret_exn
to unwrap the result.
Perform a query that doesn't return any result.
Same as exec_no_cursor_exn
but uses check_ret_exn
to unwrap the result.
exec_get_column_names db query
is the list of names of columns that would be returned by exec db query
.
transact db f
runs f db
within a transaction (begin/commit/rollback). Useful to perform a batch of insertions or updates, as Sqlite doesn't write very fast.