module type S = sig
.. end
type 'a
result
Concurrency monad value.
type ('a, 'b)
statement = {
|
sql_statement : string ; |
|
stmt_id : string option ; |
|
directive : (Sqlexpr_sqlite.st -> 'b) -> Sqlexpr_sqlite.st -> 'a ; |
}
Type of SQL statements (no output parameters).
type ('a, 'b, 'c)
expression = {
|
statement : ('a, 'c) statement ; |
|
get_data : int * (Sqlite3.Data.t array -> 'b) ; |
}
Type of SQL expressions (output parameters).
type
db
Database type
exception Error of string * exn
Exception identical to the toplevel Error
, provided for convenience.
Note that Sqlexpr_sqlite.Error _
matches this exception.
exception Sqlite_error of string * Sqlite3.Rc.t
Exception identical to the toplevel Sqlite_error
, provided for
convenience. Note that Sqlexpr_sqlite.Sqlite_error _
matches this
exception.
val open_db : ?init:(Sqlite3.db -> unit) -> string -> db
Open the DB whose filename is given. ":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
Close the DB and finalize all the associated prepared statements.
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
Execute a SQL statement.
val insert : db ->
('a, int64 result) statement -> 'a
Execute an INSERT SQL statement and return the last inserted row id.
Example:
insert db sqlc"INSERT INTO users(name, pass) VALUES(%s, %s)" name pass
val select : db ->
('a, 'b, 'b list result) expression -> 'a
"Select" a SELECT SQL expression and return a list of tuples; e.g.
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 ->
('a, 'b, 'b result) expression -> 'a
select_one db expr ...
takes the first result from
select db expr ...
.
Raises Not_found
if no row is found.
val select_one_maybe : db ->
('a, 'b, 'b option result) expression -> 'a
select_one_maybe db expr ...
takes the first result from
select db expr ...
.
Returns None if no row is found.
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 ...
.
Raises 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 ...
.
Returns None if no row is found.
val transaction : db ->
(db -> 'a result) ->
'a result
Run the provided function in a DB transaction. A rollback is performed
if an exception is raised inside the transaction.
The 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
.
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
Iterate through the rows returned for the supplied expression.
module Directives: sig
.. end
Module used by the code generated for SQL literals.
module Conversion: sig
.. end
Module used by the code generated for SQL literals.