module type S =sig
..end
type 'a
result
type ('a, 'b)
statement = {
|
sql_statement : |
|
stmt_id : |
|
directive : |
type ('a, 'b, 'c)
expression = {
|
statement : |
|
get_data : |
type
db
exception Error of string * exn
Error
, provided for convenience.
Note that Sqlexpr_sqlite.Error _
matches this exception.exception Sqlite_error of string * Sqlite3.Rc.t
Sqlite_error
, provided for
convenience. Note that Sqlexpr_sqlite.Sqlite_error _
matches this
exception.val open_db : ?init:(Sqlite3.db -> unit) -> string -> db
":memory:"
refers to an in-mem DB.
* init
function to be applied to Sqlite3.db
handle(s) before
* they are used (can be used to register functions or initialize schema in
* in-mem tables.val close_db : db -> unit
val borrow_worker : db ->
(db -> 'a result) ->
'a result
borrow_worker db f
evaluates f db'
where db'
borrows a 'worker'
* from db
and db'
is only valid inside f
. All the operations on
* db'
will use the same worker. Use this e.g. if you have an in-mem
* database and a number of operations that must go against the same
* instance (since data is not shared across different :memory:
* databases). db'
will not spawn new workers and will be closed and
* invalidated automatically.val steal_worker : db ->
(db -> 'a result) ->
'a result
steal_worker db f
is similar to borrow_worker db f
, but ensures
* that f
is given exclusive access to the worker while it is being
* evaluated.val execute : db ->
('a, unit result) statement -> 'a
val insert : db ->
('a, int64 result) statement -> 'a
insert db sqlc"INSERT INTO users(name, pass) VALUES(%s, %s)" name pass
val select : db ->
('c, 'a, 'a list result) expression -> 'c
select db sqlc"SELECT @s{name}, @s{pass} FROM users"
select db sqlc"SELECT @s{pass} FROM users WHERE id = %L" user_id
val select_f : db ->
('a -> 'b result) ->
('c, 'a, 'b list result) expression -> 'c
select_f db f expr ...
is similar to select db expr ...
but maps the
results using the provided f
function.val select_one : db ->
('c, 'a, 'a result) expression -> 'c
select_one db expr ...
takes the first result from
select db expr ...
.Not_found
if no row is found.val select_one_maybe : db ->
('c, 'a, 'a option result) expression -> 'c
select_one_maybe db expr ...
takes the first result from
select db expr ...
.val select_one_f : db ->
('a -> 'b result) ->
('c, 'a, 'b result) expression -> 'c
select_one_f db f expr ...
is returns the first result from
select_f db f expr ...
.Not_found
if no row is found.val select_one_f_maybe : db ->
('a -> 'b result) ->
('c, 'a, 'b option result) expression -> 'c
select_one_f_maybe db expr ...
takes the first result from
select_f db f expr ...
.val transaction : db ->
?kind:[ `DEFERRED | `EXCLUSIVE | `IMMEDIATE ] ->
(db -> 'a result) ->
'a result
If the BEGIN or COMMIT SQL statements from the outermost transaction fail
with SQLITE_BUSY
, they will be retried until they can be executed.
A SQLITE_BUSY
(or any other) error code in any other operation inside
a transaction will result in an Error (_, Sqlite_error (code, _))
exception being thrown, and a rollback performed.
One consequence of this is that concurrency control is very simple if
you use `EXCLUSIVE
transactions: the code can be written
straightforwardly as S.transaction db (fun db -> ...)
, and their
execution will be serialized (across both threads and processes).
Note that, for `IMMEDIATE
and `DEFERRED
transactions, you will
have to retry manually if an
Error (_, Sqlite_error (Sqlite3.Rc.Busy, _))
is raised.
All SQL operations performed within a transaction will use the same
worker. This worker is used exclusively by only one thread per
instantiated module (see Sqlexpr_sqlite.S.steal_worker
).
That is, given
module S1 = Sqlexpr_sqlite.Make(Sqlexpr_concurrency.Id)
module S2 = Sqlexpr_sqlite.Make(Sqlexpr_concurrency.Lwt)
let db = S1.open_db somefile
there is no exclusion between functions from S1
and those from S2
.kind
: transaction kind, only meaningful for outermost transaction
(default `DEFERRED
)val fold : db ->
('a -> 'b -> 'a result) ->
'a -> ('c, 'b, 'a result) expression -> 'c
fold db f a expr ...
is
f (... (f (f a r1) r2) ...) rN
where rN
is the n-th row returned for the SELECT expression expr
.val iter : db ->
('a -> unit result) ->
('b, 'a, unit result) expression -> 'b
module Directives:sig
..end
module Conversion:sig
..end