123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253(* Copyright (C) 2017--2023 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)(** Signature of connection handles.
The main signature {!S} of this module represents a database connection
handle. This is obtained by {{!Caqti_connect_sig} connection functions}
implemented in the subpackages [caqti-async], [caqti-eio], [caqti-lwt], and
[caqti-mirage].
While values of {!Caqti_request.t} hold SQL code to be sent to the database,
connection handles defined here provide the means to execute them with
actual parameters on an RDBMS. So, there is a separation between
preparation and execution. This is motivated by the common support for
prepared queries in database client libraries, and by the desire to keep the
possibly deeply nested data-processing code uncluttered by strings of SQL
code. For this separation to be reasonably safe, the request declares the
types of parameters and result, and these type declarations are placed right
next to the SQL code, so that we can rely on OCaml's powers of refactoring
large code bases safely.
The result type of {!Caqti_request.t} only describes how to decode {e
individual rows}, leaving the decision of how to process multiple rows to
the execution interface. Therefore, for each request constructor from
{!Caqti_request.Infix}, there are one or more matching retrieval functions
in the present signature. *)typedriver_connection=..(** This type is only to be extended by drivers. *)(** Essential connection signature implemented by drivers. *)moduletypeBase=sigtype+'afibertype(+'a,+'err)stream(** {2 Query} *)moduleResponse:Caqti_response_sig.Swithtype'afiber:='afiberandtype('a,'err)stream:=('a,'err)streamvalcall:f:(('b,'m)Response.t->('c,'e)resultfiber)->('a,'b,'m)Caqti_request.t->'a->('c,[>Caqti_error.call]as'e)resultfiber(** [call ~f request params] executes [request] with parameters [params]
invoking [f] to process the result; except the driver may postpone the
request until [f] attempts to retrieve the result.
One of the {{!Response.result_retrieval} result retrieval}
functions must be called exactly once before [f] returns a non-error
result. If a result retrieval function is not called, it is unspecified
whether the database query has been issued.
The argument of [f] is only valid during the call to [f], and must not be
returned or operated on by other threads. *)valset_statement_timeout:floatoption->(unit,[>Caqti_error.call])resultfiber(** Set or clear the timeout after which a running SQL statement will be
terminated if supported by the driver.
This is currently supported for MariaDB (using [max_statement_time]) and
PostgreSQL (using [statement_timeout]) and has no effect for SQLite3. *)(** {2 Transactions} *)valstart:unit->(unit,[>Caqti_error.transact])resultfiber(** Starts a transaction if supported by the underlying database, otherwise
does nothing. *)valcommit:unit->(unit,[>Caqti_error.transact])resultfiber(** Commits the current transaction if supported by the underlying database,
otherwise does nothing. *)valrollback:unit->(unit,[>Caqti_error.transact])resultfiber(** Rolls back a transaction if supported by the underlying database,
otherwise does nothing. *)(** {2 Disconnection and Reuse} *)valdeallocate:('a,'b,'m)Caqti_request.t->(unit,[>Caqti_error.call])resultfiber(** [deallocate req] deallocates the prepared query for [req] if it was
allocated. The request must not be oneshot. *)valdisconnect:unit->unitfiber(** Calling [disconnect ()] closes the connection to the database and frees
up related resources. *)valvalidate:unit->boolfiber(** For internal use by pool implementations. Tries to ensure the validity of
the connection and must return [false] if unsuccessful. *)valcheck:(bool->unit)->unit(** For internal use by pool implementations. Called after a connection has
been used. [check f] must call [f ()] exactly once with an argument
indicating whether to keep the connection in the pool or discard it. *)endmoduletypeConvenience=sigtype+'afiber(** {2 Retrieval Convenience}
Each of these shortcuts combine [call] with the correspondingly named
retrieval function from {!Caqti_response_sig.S}. *)valexec:('a,unit,[<`Zero])Caqti_request.t->'a->(unit,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [exec req x] performs [req] with parameters [x] and checks that no rows
are returned.
See also {!Caqti_response_sig.S.exec}. *)valexec_with_affected_count:('a,unit,[<`Zero])Caqti_request.t->'a->(int,[>Caqti_error.call_or_retrieve|`Unsupported]as'e)resultfiber(** [exec_with_affected_count req x] performs [req] with parameters [x],
checks that no rows are returned, and returns the number of affected rows.
See also {!Caqti_response_sig.S.exec} and
{!Caqti_response_sig.S.affected_count}. *)valfind:('a,'b,[<`One])Caqti_request.t->'a->('b,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [find req x] performs [req] with parameters [x], checks that a single row
is retured, and returns it.
See also {!Caqti_response_sig.S.find}. *)valfind_opt:('a,'b,[<`Zero|`One])Caqti_request.t->'a->('boption,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [find_opt req x] performs [req] with parameters [x] and returns either
[None] if no rows are returned or [Some y] if a single now [y] is returned
and fails otherwise.
See also {!Caqti_response_sig.S.find_opt}. *)valfold:('a,'b,[<`Zero|`One|`Many])Caqti_request.t->('b->'c->'c)->'a->'c->('c,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [fold req f x acc] performs [req] with parameters [x] and passes [acc]
through the composition of [f y] across the result rows [y] in the order
of retrieval.
See also {!Caqti_response_sig.S.fold}. *)valfold_s:('a,'b,[<`Zero|`One|`Many])Caqti_request.t->('b->'c->('c,'e)resultfiber)->'a->'c->('c,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [fold_s f x acc] performs [req] with parameters [x] and passes [acc]
through the monadic composition of [f y] across the returned rows [y] in
the order of retrieval.
Please be aware of possible deadlocks when using resources from the
callback. In particular, if the same connection pool is invoked as the
one used to obtain the current connection, it will deadlock if the pool
has just run out of connections. An alternative is to collect the rows
first e.g. with {!fold} and do the nested queries after exiting.
See also {!Caqti_response_sig.S.fold_s}. *)valiter_s:('a,'b,[<`Zero|`One|`Many])Caqti_request.t->('b->(unit,'e)resultfiber)->'a->(unit,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [iter_s f x] performs [req] with parameters [x] and sequences calls to [f
y] for each result row [y] in the order of retrieval.
Please see the warning in {!fold_s} about resource usage in the callback.
See also {!Caqti_response_sig.S.iter_s}. *)valcollect_list:('a,'b,[<`Zero|`One|`Many])Caqti_request.t->'a->('blist,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [collect_list request x] performs a [req] with parameters [x] and returns
a list of rows in order of retrieval. The accumulation is tail recursive
but slightly less efficient than {!rev_collect_list}. *)valrev_collect_list:('a,'b,[<`Zero|`One|`Many])Caqti_request.t->'a->('blist,[>Caqti_error.call_or_retrieve]as'e)resultfiber(** [rev_collect_list request x] performs [request] with parameters [x] and
returns a list of rows in the reverse order of retrieval. The
accumulation is tail recursive and slighly more efficient than
{!collect_list}. *)(** {2 Transactions} *)valwith_transaction:(unit->('a,'e)resultfiber)->('a,[>Caqti_error.transact]as'e)resultfiber(** [with_transaction f] wraps [f] in a transaction which is committed iff [f]
returns [Ok _]. *)endmoduletypePopulate=sigtype+'afibertype(+'a,+'err)stream(** {2 Insertion} *)valpopulate:table:string->columns:stringlist->'aCaqti_type.t->('a,'err)stream->(unit,[>Caqti_error.call_or_retrieve|`Congestedof'err])resultfiber(** [populate table columns row_type seq] inputs the contents of [seq] into
the database in whatever manner is most efficient as decided by the
driver. *)end(** Full connection signature available to users. *)moduletypeS=sigvaldriver_info:Caqti_driver_info.t(** Information about the driver providing this connection module. *)valdriver_connection:driver_connectionoption(** The underlying connection object of the driver if available. The open
variant constructor is defined in the driver library. This is currently
only implemented for caqti-driver-sqlite3 for the purpose of defining
custom functions. *)includeBaseincludeConveniencewithtype'afiber:='afiberincludePopulatewithtype'afiber:='afiberandtype('a,'err)stream:=('a,'err)streamend